You are on page 1of 25

BINARY

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 :

➢ Root node is at level 0

➢ Level of any node in the tree is one more than of


its father
➢ Node B & C are at level 1

➢ Node D,E, F at level 2

➢ Nodes G,H,I are at level 3

➢ Depth of a binary tree is the maximum level of


any leaf in the tree.
➢ Depth of this tree is 3
Strictly Binary tree
➢ In a binary tree, every node can have a maximum of two
children.
➢ But in strictly binary tree, every node should have exactly
two children or none.
➢ That means every internal node must have exactly two
children.
➢ A binary tree in which every node has either two or
zero number of children is called Strictly Binary
Tree
➢ Strictly binary tree is also called as Full Binary
Tree or Proper Binary Tree or 2-Tree
Complete Binary Tree
 Every node can have a maximum of two children.

 But in strictly binary tree, every node should have exactly


two children or none and in complete binary tree all the
nodes must have exactly two children and at every level of
complete binary tree there must be 2level number of nodes.
 Level 2 there must be 22 = 4 nodes and at level 3 there must
be 23 = 8 nodes.
 A binary tree in which every internal node has exactly
two children and all leaf nodes are at same level is
called Complete Binary Tree.
 Complete binary tree is also called as Perfect Binary Tree
Almost complete binary tree
➢ An almost complete binary tree is a special kind of
binary tree where insertion takes place level by
level and from left to right order at each level and
the last level is not filled fully always.
➢ It also contains nodes at each level except the last level.

 The main point here is that if we want to insert any node


at the lowest level of the tree, it should be inserted from
the left. All the internal nodes should be completely
filled.
 An almost complete tree is also a complete binary
tree
Operations on binary trees
➢ Basic operations on binary trees are simply the
operations on its nodes.
➢ Let p be a pointer to a node(p)

Function Purpose

info(p) Returns the contents of the node(p)

left(p) Returns a pointer to the left child of node(p); returns a


NULL pointer if node(p) has no left child

right(p) Returns a pointer to the right child of node(p); returns a


NULL pointer if node(p) has no right child

father(p) Returns a pointer to the father node of node(p)

brother(p) Returns a pointer to the brother node of node(p)


Functions of construction of binary trees

Function Purpose

maketree(x) Creates a new binary tree consisting of a


single node with information x and returns
a pointer to that node
setleft(p,x) Accept a pointer p to a binary node with no
left child; creates a new left child of
node(p) and places x in the created node
setright(p,x) Accept a pointer p to a binary node with no
right child; creates a new right child of
node(p) and places x in the created node
Construction and traversal of Binary trees
Two phases
1. Construction of binary trees

2. Traversing the nodes of binary trees in some


order
First Phase: Construction of binary trees
Sequence of numbers-50, 70, 60, 20, 90, 10, 40,
100
✓ When elements are given in a sequence,

➢ Always consider the first element as the root node.

➢ Consider the given elements and insert them in the


binary tree one by one.
1.Insert 50 4. Insert 20
2. Insert 70  As 20 < 50, so insert 20 to the

 As 70 > 50, so insert 70 to left of 50.


the right of 50.

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.

➢ Three traversal rules


1. Pre-order

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)

Infix expression (a+(b*c))+(d*(e + f))

Postfix Expression abc*+def +*+

Prefix Expression ++a*bc*d+ef


Problems
1. a+b

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)

You might also like