Professional Documents
Culture Documents
هياكل شجرة
هياكل شجرة
جامعة بغداد
كلية العلوم
قسم الحاسوب
Data Structures
Tree program
if(root->rptr==NULL)
root->rptr=createnode(x);
else
insert(root->rptr,x);
}
else{
if(root->lptr==NULL)
root->lptr=createnode(x);
else
insert(root->lptr,x);
}
}
// insert new node Approach#2
node *createnode(int x){
node *h;
h=new node;
h->info=x;
h->dup=1;
h->lptr=NULL;
h->rptr=NULL;
return h;
}
void insert(node *root,int x){
if(x==root->info)
root->dup+=1;
else{
if(x>root->info){
if(x==root->info)
root->dup+=1;
else
{
if(root->rptr==NULL)
root->rptr=createnode(x);
else
insert(root->rptr,x);
}
}
else{
if(x==root->info)
root->dup+=1;
else{
if(root->lptr==NULL)
root->lptr=createnode(x);
else
insert(root->lptr,x);}
}
}
}p
//count of the nodes
int count(node *root){
if(root==NULL)
return 0;
else
return (1+count(root->lptr)+count(root->rptr));
}
//count of nodes that have even information
int count_even(node *root){
if(root==NULL)
return 0;
else
{
if(root->info%2==0)
return (1+count_even(root->lptr)+count_even(root->rptr));
else
return (0+count_even(root->lptr)+count_even(root->rptr));
}
}
// count leaf
int count_leaf(node *root){
if(root==NULL)return 0;
else
{
if(root->lptr==NULL && root->rptr==NULL)
return (1+count_leaf(root->lptr)+count_leaf(root->rptr));
else
return (count_leaf(root->lptr)+count_leaf(root->rptr));
}
}
//print leavs
void print_leaf(node *root){
if(root==NULL)
return;
else
if(root->lptr==NULL && root->rptr==NULL)
cout<<root->info<<endl;
print_leaf(root->lptr);
print_leaf(root->rptr);
//delete
}
}
if (!found) {
cout << "Node not exist\n" << endl;
return;
}
delete x;
} else if (x->lptr == NULL || x->rptr == NULL) {
if (y == NULL) {
if (x->lptr != NULL)
root = x->lptr;
else
root = x->rptr;
} else if (y->lptr == x) {
if (x->lptr != NULL)
y->lptr = x->lptr;
else
y->lptr = x->rptr;
} else {
if (x->lptr != NULL)
y->rptr = x->lptr;
else
y->rptr = x->rptr;
}
delete x;
} else {
node* w = x;
node* z = x->lptr;
while (z->rptr != NULL) {
w = z;
z = z->rptr;
}
x->info = z->info;
if (w == x)
x->lptr = z->lptr;
else
w->rptr = z->lptr;
delete z;
}
}
//count of levels
int count_levels(node *root){int rl=0,ll=0;node *k=root;
if(root!=NULL)
{
while(k!=NULL){
ll+=1;
if(k->lptr!=NULL && k->rptr==NULL)
k=k->lptr;
else
if(k->rptr!=NULL && k->lptr==NULL)
k=k->rptr;
}
k=root;
while(k!=NULL){
rl+=1;
if(k->lptr!=NULL && k->rptr==NULL)
k=k->lptr;
else
if(ll>rl)
return ll;
else
if(ll==rl)
return 1;
else
return rl;
}
return 0;
}
// count of duplicate values
int count_dup(node *root){
if(root==NULL)
return 0;
else
{
if(root->dup>1)
return (1+count_dup(root->lptr)+count_dup(root->rptr));
count_dup(root->lptr);
count_dup(root->rptr);
}
}
// check if the tree have duplicate info
bool check_dup(node *root){
if(root==NULL)
return 0;
else
{
if(root->dup>1)
return (1+check_dup(root->lptr)+check_dup(root->rptr));
check_dup(root->lptr);
check_dup(root->rptr);
}
}
// print sibling of given node
void siblings(node *root,int x){
if(root==NULL)
return;
else
{
if(root->info==x){
cout<<root->rptr->info<<endl;
cout<<root->lptr->info<<endl;
}
siblings(root->lptr,x);
siblings(root->rptr,x);
}
}
//height of tree
int height_tree(node *root){
if(root==NULL)
return 0;
else
{
return (1+height_tree(root->lptr)+height_tree(root->rptr));
height_tree(root->lptr);
height_tree(root->rptr);
}
}
depth_tree(root->rptr);
}
}