You are on page 1of 21

Today topics…………….

 Binary Tree
 Binary Tree Properties
 Binary Tree Traversal Methods
 Binary Tree Representation
Binary Tree
 A binary tree is a finite set of nodes that is
either empty or consists of a root and two
disjoint binary trees called the left subtree
and the right subtree.
 A nonempty binary tree has a root element.
 The remaining elements (if any) are partitioned
into two binary trees.
 These are called the left and right subtrees of
the binary tree.
Differences Between A Tree & A Binary Tree

 No node in a binary tree may have a


degree more than 2, whereas there is
no limit on the degree of a node in a
tree.
 A binary tree may be empty; a tree
cannot be empty.
 In a binary tree, we distinguish
between the order of the children; in a
tree we do not.
Differences Between A Tree & A Binary Tree
 Since the first binary tree has an empty
right subtree, while the second has an
empty left subtree
a a

b b

• View as Tree however they are the same .


Binary Tree Form
(a + b) * (c – d) / (e + f)
//

* +
e f
+ -
a b c d
Properties of Binary Tree
• Before Examining data representation
for binary tree, let us make some
observation about BT,
• In Particular, we want to determine the
maximum number of nodes in a binary
tree of depth k and the relationship
between the number of leaf nodes and
the number of degree-two nodes in
binary tree
Minimum Number Of Nodes
 Minimum number of nodes in a binary
tree whose height is h.
 At least one node at each of first h
levels.

minimum number of
nodes is h
Maximum Number of Nodes in BT
The maximum number of nodes on level i of a
binary tree is 2i-1, i>=1.

The maximum nubmer of nodes in a binary


tree of depth k is 2k-1, k>=1.
Maximum Number Of Nodes
All possible nodes at first k levels are present.

Maximum number of nodes


=k 1 + 2 + 4 + 8 + … + 2k-1
2  2 1
i 1 k

i 1
Relations between Number of
Leaf Nodes and Nodes of Degree 2

 For any nonempty binary tree, T, if n0 is the


number of leaf nodes and n2 the number of nodes
of degree 2, then n0=n2+1

B C

D E F G

H I
Complete binary tree
Full Binary Tree
A full binary tree of a given height h has 2h – 1
nodes.

Height 4 full binary tree.


Numbering Nodes In A Full Binary
Tree
 Number the nodes 1 through 2h – 1.
 Number by levels from top to bottom.
 Within a level number from left to right.
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

Parent of node i is node i / 2, unless i = 1.


Node 1 is the root and has no parent.
Complete Binary Tree With n Nodes

 Start with a full binary tree that has at


least n nodes.
 Number the nodes as described earlier.
 The binary tree defined by the nodes
numbered 1 through n is the unique n
node complete binary tree.
Example
1

2 3

4 5 6 7
8 9 10 11 12 13 14 15

Complete binary tree with 10 nodes.


Full BT VS Complete BT
 A full binary tree of depth k is a binary tree of
depth k having 2 -1 nodes, k>=0.
 A binary tree with n nodes and depth k is
complete iff its nodes correspond to the nodes
numbered from 1 to n in the full binary tree of
depth k.
A A

B C B C

D E F G D E F G

H I J K L M N O
H I

Complete binary tree Full binary tree of depth 4


Binary Tree Representation

Array representation.
Linked representation.
Array Representation
 Number the nodes using the numbering
scheme for a full binary tree. The node that
is numbered i is stored in tree[i].
a1

2 3
b c

4 5 6 7
d e f g
8 9 10
h i j

tree[] a b c d e f g h i j
0 5 10
Right-Skewed Binary Tree
a1
b 3
7
c
15
d

tree[] a - b - - - c - - - - - - - d
0 5 10 15
Array Representation
If a complete binary tree with n nodes is represented
sequentially, then for any node with index i, 1<i<n,
we have

1. Parent(i) is at [i/2] if i != 1 , If i == 1 is at the root


and has no parent
2. LeftChild(i) is at 2i if 2i<=n, then i has no child.
3. RightChild(i) is at 2i + 1 if 2i+1<=n, then i has no
right child

You might also like