Lecture-13 & 14 (Binary-Tree)
Lecture-13 & 14 (Binary-Tree)
B C D E
Level 2
F G H I J K Level 3
L Level 4
Basic terminologies
10
Tree Terminology (continued)
• Node: stores the actual data and links
to other nodes
• Parent: immediate predecessor of a node
• Root: specially designated node which
has no parent
• Child:immediate successor of a node.
Tree Terminology(continued)
B C D E Level 2
F G H I J K Level 3
• List Representation
B C D E
F G H I J K
L
(b) Linked list representation of the tree
Binary Trees
A binary tree T is defined as a finite set of
elements called nodes such that
20
Binary Trees
B C Level 2
D E F G Level 3
Important observations regarding binary trees:
1 A
2 B C
3
D E 6 F
4 5 7 G
A binary tree with n’ nodes and height h is complete if
its nodes correspond to the nodes which are numbered 1
to n (n’n) in a full binary tree of height h.
2 B C 3 h = log2 (n +1)
4 D
5 E 6 F
A complete binary tree obeys the following
properties with regard to its node numbering:
a m
b n
c o
30
Representation Of Binary Trees
Array Representation
1 Sequential representation of a
a
2 tree with depth d will require
b
an array with approx 2d + 1
4 5
c d
e f
10
11
1 2 3 4 5 6 7 8 9 10 11
a b c d e f
Linked representation
T
LCHILD DATA RCHILD
b
c d
e f
• Observation regarding the linked representation
of Binary Tree
36
In-order Traversing
In this traversal method, the left subtree is visited first, then the root and later the right
sub-tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.
40
Example: For Given Inorder and Preorder
Inorder: D B H E A I F J CG
Preorder: A B D E H C F I J G
Now root is A
Left subtree: D B H E
Right subtree: I F J C G
continues.
A
In:D B H E IFJCG
Pre:B D E H CFIJG
D HE IFJ G
EH FIJ
I J
H
Example: For Given Inorder and Postorder
In:n1,n2,n3,n4,n5 n7,n8,n9
Post:n1,n3,n5,n4,n2 n8,n7,n9
n1 n3,n4,n5 n7,n8
n3,n5,n4 n8,n7
n3 n5 n8
Inorder And Preorder
inorder = g dhbeiaf jc
preorder = abdgheicf j
Scan the preorder left to right using the inorder to
separate left and right subtrees.
a is the root of the tree; gdhbei are in the left
subtree; fjc are in the right subtree.
gdhbei fjc
Inorder And Preorder
a
gdhbei fjc
preorder = a b d g h e i c f j
b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a
b fjc
gdh ei
Inorder And Preorder
a
b fjc
gdh ei
preorder = a b d g h e i c f j
d is the next root; g is in the left
subtree; h is in the right subtree.
a
b fjc
d ei
g h
Inorder And Postorder
Scan postorder from right to left using inorder to
separate left and right subtrees.
inorder = g d h b e i a f j c
postorder = g h d i e b j f c a
Tree root is a; gdhbei are in left subtree; fjc are in
right subtree.
Traversal Algorithm Using Stack
Assumption
Binary Tree is represented by
[5] [Backtracking] Pop the top element K from STACK, and set
PTR = K
STACK: φ, C
50
[6] Proceed down the left-most path rooted at PTR = K as
follows
(vi) Process K. (There is no right child)
[No other node is processed, since K has no left child] [7]
[Backtracking] Pop the top element C from STACK,
and set PTR = C
STACK: φ
[8] Proceed down the left-most path rooted at PTR = C
as follows
(vii) Process C and push its right child F onto STACK.
STACK: φ, F
(viii) Process E (There is no right child)
[9] [Backtracking] Pop the top element F
from STACK, and set PTR = F
STACK: φ
[10] Proceed down the left-most path
rooted at PTR = F as follows
(ix) Process F. (There is no right child)
[No other node is processed, since F has
no left child]
[11] [Backtracking] Pop the top element
NULL from STACK, and set PTR = NULL
In-order Traversal
[1] [Push NULL onto STACK and initialize PTR]
Set TOP =1, STACK[1] = NULL, PTR = ROOT
[2] Repeat while PTR != NULL
[Pushes the Left-most path onto STACK]
(a) Set TOP = TOP + 1, STACK[TOP] = PTR
(b) Set PTR = PTR -> LEFT
[3] Set PTR = STACK[TOP], TOP = TOP -1
[Pops node from STACK]
In-order Traversal
[4] Repeat Steps 5 to 7 while PTR!=
NULL: [Backtracking]
[5] Apply PROCESS to PTR->INFO
[6] [Right Child ?] If PTR->RIGHT NULL then
(a) Set PTR = PTR->RIGHT
(b) Go to Step 2
[7] Set PTR = STACK[TOP], TOP = TOP -1
[8] Exit
In-Order Traversal
A
B C
D E
G H
K L M
[1] Initially Push NULL onto STACK
STACK = φ
Set PTR = A , the root of T
[2] Proceed down the left-most path rooted at PTR = A,
pushing the nodes A, B, D, G and K onto
STACK:
STACK = φ, A, B, D, G, K
[3] [Backtracking] The nodes K, G and D are
popped and processed
STACK = φ, A, B
Set PTR = H [Right Child of D]
[4] Proceed down the left-most path rooted at PTR =
H, pushing the nodes H and L onto STACK:
STACK = φ, A, B, H, L
[5] [Backtracking] Nodes L and H are
popped and processed
STACK = φ, A, B
Set PTR = M, the Right child of H
[6] Proceed down the left-most path rooted at
PTR = M, pushing node M onto STACK
STACK = φ, A, B, M
[7] [Backtracking] Nodes M, B and A are popped
and processed
STACK = φ
Set PTR = C, the Right child of A
[8] Proceed down the left-most path rooted at PTR =
C, pushing node C and E onto STACK
STACK = φ, C, E
[9] [Backtracking] Nodes E and C are popped
and processed.