bt (2) (1)
bt (2) (1)
B C
D E F
G H I
Binary Tree
root
B C
D E F
G H I
B C
D E F
Left subtree G H I
Right subtree
Binary Tree
• Recursive definition
A
B C
root
D E F
G H I
Left subtree
Binary Tree
• Recursive definition
A
B C
D E F
root
G H I
Binary Tree
• Recursive definition
A
root
B C
D E F
G H I
Right subtree
Binary Tree
• Recursive definition
A
B C
root
D E F
G H I
B C
D E F
G H I
Not a Tree
• Structures that are not trees.
A
B C
D E F
G H I
Not a Tree
• Structures that are not trees.
A
B C
D E F
G H I
Binary Tree: Terminology
parent
A
D E F
G H I
B C
D E J F
G K H I
Level of a Binary Tree Node
• The level of a node in a binary tree is
defined as follows:
Root has level 0,
Level of any other node is one more than the
level its parent (father).
• The depth of a binary tree is the maximum
level of any leaf in the tree.
Level of a Binary Tree Node
A 0 Level 0
B 1 C 1 Level 1
D 2 E 2 F 2 Level 2
G 3 H 3 I 3 Level 3
Complete Binary Tree
• A complete binary tree of depth d is the strictly
binary all of whose leaves are at level d.
0
A
B 1 C 1
D 2 E 2 F 2 G 2
H 3 I J 3 K L 3 M 3 N 3 O 3
Complete Binary Tree
A Level 0: 20 nodes
B C Level 1: 21 nodes
D E F G Level 2: 22 nodes
H I J K L M N O Level 3: 23 nodes
Complete Binary Tree
• At level k, there are 2k nodes.
• Total number of nodes in the tree of depth
d:
d
20+ 21+ 22 + ………. + 2d = 2j = 2d+1 – 1
j=0
n = 2d+1 – 1
or log2(n+1) = d+1
or d = log2(n+1) – 1
• I.e., the depth of the complete binary tree built
using ‘n’ nodes will be log2(n+1) – 1.
• For example, for n=1,000,000, log2(1000001) is
less than 20; the tree would be 20 levels deep.
• The significance of this shallowness will become
evident later.
Operations on Binary Tree
• There are a number of operations that can
be defined for a binary tree.
• If p is pointing to a node in an existing tree
then
left(p) returns pointer to the left subtree
right(p) returns pointer to right subtree
parent(p) returns the father of p
brother(p) returns brother of p.
info(p) returns content of the node.