Professional Documents
Culture Documents
TREES
➢ A binary tree is a hierarchical data structure in
which each node has at most two children generally
referred as left child and right child.
➢ The Binary tree means that the node can have
maximum two children.
➢ Binary name itself suggests that 'two'; therefore,
each node can have either 0, 1 or 2 children.
Leaf nodes: H, I, J,F,G
Non leaf nodes:
A,B,C,D,E,
➢ Path − Path refers to the sequence of nodes along
the edges of a tree.
➢ Root − The node at the top of the tree is called
root. There is only one root per tree and one path
from the root node to any node.
➢ Parent − Any node except the root node has one
edge upward to a node called parent.
➢ Child − The node below a given node connected
by its edge downward is called its child node.
➢ Leaf − The node which does not have any child
node is called the leaf node.
➢ Traversing − Traversing means passing through
nodes in a specific order.
➢ Levels − Level of a node represents the
generation of a node. If the root node is at level 0,
then its next child node is at level 1, its
grandchild is at level 2, and so on.
➢ Sub-Tree:Sub-tree represents the descendants of
a node.(Figure : left & right sub-tree)
➢ Ancestor & Descendant nodes: A is the ancestor of
E & H as a descendant of C.
Binary Tree & their Classifications
➢ Level of a node in a binary tree can be defined :
Function Purpose
Function Purpose
3.Insert 60 5. Insert 90
As 60 > 50, so insert 60 to As 90 > 50, so insert 90 to the
the right of 50. right of 50.
As 60 < 70, so insert 60 to As 90 > 70, so insert 90 to the
the left of 70. right of 70.
6. Insert 10
As 10 < 50, so insert 10
to the left of 50.
As 10 < 20, so insert 10
to the left of 20.
8. Insert 100
As 100 > 50, so insert 100 to
the right of 50.
As 100 > 70, so insert 100 to
7.Insert 40 the right of 70.
As 40 < 50, so insert 40 As 100 > 90, so insert 100 to
to the left of 50. the right of 90.
As 40 > 20, so insert 40
to the right of 20.
Problems:
1. 50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
2. 50,26,74,48,64,74,78,12,48,64,25
3. 61,5,78,26,72,81,5,12
Second Phase: Traversing a binary tree
➢ Often we wish to process a binary tree by
“visiting” each of its nodes, each time performing
a specific action such as printing the contents of
the node.
➢ Any process for visiting all of the nodes in some
order is called a traversal.
2. In-order
3. Post-order
Pre-order Traversal (Depth-first)(RtLR)
1. Visit the root node.
2. Then visit the nodes in the left sub-tree in preorder
3. Finally, visit the nodes in the right sub-tree in
preorder
In-order Traversal(LRtR)
1. Visit the nodes in the left sub-tree in in-order
2. Then Visit the root node.
3. Finally, visit the nodes in the right sub-tree in in-
order
Post-order Traversal(Breadth-first)(LRRt)
1. Visit the nodes in the left sub-tree in post-order
2. Then, visit the nodes in the right sub-tree in post-
order
3. Finally Visit the root node.
Inorder Traversal: 7 9 4 2 5 1 3 6 8
Preorder Traversal: 1 2 4 7 9 5 3 6 8
Postorder Traversal: 9 7 4 5 2 8 6 3 1
Pre-order Traversal: 1 2 4 7 9 5 3 6 8
In-order Traversal: 7 9 4 2 5 1 3 6 8
Post-order Traversal: 9 7 4 5 2 8 6 3 1
Pre-order: A B D E C F G
In-order: D B E A F C G
Post-order: D E B F G C A
Problems:
Applications of binary Trees
1. Sorting in ascending order
➢ A binary search tree is simply an ordered or sorted
binary tree such that the value in the left child is less than
the value in the parent node.
➢ At the same time, the values in the right node are greater
than the value in the parent node.
➢ To complete a sorting procedure, the items to be sorted
are first inserted into a binary search tree.
➢ To retrieve the sorted items, the tree is traversed
using in-order traversal.
2. Conversion of infix and other notations
➢ Consider a mathematical expression that is expressed in
binary tree
➢ The tree can be constructed using the order of
precedence and asssciativity laws of arithmetic
operators in ‘C’
Operators Symbols
Parenthesis { }, ( ), [ ]
Exponential notation ^
Multiplication and Division *, /
Addition and Subtraction +, -
a + (b * c) + d * (e + f)
2. a+b*c
3. a+b*c^d-e/f
4. (a+b)*(e+d)
5. [x/(y*z)+a]/[(p/q)*s+c]
6. ((a+b)*c)-d
7. [5+(6/3)]*[6/3-7]
8. (8-5)*((4+2)/3)