Sei sulla pagina 1di 36

TREES

VRITIKA NAIK
LinkedIn: vritika-naik
Twitter: @NaikVritika
WHAT IS A TREE?

• A tree is a finite set of nodes such that:


(i) There is a distinguished node called root and
(ii) The remaining nodes are partitioned into n>=0 disjoint sets T1, T2,..,Tn,
where each of these sets is a tree. The sets T1,T2,..,Tn are the subtrees of the
root.
Height = 4
Edge
Node
BINARY TREE

• In Binary tree, no node can have more than 2


children, i.e. every node can have 0, 1 or 2
children. Each child is either left or right child.
• A binary tree is defined as a set of nodes that
is:
(i) Either empty or
(ii) Consists of a distinguished node called root
and remaining nodes are partitioned into 2
disjoint sets T1 and T2 and both are binary
trees. T1 is called left subtree and T2 is
called right subtree.
Extended Binary Tree

Types of
Binary
Tree
• Array Representation
REPRESENTING A
BINARY TREE • Linked List Representation
TRAVERSAL IN
BINARY TREES

• Inorder : LNR
• Preorder: NLR
• Post Order: LRN
struct node
NODE {

REPRESENTATI int data;


struct node *leftChild;

ON };
struct node *rightChild;
void preorder(struct treenode *ptr)

PREORDER {
if(root==NULL)

TRAVERSAL {
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}/*End of preorder( )*/
void inorder(struct treenode *ptr)

INORDER {
if(root==NULL)

TRAVERSAL {
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder( )*/
POSTORDE void postorder(struct treenode *ptr)
R {

TRAVERSA
if(root==NULL)
{

L
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}/*End of postorder( )*/
PREORDER : ABDHECFIGJK
INORDER : DHBEAIFCJGK

CREATION OF
BINARY TREE
FROM
INORDER AND
PREORDER
TRAVERSALS
POSTORDER : HIDJEBKFGCA
INORDER : HDIBEJAKFCG

CREATION OF
BINARY TREE
FROM
INORDER AND
POSTORDER
TRAVERSALS
int height(struct node *ptr)
{
int h_left, h_right;

if (ptr == NULL) /*Base Case*/

HEIGHT OF return 0;

BINARY h_left = height(ptr->lchild);


h_right = height(ptr->rchild);
TREE
if (h_left > h_right)
return 1 + h_left;
else
return 1 + h_right;
}/*End of height()*/
EXPRESSION TREE
BINARY SEARCH
TREE
• A binary tree that is empty or satisfies
the following properties:
1) All keys in left subtree of root are less
than the key in the root
2) All keys in right subtree of root are
greater than the key in the root
3) Left and right subtrees of root are also
binary trees
TRAVERSALS IN
BST
• PREORDER : 67 34 12 10 45 38
60 80 78 95 86
• INORDER : 10 12 34 38 45 60 67
78 80 86 95
• POSTORDER : 10 12 38 60 45 34
78 86 95 80 67
• LEVEL ORDER : 67 34 80 12 45
78 95 10 38 60 86
SEARCH
struct node *search_nrec(struct node *ptr, int skey)
{
while(ptr!=NULL)
{
if(skey < ptr->info)
ptr = ptr->lchild; /*Move to left child*/
else if(skey > ptr->info)
ptr = ptr->rchild; /*Move to right child */
else /*skey found*/
return ptr;
}
return NULL;
}/*End of search_nrec()*/
struct node *min_nrec(struct node *ptr)
{
if(ptr!=NULL)
while(ptr->lchild!=NULL)
ptr=ptr->lchild;
return ptr;

MIN-MAX }/*End of min_nrec()*/

NODES struct node *max_nrec(struct node *ptr)


{
if(ptr!=NULL)
while(ptr->rchild!=NULL)
ptr=ptr->rchild;
return ptr;
}/*End of max_nrec()*/
INSERTION
struct node *insert_nrec(struct node *root, int ikey)
{ struct node *tmp,*par,*ptr;
ptr = root;
par = NULL;
while( ptr!=NULL)
{ par = ptr;
if(ikey < ptr->info)
ptr = ptr->lchild;
else if( ikey > ptr->info )
ptr = ptr->rchild;
else
{ printf("Duplicate key");
return root;
}
}
INSERTION
(continued…)
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=ikey;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(par==NULL)
root=tmp;
else if( ikey < par->info )
par->lchild=tmp;
else
par->rchild=tmp;
return root;
}/*End of insert_nrec( )*/
• 3 possibilities:
1) Node has no child, i.e. it is a leaf node
DELETION 2) Node has exactly 1 child
3) Node has exactly 2 children
struct node *del_nrec(struct node *root, int dkey)
{ struct node *par,*ptr;
ptr = root;
par = NULL;
while(ptr!=NULL)
{ if( dkey == ptr->info)
break;
par = ptr;
if(dkey < ptr->info)
ptr = ptr->lchild;
else
ptr = ptr->rchild;
}
if(ptr==NULL)
printf("dkey not present in tree\n");
else if(ptr->lchild!=NULL && ptr->rchild!=NULL)/*2 children*/
root = case_c(root,par,ptr);
else if(ptr->lchild!=NULL )/*only left child*/
root = case_b(root, par,ptr);
else if(ptr->rchild!=NULL)/*only right child*/
root = case_b(root, par,ptr);
else /*no child*/
root = case_a(root,par,ptr);
return root;
}/*End of del_nrec( )*/
CASE A: Leaf Node
struct node *case_a(struct node *root, struct node *par,struct node *ptr )
{
if(par==NULL) /*root node to be deleted*/
root=NULL;
else if(ptr==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
free(ptr);
return root;
}/*End of case_a( )*/
CASE B: Exactly 1 child
struct node *case_b(struct node *root,struct node *par,struct node *ptr)

{ struct node *child;

if(ptr->lchild!=NULL) /*node to be deleted has left child */

child=ptr->lchild;

else /*node to be deleted has right child */

child=ptr->rchild;

if(par==NULL ) /*node to be deleted is root node*/

root=child;

else if( ptr==par->lchild) /*node is left child of its parent*/

par->lchild=child;

else /*node is right child of its parent*/

par->rchild=child;

free(ptr);

return root;

}/*End of case_b( )*/


CASE C: Exactly 2 children
struct node *case_c(struct node *root, struct node *par,struct node *ptr)

{ struct node *succ,*parsucc;

parsucc = ptr;

succ = ptr->rchild;

while(succ->lchild!=NULL)

{ parsucc = succ;

succ = succ->lchild;

ptr->info = succ->info;

if(succ->lchild==NULL && succ->rchild==NULL)

root = case_a(root, parsucc,succ);

else

root = case_b(root, parsucc,succ);

return root;

}/*End of case_c( )*/


• A binary tree is made threaded by making all right child pointers that would normally
be NULL point to the inorder successor of the node (if it exists).
• There are two types of threaded binary trees.
THREADED BST Single Threaded: Where a NULL right pointers is made to point to the inorder
successor (if successor exists)
• Double Threaded: Where both left and right NULL pointers are made to point to
inorder predecessor and inorder successor respectively. The predecessor threads are
useful for reverse inorder traversal and postorder traversal.
INSERTION AND
DELETION
AVL TREE
• AVL tree is a self-balancing binary search
tree in which each node maintains extra
information called a balance factor whose
value is either -1, 0 or +1.
• AVL tree got its name after its inventor
Georgy Adelson-Velsky and Landis.
• Balance factor of a node in an AVL tree is
the difference between the height of the
left subtree and that of the right subtree of
that node.
• Balance Factor = (Height of Left Subtree -
Height of Right Subtree) or (Height of
Right Subtree - Height of Left Subtree)
RBT
• Red-Black tree is a self-balancing binary search tree in
which each node contains an extra bit for denoting the
color of the node, either red or black.
• A red-black tree satisfies the following properties:
1. Red/Black Property: Every node is colored, either red
or black.
2. Root Property: The root is black.
3. Leaf Property: Every leaf (NIL) is black.
4. Red Property: If a red node has children then, the
children are always black.
5. Depth Property: For each node, any simple path from
this node to any of its descendant leaf has the same
black-depth (the number of black nodes).
• Binary tree satisfying the following properties:

HEAP 1) Structure property – All the levels have maximum number of nodes
except possibly the last level. In the last level, all nodes occur to the left
2) Heap Order Property – The key value in any node N is greater than or
equal to the key values in both its children.
B TREE

• B-tree is a special type of self-balancing search


tree in which each node can contain more than
one key and can have more than two children.
It is a generalized form of the binary search
tree.
• It is also known as a height-balanced m-way
tree.
• There was a need for such types of data
structures that minimize the disk accesses.
• A B+ tree is an advanced form of a self-balancing tree in which all the
values are present in the leaf level.
B+ TREE • An important concept to be understood before learning B+ tree is multilevel
indexing, i.e. nodes contain keys and corresponding data items. It makes
accessing the data easier and faster.
• Suited for sequential access
THANK YOU!
VRITIKA NAIK
LinkedIn: vritika-naik
Twitter: @NaikVritika

Potrebbero piacerti anche