Week 07 Lecture
Week 07 Lecture
AND ALGORITHMS
Week-07
B C
G H
D E
J K
F
L
1. This TREE consists of 11 nodes represented by the letters A
through L.
2. The root of TREE is the node A at the top of the diagram.
3. B is the left successor of the node A.
4. C is the right successor of the node A.
BINARY TREE… A
B C
G H
D E
J K
F
1. The nodes A, B, C and H have two successors.
L
2. The nodes E and J have only one successor.
3. The nodes D, F, G, L and K have no successor.
4. The nodes with no successor are called terminal nodes.
TERMINOLOGY…
Suppose A is a root node in a tree with left successor B and right successor C
A is called the PARENT of B and C.
B is called the LEFT CHILD of A.
C is called the RIGHT CHILD of A.
B and C are said to be SIBLINGS.
Nodes are connected by lines called EDGES.
Sequence of consecutive edges is called a PATH.
A terminal node is called a LEAF.
O and P are Leaf Nodes
F and J are ANCESTORS of O.
A forest is a set of DISJOINT trees. Removing the root of a tree produces a forest
Height of Tree is Maximum Level of its node Leveln+1
B, C, D, E, F, G, H, I, J, K, M,O and P are DESCENDANT of A.
Binary Tree From Algebraic Expression
Consider any algebraic expression E involving only Binary Operations
E=(a–b) / ((c*d)+e)
- +
a b * e
c d
Representation of Tree as an Array
S
1
Y
S
A 2 A
3 Y
W B
R T 4 R
5 T
6 W
7 B
left Child = 2n
right Child = 2n+1
A Y 2 B 0 0
3 R 0 0
4 8
W B
R T AVAIL 5 S 1 9
4 6 W 0 0
7 T 0 0
8 10
9 Y 6 2
10 0
TRAVERSING OF BINARY TREES
There are three standard ways of traversing a binary tree
PREORDER
Process the root R
Traverse the left sub tree of R in preorder Node-Left-Right (NLR)
Traverse the right sub tree of R in preorder Traversal
INORDER
Traverse the left sub tree of R in ignored Left-Node-Right (LNR)
Process the root R
Traverse the right sub tree of R in ignored Traversal
POSTORDER
Traverse the left sub tree of R in postorder Left-Right-Node (LRN)
Traverse the right sub tree of R in postorder Traversal
Process the root R
TRAVERSING OF BINARY TREES (Preorder)
NLR A
NLR B C NLR
D E F G
Preorder NLR: A, B, D, E, C, F, G
TRAVERSING OF BINARY TREES (Inorder)
LNR A
LNR B C LNR
D E F G
Inorder LNR: D, B, E, A, F, C, G
TRAVERSING OF BINARY TREES (Postorder)
LRN A
LRN B C LRN
D E F G
Postorder LRN: D, E, B, F, G, C, A
TRAVERSING OF BINARY TREE USING LINKED LIST
100
200 A 500
500
200
600 C 700
300 B 400
Preorder NLR: A, B, D, E, C, F, G
Inorder LNR: D, B, E, A, F, C, G
Postorder LRN: D, E, B, F, G, C, A
THANKS!
Any questions?
16