Tree 10 Oct 2023-1
Tree 10 Oct 2023-1
A tree is recursively defined as a set of one or more nodes where one node is
designated as the root of the tree and all the remaining nodes can be partitioned
into non-empty sets each of which is a sub-tree of the root.
Figure shows a tree where node A is the root node; nodes B, C, and D are
children of the root node and form sub-trees of the tree rooted at node A
Root node : The root node R is the topmost node in the tree.
If R = NULL, then it means the tree is empty.
Sub-trees : If the root node R is not NULL, then the trees T1 , T2 ,
and T3 are called the sub-trees of R.
Leaf node : A node that has no children is called the leaf node or
the terminal node.
Path : A sequence of consecutive edges is called a path. For
example, in Fig. the path from the root node A to
node I is given as: A, D, and I.
Ancestor node : An ancestor of a node is any predecessor node on the
path from root to that node. The root node does not have
any ancestors. In the tree given in Fig. nodes A, C, and G
are the ancestors of node K
Descendant node : A descendant node is any successor node on any path
from the node to a leaf node. Leaf nodes do not have any
descendants. In the tree given in Fig. nodes C, G, J, and K
are the descendants of node A.
Figure shows a binary tree. In the figure, R is the root node and
the two trees T1 and T2 are called the left and right sub-trees of
R. T1 is said to be the left successor of R. Likewise, T2 is called
the right successor of R. Note that the left sub-tree of the root
node consists of the nodes: 2, 4, 5, 8, and 9. Similarly, the right
sub-tree of the root node consists of nodes: 3, 6, 7, 10, 11, and
12. In the tree, root node 1 has two successors: 2 and 3. Node 2
has two successor nodes: 4 and 5. Node 4 has two successors: 8
and 9. Node 5 has no successor. Node 3 has two successor
nodes: 6 and 7. Node 6 has two successors: 10 and 11. Finally,
node 7 has only one successor: 12. A binary tree is recursive by
definition as every node in the tree contains a left sub-tree and a
right sub-tree. Even the terminal nodes contain an empty left
sub-tree and an empty right sub-tree. Look at Fig. nodes 5, 8, 9,
10, 11, and 12 have no successors and thus said to have empty
sub-trees
Parent If N is any node in T that has
left successor S1 and right
successor S2, then N is called
the parent of S1 and S2.
Correspondingly, S1 and S2
are called the left child and
the right child of N. Every
node other than the root
node has a parent.
Level number
Every node in
the binary tree is
assigned a level
number (refer Fig.
The root node is
defined to be at level
0. The left and the
right child of the root
node have a level
number 1. Similarly,
every node is at one
level higher than its parents. So all child nodes are defined to
have level number as parent's level number + 1.
Degree of a node It is equal to the number of children that a node has. The degree of a leaf
node is zero. For example, in the tree, degree of node 4 is 2, degree of node 5 is
zero and degree of node 7 is 1. Sibling All nodes that are at the same level and
share the same parent are called siblings (brothers). For example, nodes 2 and 3;
nodes 4 and 5; nodes 6 and 7; nodes 8 and 9; and nodes 10 and 11 are siblings
Copies Two binary trees T and T¢ are said to be copies if they have similar structure and if they
have same content at the corresponding nodes. Figure 9.6 shows that T¢ is a copy
of T.
Edge It is the line connecting a node N to any of its successors. A binary tree of n nodes has
exactly n – 1 edges because every node except the root node is connected to its
parent via an edge.
Path A sequence of consecutive edges. For example, in Fig. 9.4, the path from the root node to
the node 8 is given as: 1, 2, 4, and 8.
Depth The depth of a node N is given as the length of the path from the root R to the node N.
The depth of the root node is zero.
Height of a tree It is the total number of nodes on the path from the root node to the
deepest node in the tree. A tree with only a root node has a height of 1. A binary
tree of height h has at least h nodes and at most 2h – 1 nodes. This is because
every level will have at least one node and can have at most 2 nodes. So, if every
level has two nodes then a tree with height h will have at the most 2h – 1 nodes as
at level 0, there is only one element called the root. The height of a binary tree
with n nodes is at least log2 (n+1) and at most n.
In-degree/out-degree of a node It is the number of edges arriving at a node. The root node
is the only node that has an in-degree equal to zero. Similarly, out-degree of a
node is the number of edges leaving that node. Binary trees are commonly used to
implement binary search trees, expression trees, tournament trees, and binary
heaps
Complete Binary Trees A complete binary tree is a binary tree that satisfies two
properties. First, in a complete binary tree, every level, except possibly the last, is
completely filled. Second, all nodes appear as far left as possible. In a complete
binary tree Tn , there are exactly n nodes and level r of T can have at most 2r
nodes. Figure 9.7 shows a
complete binary tree.
Note that in Fig. 9.7, level
0 has 20 = 1 node, level 1
has 21 = 2 nodes, level 2
has 22 = 4 nodes, level 3
has 6 nodes which is less
than the maximum of 23 =
8 nodes. In Fig. 9.7, tree
T13 has exactly 13 nodes.
They have been purposely
labelled from 1 to 13, so
that it is easy for the
reader to find the parent
node, the right child node,
and the left child node of
the given node. The formula can be given as—if K is a parent node, then its left
child can be calculated as 2 × K and its right child can be calculated as 2 × K + 1. For
example, the children of the node 4 are 8 (2 × 4) and 9 (2 × 4 + 1). Similarly, the
parent of the node K can be calculated as | K/2 |. Given the node 4, its parent can
be calculated as | 4/2 | = 2. The height of a tree Tn having exactly n nodes is given
as: Hn = | log2 (n + 1)
Extended Binary Trees A binary tree T is said to be an extended binary tree (or a 2-
tree) if each
node in the tree
has either no
child or exactly
two children.
Figure 9.8
shows how an
ordinary binary
tree is
converted into
an extended
binary tree.
Exp = (a – b) + (c * d) This
expression can be represented
using a binary tree as shown in
Fig. 9.13
Expression for the above binary tree is [{(a/b) + (c*d)} ^ {(f % g)/(h – i)}
IN-order Traversal :
Q, E, F, R, D, H, B, A, I, J, K, C, L, P
Post-order Traversal
Q, R, F, E, H, D, B, K, J, I, P, L, C, A
Pre-order Traversal:
36,25,20,10,1,15,22,32,48,43,56,50,60,58,75
Example 5
Pre-order Traversal:
40, 30, 25, 15, 28, 35, 50, 45, 60, 55, 70
IN-order Traversal :
15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70
Post-order Traversal :
15, 28, 25, 35, 30, 45, 55, 70, 60, 50, 40
Example 6
Pre-order Traversal:
50, 17, 12, 9, 14, 23, 19, 72, 54,67,76,
IN-order Traversal :
9, 12, 14, 17, 23, 19, 50, 54 ,67, 72, 76
Post-order Traversal :
9, 14, 12, 19, 23, 17, 67, 54, 76, 72, 50
Example 7
Pre-order Traversal:
1, 2, 5, 6, 3, 8, 7
IN-order Traversal :
5, 2, 6, 1, 8, 3, 7
Post-order Traversal :
5, 6, 2, 8, 7, 3, 1
Example 8
Pre-order Traversal:
100, 20, 10, 30, 200, 150, 300
IN-order Traversal :
10, 20, 23, 100, 150, 200, 300
Post-order Traversal :
10, 30, 20, 150, 300, 200, 100
Example 9
Pre-order Traversal:
IN-order Traversal :
10,15, 20, 23, 25, 30, 35, 39, 42 Post-
order Traversal :
15 , 10 , 23 , 25, 20, 35, 42, 39, 30
Example 10
Pre-order
Traversal:
A, B, D E, C, F, G
IN-order Traversal :
D, B, E, A, F, C, G
Post-order Traversal :
15 , 10 , 23 , 25, 20, 35, 42, 39, 30
Example 11
Pre-order Traversal:
A, B, C, G, D, E, F
IN-order Traversal :
G, C, B, D, A F, E
Post-order Traversal :
G, C D, B, F, E, A
Example 12
Pre-order Traversal:
A, B, C, E, D, F, G, H, J, I, K
IN-order Traversal :
E, C, B, D, A, F, J, H, G , I, K
Post-order Traversal :
E, C, D, B, J, H, K, I, G, F, A
Example 13
Example 14
Example 15
Example 16
Example 17
Example 18
Example 19
Example 20
Example 21
Example 22
Example 23
Example 24
Example 25
Example 26 27
Stepwise construction of Binary search tree for following elements: 30,100,90,15,2,25,36,72,78,10 is
as follows: