Professional Documents
Culture Documents
Lec 6 (Trees)
Lec 6 (Trees)
Lec 6 (Trees)
B C
D E
F G
24
Complete Binary Tree
A
B B
Skewed Binary Tree
C B C
D
D E F G
E
H I
Sequential Representation of
binary tree
Sequential Representation of Binary Tree
data
left right
Applications of Binary tree
• Implementing routing table in router
• Expression evaluation
• To solve database problems such as indexing.
• Data Compression code.
Q. In a complete Binary tree if node is stored
at index 7(considering index starts from 1),
then which of the following index contain
sibling node.
A)8
B)3
C)6
D)May be 6 or 8
Answer:- C
Q. Following sequence of nodes represent which
type of binary tree?
[1,2,3,4,5,_,_,_,_,6,7]
A.Complete Binary Tree
B.Full Binary Tree
C.Tree
D.None
Answer:- B
Q. From the following sequence of nodes what is
the height of node C?
[A,B,C,_,_,D,_,_,_,_,_,_,E]
A.1
B.3
C.2
D.0
Answer:- c
Tree Traversals
• Pre-Order
–NLR
• In-Order
–LNR
• Post-Order
–LRN
Tree Traversals
Pre-Order(NLR)
1, 3, 5, 9, 6, 8
Tree Traversals
In-Order(LNR)
5, 3, 9, 1, 8, 6
Tree Traversals
Post-Order(LRN)
5, 9, 3, 8, 6, 1
Binary Tree Traversal
Preorder Traversal of Binary Tree
PREORDER (INFO, LEFT, RIGHT, ROOT )
1.IF: ROOT == NULL
THEN: Return
49
In-order traversal
INORDER(INFO,LEFT,RIGHT,ROOT)
Set PTR = STACK[TOP] and TOP = TOP-1 [pop root of sub-tree from
stack]
3. Repeat steps 5 to 7 while PTR ≠ NULL
4. Apply PROCESS to PTR info
5. [right child?] If PTR right ≠ NULL then:
a) Set PTR = PTR right
b) go to step 2
[end of if structure]
6. Set PTR = STACK[TOP] and TOP = TOP-1 [backtracking]
[end of step 4 loop]
7. Exit
example
51
Postorder traversal
1.P= Root.
2. Push(Stack, Root)
3. Repeat step 4 to 13 while (stack is non empty)
4. P=Stack[Top]
5. If (Left[P]!=Null && Visited[Left[P]]==FALSE)
6. Push(Stack, Left[P])
7. Else
8.If(Right[P]!=Null && Visited[Right[P]]==FALSE)
9. Push(Stack, Right[P])
10. Else #define BOOL int
11. Pop(Stack) struct node {
12. Print Info[P] int info;
13.visited[P]=TRUE struct node *left;
[End of if] struct node
[End of while] *right;
14.Stop 52 BOOL visited;
example
53
Q. Complete the logic of Preorder Tree traversal :-
PREORDER(INFO,LEFT,RIGHT,ROOT)
1. [initially push NULL onto STACK , and initialize PTR]
Set TOP = 1, STACK[1] = NULL and PTR = ROOT
2. Repeat steps 3 to 5 while PTR≠NULL
3. Apply PROCESS to PTR info
4. [right child?]
If PTR right ≠ NULL , then [push on stack]
Set TOP = TOP+1, and STACK[TOP] = right[ptr]
[end of if structure]
5. [left child?]
If PTR left ≠ NULL , then
Set PTR = PTR left
Else:
Set [Write Here]
[end of if structure]
[end of step 2 loop]
6.exit
A. TOP = TOP+1, and STACK[TOP] = right[ptr]
B. PTR = STACK[TOP] and TOP = TOP-1
C. PTR = =STACK[TOP] and TOP = TOP+1
D. None
Answer:- B
Tree traversal Applications
• Pre-order
– Tree copying
– Counting the number of nodes
– Counting the number of leaves
– Prefix notation from a expression tree
• Post-order
– Deleting a binary tree
– All stack oriented programming languages – mostly functional
languages which fully works on nested functions.
– Calculator programs
– postfix notation in an expression tree – used in calculators
• In-order
– we can extract the sorted values in a BST
Pre-order:-
Inorder:-
Post order-
Problem: Create a tree from the given traversals
preorder: F A E K C D H G B
inorder: E A C K F H D B G
Problem: Create a tree from the given traversals
preorder: F A E K C D H G B
inorder: E A C K F H D B G
E K
C
As K is to the left of C in preorder
• Creating the right subtree of F
• The root node is D
• From inorder, the nodes on the left of D are: H (left root of D)
the nodes on the right of D are: B G (right root of D)
Thus the tree is:
A D
E K H G
C B
F
A D
E K H G
C B
• Ex:
• Draw the tree :
– Preorder: ABDGCEHIF
– Inorder: DGBAHEICF
– PostOrder: GDBHIEFCA
Q. Construct tree from following traversal
Preorder:-J,E,A,G,F,H,R,P,Q,T
Inorder:- A,E,F,G,H,J,Q,P,R,T
Q. Construct tree from following traversal
Inorder:- A,E,F,G,H,J,Q,P,R,T
Postorder:-A,F,H,G,E,Q,P,T,R,J
Q. Find the root of left sub tree if post order and in order tree traversal is given:-
Postorder:- D,E,B,F,G,C,A
Inorder:- D,B,E,A,F,C,G
A.C
B.F
C.G
D.B
Q. Given Inorder and Preorder Traversal of a binary tree:
Preorder: L, G, D, C, M, H, T, K
Inorder: G, D, L, C, H, T, M, K
Construct the binary tree and find out the Postorder traversal of the Tree.
1) [Initilizes Child]
If Locleft == Null and Locright == Null, then
Set Child = Null
Else if Locleft ≠ Null, then
Set Child = Locleft
Else
Set Child = Locright
[End of if structure]
2. If PAR != NULL, then:
If LOC = LEFT[PAR], then:
Set LEFT[PAR] = CHILD.
Else: Set RIGHT[PAR] = CHILD.
Else: Set ROOT = CHILD.
3) Return
BST (Deletion)
CaseB(Info, Left, Right, Root, Loc, Par)
This procedure deletes node N at location Loc, where N has 2 children. Par=Null N is root
node; Suc inorder successor, ParSuc Parent of inorder successor
1) [Find Suc and ParSuc]
a) Set Ptr = Locright and Save = Loc
b) Repeat while Ptrleft ≠ Null
Set Save = Ptr and Ptr = Ptrleft
c) Set Suc = Ptr and ParSuc = Save
2) [Delete inorder successor using Procedure of CaseA]
Call CaseA (Info, Left, Right, Root, Suc, ParSuc)
• [Replace Node N by its inorder successor]
a) If Par ≠ Null, then
If Loc == Parleft, then Set Parleft = Suc
Else Set Parright = Suc
[End of if structure]
Else Set Root = Suc
[End of if structure]
– Set Sucleft = Locleft and Sucright = Locright
4) Return
Demonstration of Case B
Q. Create BST for the following list of elements:-
[89, 53, 95, 1, 9, 67, 72, 66, 75, 77, 18, 24, 35, 90, 38, 41, 49, 81, 27, 97]
And perform Delete operation
Algorithm: DEL( INFO, LEFT,RIGHT,ROOT,AVAIL,ITEM)
This procedure deletes ITEM from the tree.