binary tree beginning, need one more function
May 13, 2009 at 9:52pm UTC
hi. I am working on binary tree, but jst want to see is everything looking good so far.
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include<iostream>
#include<stdio.h>
#include <string>
using namespace std;
struct node
{
string key_value;
node *left;
node *right;
}*p_akt;
class btree
{
public :
btree();
~btree();
void create();
bool Lchild(node);
bool Rchild(node);
void make(node,string question,string animal);
void data(node *p);
void root();
void Parent(node *p,node * root);
int brojac;
//void insert(string key);
//node *search(string key);
//void destroy_tree();
private :
};
btree::btree()
{
p_akt=0;
brojac=0;
// root=NULL;
}
void btree::create()
{
node * temp=p_akt;
}
void btree::make(node,string question,string animal)
{
node * temp=p_akt;
temp=p_akt;
temp->left=new node;
temp->left->key_value= question;
temp->left->right=new node;
temp->left->right->key_value= animal;
}
bool Lchild(node)
{
node * temp = p_akt;
if (temp->left!=NULL)
{
temp=temp->left;
return true ;
}
else
return false ;
}
bool Rchild(node *p)
{
node * temp = p;
if (temp->right!=NULL)
{
temp=temp->right;
return true ;
}
else
return false ;
}
void btree::data(node *p)
{
cout<< (string)p->key_value<<endl;
}
void btree::Parent(node *p,node *root)
{
node * temp = root;
if (temp==NULL)
return ;
else if ((temp->left!=NULL) || (temp->right!=NULL))
{
if ((temp->left->key_value==p->key_value) || (temp->right->key_value==p->key_value))
temp=p;
}
else
{
node * temp2=temp;
temp=temp->left;
Parent(p,temp);
temp2=temp->right;
Parent(p, temp2);
}
}
i need one more function, which will take me back to the root, but don't know how to write it.
May 13, 2009 at 9:57pm UTC
You can't go back to the root from a node in a proper tree, since it doesn't have cycles. If you need both the root and a node in a function, just pass both.
Topic archived. No new replies allowed.