-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
1/67
PROBLEMS AND DIFFICU
GIVE US THE CHANCE
BECOME STRONGER A
BETTER.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
2/67
TREESTable of Contents1) Introduction to Trees
Tree TerminoloTrees Properties
2) Binary TreesComplete Binary Trees
Extended Binary Trees3) Memory Representation
Sequential
Linked4) Binary Tree Traversal
Pre-orderIn-orderPost-order
5) Traversal Algorithms using Stacks
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
3/67
President-CEO
ProductionManager Man
Shippin
Supervis
Personnel
Manager
Warehouse
Supervisor
Purchasing
Supervisor
HIERARCHICAL TREE STRUC TURE
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
4/67
A tree is a finite set of one or msuch that:
There is a specially designated n
Definition of Tree
.The remaining nodes are partitioneddisjoint sets T1, ..., Tn, where each of ta tree.
We call T1, ..., Tn the subtrees of the ro
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
5/67
Tree Terminology
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
6/67
Terminology
The degree of a node is the number ofof the node
The degree of A is 3; the degree of C is 1.
The node with degree 0 is a leaf or
.A node that has subtrees is the parentroots of the subtrees.
The roots of these subtrees are the chithe node.
Children of the same parent aresiblings.The ancestors of a node are all thealong the path from the root to the node.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
7/67
Store elements Hierarchically
The top element: root
,
has a parent
Each element has 0 or more
children
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
8/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
9/67
Ancestors of u
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
10/67
Descendants of u
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
11/67
Level Number
Each node in a binary tree T is assigNumber.
The root R of the tree T is assigned a level
one more than the level number of its paren
The nodes with the same level numberbelong to the same generation.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
12/67
A
HG
FE
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
13/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
14/67
Node B has height 3, Node B has De
Node E has height 2, Node E has D
Node K has height 1, Node K has De
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
15/67
Exercise
Number of Nodes Degree of a node(shown by green number) Leaf node(terminal) Non terminal nodes 3
Parent Children Sibling Ancestor Descendant
Level of a node Height(depth) of a tree
2 1 3
2 0 0 1 0
0 0 0
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
16/67
Exercise to do
A
B C
Property
Number of nodes
Height
Root Node
D
G
E F
IH
Leaves
Interior nodes
Number of levels
Ancestors of H
Descendants of BSiblings of E
Degree of node A
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
17/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
18/67
Binary Trees
ABinary Tree T is defined as a finite set of enodes , such that:
a)Tis empty (called null tree or empty tree) o
,and the remaining nodes of T form an o
disjoint binary treeT1(left subtree) andT2(
[This definition of the binary tree is recursive
Any node in Binary tree T has either 0,1 or 2A special class of trees: max degree for each no
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
19/67
Example
A
B
CE
IM
HL
DF GK
Left Subtree and Right Subtree ?
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
20/67
Difference between Binary tree and a Gen
A binary tree can be empty whereas a tr
empty.
Each element in binary tree has at most t
whereas each element in a tree can have an
sub trees
The sub trees of each element in a bin
ordered. That is we can distinguish betw
right sub trees. The sub trees in a tree are u
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
21/67
Various Binary TreesSkewed Binary Tree: If every node in a tree has only one childnode), then we call such treesSkew Trees. If every node has only
call them as Left skew trees. Similarly, if every node has only ri
call them right skew trees.
A
B
C
D
Skewed Tree
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
22/67
Complete Binary Tree
The tree T is said to be
complete if all its levels,
except possibly the last, have
e max mum num er opossible nodes, and if all the
nodes at the last level appear
as far left as possible.E
I
D
H
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
23/67
Extended Binary Tree
A binary tree T is said to be a 2-tree or an extended binary t
N has either 0 or 2 children.
The nodes with 2 children are called internal nodes.
The nodes with 0 children are called external nodes.
The external and internal nodes are distinguished diagramm
circles for internal nodes and squares for external nodes.
A
B
D
B C
G
DF
Fig: Binary Tree T Fig: Extended 2-tree
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
24/67
Maximum Number of Nodes in Bina
The maximum number of nodes onbinary tree is 2i, i>=0.
The maximum number of nodes in tree of depth kis 2k-1, k>=1.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
25/67
Full Binary Tree V/S Complete Bin
A full binary tree of depth k is a binary trehavin2k-1nodes,k>=0.
A binary tree !ith n nodes and "ompleteiffits nodes "orrespond to the nodfrom 1 tonin the full binary tree of depthk.
A
B C
GE
I
D
H
F
B
ED
JIH
Full binaClete binary tree
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
26/67
Complete Binary Tre
#f a "omplete binary tree !ithnnodes
(depth=log n + 1)
is represented se$uentially,
t en or any no e ! t n ex , 1%= %=nparent'i( is ati)2*ifi+=1. #fi=1,iis ahas no parent.
leftChild'i( is at 2i if 2i%=n. #f 2i>n,
left "hild.rightChild'i( is at 2i1 if 2i1%=nthenihas no riht "hild.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
27/67
Representing Binary Trees in M
Binary trees are represented using two w
Sequential Representation (using Single Array)
[ The main Requirement of any representation of T is th
direct access to the root R of T and given any node N ofdirect access to the children of N. ]
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
28/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
29/67
Advantages of Sequential Representation
%'
1. No need to store left and right pointers in the nodes2. Direct access to nodes: to get to node k, access A[k
TR
1 45
2 22
3 77
4 11
5 30
6
7 90
22 ((
11 30 )0
**2'1'
8
9 15
10 25
11
12
13
14 88
15
16
.
.
.
29
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
30/67
It can be seen that a sequential representatio
tree requires numbering of nodes; starting w
level 1, then on level 2 and so on. Th
numbered from left to right .
It is an ideal case for representation of a co
tree and in this case no space is wasted.
o er nary rees, mos o e space remaAs can be seen in the figure, we require 1
array even though the tree has only 9 nodes.
for successors of the terminal nodes are
would actually require 29 locations insteasequential representation is usually ineff
binary tree is complete or nearly complete
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
31/67
Sequential Representation is usually inefficient unless, the Binary trT is complete or nearly complete.
A
B
++
C
++
,1-
,2-
,3-
,%-
,'-
,.-
A
B
A
(1) waste space(2) insertion!e"etio
pro#"e$
++
++
D
++
/
E
(,*-
,)-
/
,1.-
E
C
DB
E
I
D
H
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
32/67
Linked Representati
Tree T will be maintained in memory by me
Representation which uses three parallel
LEFT and RIGHT, and a pointer variable ROO
1. INFO[K] contains the data at the node
. con a ns e oca on o ethe node N
3. RIGHT[K] contains the location of th
of node N.
[Where K is the location ]
le!t
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
33/67
Linked Representati
ointer variable //T "ontains thethe root of T.
#f the tree T ids empty, then //T
.#f any subtree of the tree is empt"orrespondin pointer !ill "onta
value.
"atale!t
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
34/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
35/67
ExerciseConstruct a Binary tree using Linked Repr
INFO LEFT
1 K 0
2 C 3
3 G 0
4 14
5 A 10
6 H 17
5
Root
Avail
7 L 0
8 9
9 4
10 B 18
11 19
12 F 0
13 E 12
14 15
15 1616 11
17 J 7
18 D 0
19 20
20 0
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
36/67
Binary Tree Travers
There are three standard !ays of tbinary tree T !ith root .
These three alorithms, "alled&
#norder
ostorder
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
37/67
Preorder
1. Process the root R.
2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
Inorder
1. Traverse the left subtree of R in inorder.
2. Process the root R.
3. Traverse the right subtree of R in inorder.
Postorder
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postoder.
3. Process the root R.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
38/67
Preorder
a
b c
a b c
1. Process the root R.2. Traverse the left subtree of R in preorder.
3. Traverse the right subtree of R in preorder.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
39/67
Preorder Example
a
b c
d e
g h i j
a b d g h e i c f j
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
40/67
Preorder of Expression Tr
+
e
*
/
+
a b
-
c d
Gives prefix form of expression !
/ * + a b - c d + e f
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
41/67
Inorder Example
a
b c
b a c
1. Traverse the left subtree of R in inorder.
2. Process the root R.3. Traverse the right subtree of R in inorder.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
42/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
43/67
Inorder of Expression Tre
+
e
*
/
+
a b
-
c d
Gives Infix form of expression !
ea + b * c d / +-
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
44/67
Postorder Example
a
b c
b c a
1. Traverse the left subtree of R in postorder.
2. Traverse the right subtree of R in postoder.
3. Process the root R.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
45/67
Postorder Example
a
b c
d e
g h i j
g h d i e b j f c a
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
46/67
Postorder of Expression T
+
e
*
/
Gives postfix form of expression!
a b + c d - * e f + /
+
a b
-
c d
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
47/67
Illustrations for Travers
Assume: visiting a node
is printing its label
Preorder:
3
85
1 3 5 4 6 7 8 9 10 11 12
Inorder:
4 5 6 3 1 8 7 9 11 10 12
Postorder:4 6 5 3 8 11 12 10 9 7 1
1
4 6
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
48/67
Illustrations for Traversals (C
Assume: visiting a node
is printing its data
Preorder: 15 8 2 6 3 7
15
8
11 10 12 14 20 27 22 30
Inorder: 2 3 6 7 8 10 11
12 14 15 20 22 27 30
Postorder: 3 7 6 2 10 1412 11 8 22 30 27 20 15
6
3 7
10 12
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
49/67
Arithmetic Expression Using Bina
E
#rer 4#re!i5
A
4
D
C
B
nrA 4 B in!i5 e
#t
A B 4 #t!i
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
50/67
Some Examples
preorder = ab a
b
a
inorder = ab b
a
a
postorder = ab b
a
b
level order = ab a
b
a
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
51/67
Binary Tree Constructi
Can you construct the binary tre
two traversal sequences?
Depends on which two sequence
given.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
52/67
Construct a Binary Tree from g
Inorder and Preorder Sequen
Inorder Sequence : D B E A F C
Preorder Sequence : A B D E C F
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
53/67
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 GSolution: The tree is drawn from the root as follow
(a) The root of tree is obtained by choosing the firpreorder. Thus F is the root of the proposed tre
b The left child of the tree is obtained as follows(a) Use the inorder traversal to find the nodes to the le
root node selected from preorder. All nodes to the this case F) in inorder form the left subtree of the rC K )
(b) All nodes to the right of root node (in this case F ) right subtree of the root (H D B G)
(c) Follow the above procedure again to find the subsetheir subtrees on left and right.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
54/67
F is the root Nodes on left subtree( left of F):E A C K (fr
Nodes on right subtree(right of F):H D B G(fr
The root of left subtree:
From preorder: A E K C , Thus the root of left subtree is A D H G B , Thus the root of right subtree is
Creating left subtree first:
From inorder: elements of left subtree of A are: E (root of
elements of right subtree of A are: C K (roo
As K is to the left of C in preorder
A
F
D
KE
C
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
55/67
Creating the right subtree of F
The root node is D From Inorder, the nodes on the left of D are: H (left ro
the nodes on the right of D are: B G (ri
Thus the tree is:
F
A D
E K HG
C B
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
56/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
57/67
Inorder and Preorder
Preorder = a b d g h e i c f j
a
gdhbei fjc
b is the next root; gdh are in the lsubtree; ei are in the right subtree
a
gdh
fjcb
ei
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
58/67
Inorder And Preorder
reorder = a b d h e i c f
a
gdh
fjcb
ei
d is the next root; g is in the lef
h is in the right subtree.a
g
fjcb
eid
h
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
59/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
60/67
Preorder Traversal (using stackAlgorithm 9: PREORD(INFO, LEFT, RIGHT, ROOT)
The algorithm does a preorder traversal of Binary tree T, applying aneach of its nodes. An array STACK is used to temporarily hold the a
1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2. Repeat Steps 3 to 5 while PTR != NULL:
3. Apply PROCESS to INFO[PTR].
4. [Right Child?]
If RIGHT PTR != NULL thenSet TOP := TOP+1, and STACK[TOP]:= RIGHT[PTR].
[end of If structure]
5. [Left Child?]
If LEFT[PTR]!= NULL then
Set PTR:= LEFT[PTR].
ElseSet PTR:= STACK[TOP] TOP:= TOP-1.
[End of If structure]
[End of step 2 Loop]
6. Exit.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
61/67
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
62/67
6. Proceed down the left most path rooted at PTR= K as foll
(vi) Process K. (There is no right child)
No other node is processed, since K has no left child.7. [Backtracking] Pop C from Stack, and set PTR := C. This l
STACK:
Since PTR NULL, return to Step (a) of the algorithm.
8. Proceed down the left most path rooted at PTR = C as follo
(vii) Process C and push its right child F onto STACK:
STACK:
F
Preorder Traversal Algorithm (C
(viii) Process E. (There is no right child)
9. [Backtracking] Pop F from STACK, and set PTR :=F, This
STACK:
10. Proceed down the left-most path rooted at PTR :=F as fo
(ix)Process F.
No other node is processed, since F has no left child.
11. [Backtracking] Pop the top element NULL from STACK,
NULL.
Since PTR=NULL, the algorithm is completed.
As seen from the steps 2, 4, 6, 8 and 10, the nodes are proces
B, D, G, H, K, C, E, F. This is required preorder traversal.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
63/67
Inorder Traversal (using stack)
Algorithm 9: INORD(INFO, LEFT, RIGHT, ROOT)The algorithm does a inorder traversal of Binary tree T, applying an op
each of its nodes. An array STACK is used to temporarily hold the add
1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2. Repeat while PTR != NULL:
a) Set TOP:=TOP+1, STACK[TOP]:=PTR.
b Set PTR:= LEFT[PTR].[End of loop]
3. Set PTR:=STACK[TOP], TOP:=TOP-1.
4. Repeat Steps 5 to 7 while PTR != NULL:
5. Apply PROCESS to INFO[PTR].
6. If RIGHT[PTR]!= NULL, then:
a) Set PTR:=RIGHT[PTR]
b) Go to Step 2.[End of If structure]
7. Set PTR:=STACK[TOP] and TOP:=TOP-1.
[End of step 4 Loop.]
8. Exit
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
64/67
Postorder Traversal (using stac
Algorithm 9: POSTORD(INFO, LEFT, RIGHT, ROOT)
The algorithm does a postorder traversal of Binary tree T, applying an
each of its nodes. An array STACK is used to temporarily hold the add
1. Set TOP:= 1, STACK[1]:= NULL and PTR := ROOT.
2. Repeat Step 3 to 5 while PTR!=NULL:
3. Set TOP := TOP+1, STACK[Top]:=PTR.
4. If RIGHT[PTR]!=NULL, then:
Set TOP := TOP+1, Stack[Top]:=-RIGHT[PTR].
End of If structure5. Set PTR:=LEFT[PTR]
[End of step 3 Loop]
6. Set PTR:=STACK[Top], TOP := TOP-1.
7. Repeat while PTR>0:
a)Apply PROCESS to INFO[PTR].
b)PTR:=STACK[Top], TOP := TOP-1.
[End of Loop]8. If PTR
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
65/67
Similar and Copy Tree
A
B
E
F
A
B
C D GH C D
Tree T1 Tree T3Tree T2
1) Tree T1,T2 and T3 is said as Similar Tree because Str
trees is same.2) Tree T1 and T3 is said as Copy Tree because along w
content(data) part of both trees are same.3) Tree T1 and Tree T4 are neither similar nor Copy tree.
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
66/67
Exercise
Q1: The post order traversal of a binary tree is 452631. Find outorder traversala. 126345b. 142653
c. 124536d. 124356
Q2: The inorder and preorder traversals of a binary tree are d h and a b d h e c f g I j respectively. The Post order traversal is
a) h d e b f i j g c ab) d h e b i j g f c ac) e h d b f i j g c a
D) h e d b f i j g a c
-
8/10/2019 18569_TREES [Read-Only] [Compatibility Mode].pdf
67/67