Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
VRITIKA NAIK
LinkedIn: vritika-naik
Twitter: @NaikVritika
WHAT IS A 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 {
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;
HEIGHT OF return 0;
child=ptr->lchild;
child=ptr->rchild;
root=child;
par->lchild=child;
par->rchild=child;
free(ptr);
return root;
parsucc = ptr;
succ = ptr->rchild;
while(succ->lchild!=NULL)
{ parsucc = succ;
succ = succ->lchild;
ptr->info = succ->info;
else
return root;
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