0% found this document useful (0 votes)
91 views

Tree

The document describes trees and binary trees. It defines key tree terminology like nodes, roots, parents, children, leaves, height, levels, and degrees. It explains that a binary tree is a tree where each node has at most two children. It also describes different tree traversals like preorder, inorder and postorder traversal sequences. The document then provides an example C program for implementing an AVL tree with functions for insertion, deletion, finding minimum/maximum nodes, and tree traversals.

Uploaded by

Sitaram Sds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Tree

The document describes trees and binary trees. It defines key tree terminology like nodes, roots, parents, children, leaves, height, levels, and degrees. It explains that a binary tree is a tree where each node has at most two children. It also describes different tree traversals like preorder, inorder and postorder traversal sequences. The document then provides an example C program for implementing an AVL tree with functions for insertion, deletion, finding minimum/maximum nodes, and tree traversals.

Uploaded by

Sitaram Sds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

Tree

Nodes
Each node can have 0 or more children
A node can have at most one parent
Binary tree
Tree with 02 children per node
Terminology
Node user-defined data structure that that contains pointers to data and
pointers to other nodes:
Root Node from which all other nodes descend
Parent has child nodes arranged in subtrees.
Child nodes in a tree have 0 or more children.
Leaf node without descendants
Root no parent
Leaf no child
Interior non-leaf
Height distance from root to leaf
Degree number of direct children a tree/subtree has.
Levels : Root of a tree is at Level 1, it's children at Level 2, it's grandchildren are
at Level 3...
A tree in which each node can only have 0, 1, or 2 children.
A binary tree is either empty or consists of a root, and (possibly empty) left and
right subtrees.
Note the recursive definition in the above!
Tree traversal
Preorder traversal sequence root, left, right
Inorder traversal sequence: (left, root, right)

Postorder traversal sequence: (left, right, root)

Preorder traversal sequence: F, B, A, D, C, E, G, I, H

Inorder traversal sequence: A, B, C, D, E, F, G, H, I

Postorder traversal sequence: A, C, E, D, B, H, I, G, F

AVL Tree C Program


# include <iostream.h>
# include <stdlib.h>
# include <conio.h>
struct node
{
int element;
node *left;
node *right;
int height;
};
typedef struct node *nodeptr;
class bstree
{

public:
void insert(int,nodeptr &);
void del(int, nodeptr &);
int deletemin(nodeptr &);
void find(int,nodeptr &);
nodeptr findmin(nodeptr);
nodeptr findmax(nodeptr);
void copy(nodeptr &,nodeptr &);
void makeempty(nodeptr &);
nodeptr nodecopy(nodeptr &);
void preorder(nodeptr);
void inorder(nodeptr);
void postorder(nodeptr);
int bsheight(nodeptr);
nodeptr srl(nodeptr &);
nodeptr drl(nodeptr &);
nodeptr srr(nodeptr &);
nodeptr drr(nodeptr &);
int max(int,int);
int nonodes(nodeptr);
};
// Inserting a node
void bstree::insert(int x,nodeptr &p)
{
if (p == NULL)
{
p = new node;
p->element = x;
p->left=NULL;
p->right = NULL;
p->height=0;
if (p==NULL)
cout<<"Out of Space";
}
else
{
if (x<p->element)
{
insert(x,p->left);
if ((bsheight(p->left) - bsheight(p->right))==2)
{

if (x < p->left->element)
p=srl(p);
else
p = drl(p);
}
}
else if (x>p->element)
{
insert(x,p->right);
if ((bsheight(p->right) - bsheight(p->left))==2)
{
if (x > p->right->element)
p=srr(p);
else
p = drr(p);
}
}
else
cout<<"Element Exists";
}
int m,n,d;
m=bsheight(p->left);
n=bsheight(p->right);
d=max(m,n);
p->height = d + 1;
}
// Finding the Smallest
nodeptr bstree::findmin(nodeptr p)
{
if (p==NULL)
{
cout<<"
Empty Tree
";
return p;
}
else
{
while(p->left !=NULL)
p=p->left;
return p;

}
}
// Finding the Largest
nodeptr bstree::findmax(nodeptr p)
{
if (p==NULL)
{
cout<<"
Empty Tree
";
return p;
}
else
{
while(p->right !=NULL)
p=p->right;
return p;
}
}
// Finding an element
void bstree::find(int x,nodeptr &p)
{
if (p==NULL)
cout<<"
Element not found
";
else
if (x < p->element)
find(x,p->left);
else
if (x>p->element)
find(x,p->right);
else
cout<<"
Element found !
";
}
// Copy a tree
void bstree::copy(nodeptr &p,nodeptr &p1)
{
makeempty(p1);

p1 = nodecopy(p);
}
// Make a tree empty
void bstree::makeempty(nodeptr &p)
{
nodeptr d;
if (p != NULL)
{
makeempty(p->left);
makeempty(p->right);
d=p;
free(d);
p=NULL;
}
}
// Copy the nodes
nodeptr bstree::nodecopy(nodeptr &p)
{
nodeptr temp;
if (p==NULL)
return p;
else
{
temp = new node;
temp->element = p->element;
temp->left = nodecopy(p->left);
temp->right = nodecopy(p->right);
return temp;
}
}
// Deleting a node
void bstree::del(int x,nodeptr &p)
{
nodeptr d;
if (p==NULL)
cout<<"Element not found
";
else if ( x < p->element)
del(x,p->left);
else if (x > p->element)
del(x,p->right);

else if ((p->left == NULL) && (p->right == NULL))


{
d=p;
free(d);
p=NULL;
cout<<"
Element deleted !
";
}
else if (p->left == NULL)
{
d=p;
free(d);
p=p->right;
cout<<"
Element deleted !
";
}
else if (p->right == NULL)
{
d=p;
p=p->left;
free(d);
cout<<"
Element deleted !
";
}
else
p->element = deletemin(p->right);
}
int bstree::deletemin(nodeptr &p)
{
int c;
cout<<"inside deltemin
";
if (p->left == NULL)
{
c=p->element;
p=p->right;
return c;

}
else
{
c=deletemin(p->left);
return c;
}
}
void bstree::preorder(nodeptr p)
{
if (p!=NULL)
{
cout<<p->element<<"-->";
preorder(p->left);
preorder(p->right);
}
}
// Inorder Printing
void bstree::inorder(nodeptr p)
{
if (p!=NULL)
{
inorder(p->left);
cout<<p->element<<"-->";
inorder(p->right);
}
}
// PostOrder Printing
void bstree::postorder(nodeptr p)
{
if (p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->element<<"-->";
}
}
int bstree::max(int value1, int value2)
{

return ((value1 > value2) ? value1 : value2);


}
int bstree::bsheight(nodeptr p)
{
int t;
if (p == NULL)
return -1;
else
{
t = p->height;
return t;
}
}
nodeptr bstree:: srl(nodeptr &p1)
{
nodeptr p2;
p2 = p1->left;
p1->left = p2->right;
p2->right = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(bsheight(p2->left),p1->height) + 1;
return p2;
}
nodeptr bstree:: srr(nodeptr &p1)
{
nodeptr p2;
p2 = p1->right;
p1->right = p2->left;
p2->left = p1;
p1->height = max(bsheight(p1->left),bsheight(p1->right)) + 1;
p2->height = max(p1->height,bsheight(p2->right)) + 1;
return p2;
}
nodeptr bstree:: drl(nodeptr &p1)
{
p1->left=srr(p1->left);
return srl(p1);
}
nodeptr bstree::drr(nodeptr &p1)
{

p1->right = srl(p1->right);
return srr(p1);
}
int bstree::nonodes(nodeptr p)
{
int count=0;
if (p!=NULL)
{
nonodes(p->left);
nonodes(p->right);
count++;
}
return count;
}
int main()
{
clrscr();
nodeptr root,root1,min,max;//,flag;
int a,choice,findele,delele,leftele,rightele,flag;
char ch='y';
bstree bst;
//system("clear");
root = NULL;
root1=NULL;
cout<<"
AVL Tree
";
cout<<" ========
";
do
{
cout<<"
1.Insertion
2.FindMin
";
cout<<"3.FindMax
4.Find
5.Copy
";

cout<<"6.Delete
7.Preorder
8.Inorder
";
cout<<" 9.Postorder
10.height
";
cout<<"
Enter the choice:
";
cin>>choice;
switch(choice)
{
case 1:
cout<<"
New node's value ?
";
cin>>a;
bst.insert(a,root);
break;
case 2:
if (root !=NULL)
{
min=bst.findmin(root);
cout<<"
Min element : "<<min->element;
}
break;
case 3:
if (root !=NULL)
{
max=bst.findmax(root);
cout<<"
Max element : "<<max->element;
}
break;
case 4:
cout<<"
Search node :
";
cin>>findele;

if (root != NULL)
bst.find(findele,root);
break;
case 5:
bst.copy(root,root1);
bst.inorder(root1);
break;
case 6:
cout<<"Delete Node ?
";
cin>>delele;
bst.del(delele,root);
bst.inorder(root);
break;
case 7:
cout<<"
Preorder Printing... :
";
bst.preorder(root);
break;
case 8:
cout<<"
Inorder Printing.... :
";
bst.inorder(root);
break;
case 9:
cout<<"
Postorder Printing... :
";
bst.postorder(root);
break;
case 10:
cout<<"
Height and Depth is
";
cout<<bst.bsheight(root);
cout<<"No. of nodes:- "<<bst.nonodes(root);

break;
}
cout<<"
Do u want to continue (y/n) ?
";
cin>>ch;
}while(ch=='y');
return 0;
}

Full Binary Tree : a binary tree with height h, and no empty trees in Levels 1
through h
All nodes either have two children or are leaves
Contains a total of 2h-1 nodes
h = 4 in example
/* binary search tree*/

#include<stdio.h>
#include<conio.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
};
struct tree *create();
void preorder(struct tree *);
void inorder(struct tree *);
void postorder(struct tree *);
struct tree *create()
{
struct tree *p,*root;
int m,x;
char s;
root=(struct tree *)malloc(sizeof(struct tree));
printf("\nenter the value of the main root");
scanf("%d",&m);
root->data=m;
root->left=NULL;
root->right=NULL;
printf("\nenter n to stop creation of the binary search tree");
fflush(stdin);
scanf("%c",&s);
while(s!='n')
{
p=root;
printf("\nenter the value of the newnode");
fflush(stdin);
scanf("%d",&x);
while(1)
{
if(x<p->data)
{
if(p->left==NULL)
{
p->left=(struct tree *)malloc(sizeof(struct tree));
p=p->left;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->left;
}
else
{

if(p->right==NULL)
{
p->right=(struct tree *)malloc(sizeof(struct tree));
p=p->right;
p->data=x;
p->right=NULL;
p->left=NULL;
break;
}
else
p=p->right;
}
}
printf("\nwant to continue");
fflush(stdin);
scanf("%c",&s);
}
return(root);
}
void preorder(struct tree *p)
{
if(p!=NULL)
{
printf("%d ",p->data);
preorder(p->left);
preorder(p->right);
}
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("\t%d",p->data);
inorder(p->right);
}
}
void postorder(struct tree *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("\t%d",p->data);
}
}
void main()
{
int h;
struct tree *root;

clrscr();
while(1)
{
printf("\nenter 1. for creation of the binary search tree");
printf("\nenter 2. for preorder traversal");
printf("\nenter 3. for inorder traversal");
printf("\nenter 4. for postorder traversal");
printf("\nenter 5. for exit");
printf("\nenter your choice");
scanf("%d",&h);
switch(h)
{
case 1:
root=create();
break;
case 2:
preorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(0);
default:
printf("\nentered a wrong choice");
}
}
}

PROGRAM FOR IMPLEMENTATION OF B-TREE (INSERTION & DELETION)


using / in c
#include<stdio.h>
#include <conio.h>
#define M 5
struct node{
int n; /* n < M No. of keys in node will always less than order of B
tree */
int keys[M-1]; /*array of keys*/
struct node *p[M]; /* (n+1 pointers will be in use) */
}*root=NULL;
enum KeyStatus { Duplicate,SearchFailure,Success,InsertIt,LessKeys };

void insert(int key);


void display(struct node *root,int);
void DelNode(int x);
void search(int x);
enum KeyStatus ins(struct node *r, int x, int* y, struct node** u);
int searchPos(int x,int *key_arr, int n);
enum KeyStatus del(struct node *r, int x);
int main()
{
int key;
int choice;
printf("Creation of B tree for node %d\n",M);
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Search\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the key : ");
scanf("%d",&key);
insert(key);
break;
case 2:
printf("Enter the key : ");
scanf("%d",&key);
DelNode(key);
break;
case 3:
printf("Enter the key : ");
scanf("%d",&key);
search(key);
break;
case 4:
printf("Btree is :\n");
display(root,0);
break;
case 5:
exit(1);
default:

printf("Wrong choice\n");
break;
}/*End of switch*/
}/*End of while*/
return 0;
}/*End of main()*/
void insert(int key)
{
struct node *newnode;
int upKey;
enum KeyStatus value;
value = ins(root, key, &upKey, &newnode);
if (value == Duplicate)
printf("Key already available\n");
if (value == InsertIt)
{
struct node *uproot = root;
root=malloc(sizeof(struct node));
root->n = 1;
root->keys[0] = upKey;
root->p[0] = uproot;
root->p[1] = newnode;
}/*End of if */
}/*End of insert()*/
enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct node
**newnode)
{
struct node *newPtr, *lastPtr;
int pos, i, n,splitPos;
int newKey, lastKey;
enum KeyStatus value;
if (ptr == NULL)
{
*newnode = NULL;
*upKey = key;
return InsertIt;
}
n = ptr->n;
pos = searchPos(key, ptr->keys, n);
if (pos < n && key == ptr->keys[pos])
return Duplicate;
value = ins(ptr->p[pos], key, &newKey, &newPtr);
if (value != InsertIt)
return value;
/*If keys in node is less than M-1 where M is order of B tree*/
if (n < M - 1)

{
pos = searchPos(newKey, ptr->keys, n);
/*Shifting the key and pointer right for inserting the new key*/
for (i=n; i>pos; i--)
{
ptr->keys[i] = ptr->keys[i-1];
ptr->p[i+1] = ptr->p[i];
}
/*Key is inserted at exact location*/
ptr->keys[pos] = newKey;
ptr->p[pos+1] = newPtr;
++ptr->n; /*incrementing the number of keys in node*/
return Success;
}/*End of if */
/*If keys in nodes are maximum and position of node to be inserted is
last*/
if (pos == M - 1)
{
lastKey = newKey;
lastPtr = newPtr;
}
else /*If keys in node are maximum and position of node to be inserted
is not last*/
{
lastKey = ptr->keys[M-2];
lastPtr = ptr->p[M-1];
for (i=M-2; i>pos; i--)
{
ptr->keys[i] = ptr->keys[i-1];
ptr->p[i+1] = ptr->p[i];
}
ptr->keys[pos] = newKey;
ptr->p[pos+1] = newPtr;
}
splitPos = (M - 1)/2;
(*upKey) = ptr->keys[splitPos];
(*newnode)=malloc(sizeof(struct node));/*Right node after split*/
ptr->n = splitPos; /*No. of keys for left splitted node*/
(*newnode)->n = M-1-splitPos;/*No. of keys for right splitted node*/
for (i=0; i < (*newnode)->n; i++)
{
(*newnode)->p[i] = ptr->p[i + splitPos + 1];
if(i < (*newnode)->n - 1)
(*newnode)->keys[i] = ptr->keys[i + splitPos + 1];
else
(*newnode)->keys[i] = lastKey;
}

(*newnode)->p[(*newnode)->n] = lastPtr;
return InsertIt;
}/*End of ins()*/
void display(struct node *ptr, int blanks)
{
if (ptr)
{
int i;
for(i=1;i<=blanks;i++)
printf(" ");
for (i=0; i < ptr->n; i++)
printf("%d ",ptr->keys[i]);
printf("\n");
for (i=0; i <= ptr->n; i++)
display(ptr->p[i], blanks+10);
}/*End of if*/
}/*End of display()*/
void search(int key)
{
int pos, i, n;
struct node *ptr = root;
printf("Search path:\n");
while (ptr)
{
n = ptr->n;
for (i=0; i < ptr->n; i++)
printf(" %d",ptr->keys[i]);
printf("\n");
pos = searchPos(key, ptr->keys, n);
if (pos < n && key == ptr->keys[pos])
{
printf("Key %d found in position %d of last dispalyed
node\n",key,i);
return;
}
ptr = ptr->p[pos];
}
printf("Key %d is not available\n",key);
}/*End of search()*/
int searchPos(int key, int *key_arr, int n)
{
int pos=0;
while (pos < n && key > key_arr[pos])
pos++;
return pos;

}/*End of searchPos()*/
void DelNode(int key)
{
struct node *uproot;
enum KeyStatus value;
value = del(root,key);
switch (value)
{
case SearchFailure:
printf("Key %d is not available\n",key);
break;
case LessKeys:
uproot = root;
root = root->p[0];
free(uproot);
break;
}/*End of switch*/
}/*End of delnode()*/
enum KeyStatus del(struct node *ptr, int key)
{
int pos, i, pivot, n ,min;
int *key_arr;
enum KeyStatus value;
struct node **p,*lptr,*rptr;
if (ptr == NULL)
return SearchFailure;
/*Assigns values of node*/
n=ptr->n;
key_arr = ptr->keys;
p = ptr->p;
min = (M - 1)/2;/*Minimum number of keys*/
pos = searchPos(key, key_arr, n);
if (p[0] == NULL)
{
if (pos == n || key < key_arr[pos])
return SearchFailure;
/*Shift keys and pointers left*/
for (i=pos+1; i < n; i++)
{
key_arr[i-1] = key_arr[i];
p[i] = p[i+1];
}
return --ptr->n >= (ptr==root ? 1 : min) ? Success : LessKeys;
}/*End of if */

if (pos < n && key == key_arr[pos])


{
struct node *qp = p[pos], *qp1;
int nkey;
while(1)
{
nkey = qp->n;
qp1 = qp->p[nkey];
if (qp1 == NULL)
break;
qp = qp1;
}/*End of while*/
key_arr[pos] = qp->keys[nkey-1];
qp->keys[nkey - 1] = key;
}/*End of if */
value = del(p[pos], key);
if (value != LessKeys)
return value;
if (pos > 0 && p[pos-1]->n > min)
{
pivot = pos - 1; /*pivot for left and right node*/
lptr = p[pivot];
rptr = p[pos];
/*Assigns values for right node*/
rptr->p[rptr->n + 1] = rptr->p[rptr->n];
for (i=rptr->n; i>0; i--)
{
rptr->keys[i] = rptr->keys[i-1];
rptr->p[i] = rptr->p[i-1];
}
rptr->n++;
rptr->keys[0] = key_arr[pivot];
rptr->p[0] = lptr->p[lptr->n];
key_arr[pivot] = lptr->keys[--lptr->n];
return Success;
}/*End of if */
if (posn > min)
{
pivot = pos; /*pivot for left and right node*/
lptr = p[pivot];
rptr = p[pivot+1];
/*Assigns values for left node*/
lptr->keys[lptr->n] = key_arr[pivot];
lptr->p[lptr->n + 1] = rptr->p[0];
key_arr[pivot] = rptr->keys[0];
lptr->n++;

rptr->n--;
for (i=0; i < rptr->n; i++)
{
rptr->keys[i] = rptr->keys[i+1];
rptr->p[i] = rptr->p[i+1];
}/*End of for*/
rptr->p[rptr->n] = rptr->p[rptr->n + 1];
return Success;
}/*End of if */
if(pos == n)
pivot = pos-1;
else
pivot = pos;
lptr = p[pivot];
rptr = p[pivot+1];
/*merge right node with left node*/
lptr->keys[lptr->n] = key_arr[pivot];
lptr->p[lptr->n + 1] = rptr->p[0];
for (i=0; i < rptr->n; i++)
{
lptr->keys[lptr->n + 1 + i] = rptr->keys[i];
lptr->p[lptr->n + 2 + i] = rptr->p[i+1];
}
lptr->n = lptr->n + rptr->n +1;
free(rptr); /*Remove right node*/
for (i=pos+1; i < n; i++)
{
key_arr[i-1] = key_arr[i];
p[i] = p[i+1];
}
return --ptr->n >= (ptr == root ? 1 : min) ? Success : LessKeys;
}/*End of del()*/

You might also like