Data Structure(KCS205)
Pranveer Singh Institute of
Technology, Kanpur
Binary Tree
Nature View of a Tree
leaves
branches
root
Computer Scientist’s View
root
leaves
branches
nodes
Tree Definition
• Arrays, Linked lists, Stack and Queue are linear data
structures.
– Here elements are arranged in linear fashion(i.e. one-
dimensional representation)
• Tree is a non linear data structure, used to represent
hierarchical relationship between elements.
– Here elements are arranged non-linear fashion(two-
dimensional representation)
Tree- to represent hierarchy relationship
among data
• A family hierarchy An algebraic expression
A /
B C -- +
H E A B C D
D
F Here each operators appear as internal
node and its corresponding operands form
its left and right child
G
Basic terminologies
• Node: Main component of any tree structure. It stores
the actual data and links to the other node.
• Parent: Parent is the immediate predecessor of a node.
• Child : Immediate successors of a node are called child.
• Root: Specially designated node which has no parent.
• Leaf: A node which is at the end and which does not
have any child. They are also called terminal node
Basic terminologies
• Link: Pointer to a node in the tree. Also called Edge. It is a
line drawn from a node N of tree to its successor.
• Path: Sequence of consecutive edges.
• Level: It is the rank of the hierarchy and the root node is in
level 0.
• Height: Maximum no. of edges that is possible in a path
starting from root node to a leaf node. It is also called
depth.
• Degree: Maximum no. of children that is possible for a
node is called degree of that node.
• Sibling: The nodes which have the same parent
• Leaf nodes are also called external nodes and non leaf
nodes are called internal nodes.
• If a node is at level l, then its child is at level (l+1) and
parent at level( l-1).
• Child node at left side is called left child and at right side
is called right child.
Example
• This tree has 7 nodes.
• A is the parent of B & C. A
• D is the left child and E is the
B C
right child of B.
• AB and AC are the D E F
two links or edges of node A.
• A is the root node. G
• D, E & G are leaf nodes.
• A is at level 0, B & C at level 1, D,E & F at level 2 ,G at level 3.
• Longest path is A-C-F-G. So, height of the tree=3.
• Degree of B=2, D(C)=1, D(F)=1
• B and C are siblings, D & E
• Find A
1. Height of the tree T B C D
2. Level(H), level(C), level(K)
3. Degree of T
G H I J
4. The longest path E F
5. Parent(M), Sibling(I)
and Child(B) K L M
Definition
• A tree T is defined as a finite set of one or more nodes
such that:
a) There is a specially designed node called the root,
b) Remaining nodes are partitioned in to n (n>0) disjoint
sets T1, T2,…, Tn , where each Ti is a tree. T1, T2,…, Tn are
called sub trees of the root.
A
B C D T3
T1
G H I J
E F
L M T2
K
M-ary trees
•• Definition
Definition..AArooted
rootedtree
treeisiscalled
calledananm-ary
m-arytree
treeifif
every
everyinternal
internalnode
nodehas
hasnonomore
morethan
thanm mchildren.
children.
The
Thetree
treeisiscalled
calledaafull
fullm-ary
m-arytree
treeififevery
everyinternal
internal
node
nodehas hasexactly
exactlym mchildren.
[Link]-ary
m-arytree
treewith
withm m==
22isiscalled
calledaabinary
binarytree.
tree.
4 internal nodes
All these 4 nodes have
<=3 children
So, m = 3
Binary Trees
• Special form of a tree.
• A binary tree T is defined as a finite set of nodes such
that:
a) T is empty( called the empty binary tree)
b) T contains a specially designed node called the root of
T and the remaining nodes of T form two disjoint
binary trees T1 & T2 which are called the left sub-tree &
the right sub-tree respectively.
• Any node N in a binary tree A
has either 0,1 or 2 successors. C
B
• A tree can never be empty,
but binary tree may be empty. D E F G
• A tree can have any no. of children, H
but in a binary tree, a node can have
I
at most two children.
Two special situations of binary trees are:
• Full binary tree & Complete binary tree.
• Full binary tree
– A binary tree is a full binary tree, if it contains maximum possible
no. of nodes in all levels.
Level Nodes
0 1
A
1 2
B C
2 4
D E F
G
3 8
H I J K L M N O
– The maximum no. of nodes in each level l is: 2 l
• Complete binary tree
– A binary tree is said to be complete binary tree, if all its level,
except possibly the last level, have the maximum no. of
possible nodes, and all the nodes in the last level appear as far
left as possible.
Level Nodes
A 0 1
1 2
B C
2 4
D E F
G
J K
3 5
H I L
Properties of binary tree
• In any binary tree, the maximum no. of nodes on level l is
2l , where l>=0.
• Maximum no. of nodes possible in a binary tree of height
h is 2h+1-1.
• Minimum no. of nodes possible in a binary tree of height
h is h+1.
• For strict binary tree, if n is the no. of nodes and e is the
no. of edges, then, n= e+1
Representation of binary tree
• Implicit & Explicit representation
• Implicit representation
– Sequential / Linear representation, using arrays.
• Explicit representation
– Linked list representation, using pointers.
Sequential representation
• This representation is static.
• Block of memory for an array is allocated, before storing
the actual tree.
• Once the memory is allocated, the size of the tree will be
fixed.
• Nodes are stored level by level, starting from the zero th
level.
• Root node is stored in the starting memory location, as
the first element of the array.
Sequential representation
• Consider a linear array TREE
Rules for storing elements in TREE are:
1. The root R of T is stored in location 1.
2. For any node with index i 1<i<=n:
1. PARENT(i)= i/2
For the node when i=1,there is no parent.
2. LCHILD(i)=2*i
If 2* i >n, then i has no left child
3. RCHILD(i)=2*i+1
If 2*i+1>n, then i has no right child.
Sequential representation: Example
45
22 77
11 30 90
15 25 88
1 2 3 4 5 6 7 8 9 10 11 12 13 14
45 22 77 11 30 90 15 25 88
Sequential representation: Example
+
- *
C /
A B
D E
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+ - * A B C / D E
Sequential representation- Advantages:
1. Any node can be accessed from any other node by
calculating the index.
2. Here, data are stored simply without any pointers to their
successor or predecessor.
3. Programming languages, where dynamic memory allocation
is not possible(like BASIC, FROTRAN), array representation is
only possible.
4. Inserting a new node and deletion of an existing node is
easy.
Sequential representation- Disadvantages:
1. Other than full binary trees, majority of the array entries
may be empty.
2. It allows only static representation. It is not possible to
enhance the tree structure, if the array structure is limited.
Draw the tree structure whose array representation is
given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A B . C . . . D . . . . . . . E
Linked list representation
• It consist of three parallel arrays DATA, LC and RC
DATA
LC RC
• Each node N of T will correspond to a location K such that:
– DATA[K] contains the data at the node N
– LC[K] contains the location of the left child of node N
– RC[K] contains the location of the right child of node N
Linked list representation
• It consist of three parallel arrays DATA, LC and RC
DATA
LC RC
• Each node N of T will correspond to a location K such that:
– DATA[K] contains the data at the node N
– LC[K] contains the location of the left child of node N
– RC[K] contains the location of the right child of node N
Linked list representation: Example
4
2 7
1 3 9
4
1 1 8
2 x 7
x 1 3 x 9 x
x 1 x x 1 x x 8 x
Linked list representation: Example
+
- *
B C /
A
+
D E
- *
x A x x B x x C x /
x D x x E x
Skew binary tree
• Consider a binary tree with n nodes.
• If the maximum height possible hmax=n, then it is called
skew binary tree.
5
5
(1). (2).
10 10
15
15