Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

‫وزارة التعليم العالي والبحث العلمي‬

‫جامعة بغداد‬
‫كلية العلوم‬
‫قسم الحاسوب‬

‫‪Data Structures‬‬
‫‪Tree program‬‬

‫اعداد الطالب ‪ :‬عبدالوهاب حسين مراد علي‬


insert new node Approach#1
node *createnode(int x){
node *h;
h=new node;
h->info=x;
h->lptr=NULL;
h->rptr=NULL;
return h;
}
void insert(node *root,int x){
if(x>=root->info){

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));
}
}

//print even numbers


void print_even(node *root){
if(root==NULL)return;
else
{
if(root->info%2==0)
cout<<root->info<<" ";
print_even(root->lptr);
print_even(root->rptr);
}
}

//sum of even numbers


int sum_even(node *root){
if(root==NULL)
return 0;
else{
if(root->info%2==0)
return (root->info+sum_even(root->lptr)+sum_even(root->rptr));
else
return (sum_even(root->lptr)+sum_even(root->rptr));
}
}
// search
void search(node *root,int x){
if(root==NULL)return;
if(root->info==x)
cout<<"found \n";
else
if(x>=root->info)
search(root->rptr,x);
else
search(root->lptr,x);
}

// 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

void del(node* root, int d) {


node* x = root;
node* y = NULL;
bool found = false;
while (x != NULL && !found) {
if (d == x->info)
found = true;
else {
y = x;
if (d > x->info)
x = x->rptr;
else
x = x->lptr;

}
}

if (!found) {
cout << "Node not exist\n" << endl;
return;
}

if (x->lptr == NULL && x->rptr == NULL) {


if (y == NULL)
root = NULL;
else if (y->lptr == x)
y->lptr = NULL;
else
y->rptr = NULL;

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(k->rptr!=NULL && k->lptr==NULL)


k=k->rptr;}

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 of the tree


int depth_tree(node *root){
if(root==NULL)
return 0;
else
{
return (1+depth_tree(root->lptr)+depth_tree(root->rptr));
depth_tree(root->lptr);

depth_tree(root->rptr);

}
}

You might also like