#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct node
{
int key;
node *Left, *Right, *Parent;
};
node* Add(node* &r, int k, node* Par = nullptr)
{
if(!r)
{
r = new node;
r->Left = r->Right = nullptr;
r->key = k;
r->Parent = Par;
return r;
}
if (k == r->key) return r;
if(r->key > k) return Add(r->Left, k, r);
return Add(r->Right, k, r);
}
void Print_in_order (node* r)
{
if (!r) return;
Print_in_order(r->Left);
cout<<r->key<<" - ";
Print_in_order(r->Right);
}
void Myk (node* r, int depth = 0)
{
if (!r) return;
Myk(r->Right, depth+1);
for(int i=0; i<depth; i++) cout<<" ";
cout<<r->key<<endl;
Myk(r->Left, depth+1);
}
node* Find(node *r, int key)
{
if (!r) return 0;
if (key == r->key) return r;
if (r->key > key) return Find(r->Left, key);
return Find(r->Right, key);
}
int main()
{
node* root = nullptr;
srand(time(0));
for(int i=0; i<15; i++)
{
int los = rand()%80;
Add(root,los);
cout<<"dodano "<<los<<endl;
}
cout<<endl<<endl;
Myk(root);
cout<<endl<<endl;
return 1;
}
Witam, mam tu dwukierunkowe drzewo, ale moje pytanie brzmi jak je przerobić na drzewo BST - Binarne drzewo poszukiwań? Wiem, że jeżeli element drzewa BST znajduje się w lewej/prawej gałęzi to jest mniejszy od swego poprzednika, ale jak coś takiego wprowadzić?
Pozdrawiam K. S.