Binary Tree 1
Binary Tree 1
leaves
branches
root
Computer Scientist’s View
root
leaves
branches
nodes
Binary Tree
G H
B C
D E F G
L M N O
H I J K
B and C are siblings.
J is a descendent of B.
B C
D E F G
N O
H I J K L M
Binary Tree
Structures that are not binary trees
B c
E F
D
I
G H
Binary Tree
Structures that are not binary trees
B c
E F
D
G
Binary Tree
Structures that are not binary trees
B c
E F
D
H I
Size and depth
a The size of a binary tree is the
number of nodes in it
This tree has size 12
b c
The depth of a node is its
distance from the root
d e f a is at depth zero
e is at depth 2
g h i j k The height of a binary tree is
the depth of its deepest node
l This tree has height 4
What is the max #nodes
at some level/depth l?
l
2l
20
21
22
23
Height of Binary Tree
Maximum height of tree for N nodes:
Hmax = N - 1
The minimum height of the tree :
• Hmin = log2N
• Nmax = 2H+1 - 1
Strictly Binary Tree
Strictly binary trees:
If every nonleaf node in a binary tree has nonempty left
and right subtrees, the tree is called a strictly binary
tree. A strictly binary tree with n leaves always
contains 2n -1 nodes.
A
B C
D E
F G
Strictly Binary tree
Structure that is not a strictly binary tree:
because nodes C and E have one son each.
A
B
c
D E
F
G
H I
A complete binary tree
Complete binary tree of height d is the strictly binary tree
all of whose leaves are at level d.
The total number of nodes = the sum of the number of
nodes at each level between 0 and 2d d + 1 - 1 =
A
B
C
D E F G
H I J K L M N O
Almost complete binary tree
A binary tree of depth d is an almost complete binary tree if:
- 1. A node at level less than d -1 has two sons
- 2. For any node in the tree with a right descendant at level d, must have
a left son and every left descendant is either a leaf at level d or has
two sons. The strictly binary tree
is not almost complete,
A
since A has a right descendant
B at level 3 (J) but also has a left
C descendant that is a leaf at level
D E 2 (E)
F G
Violates condition 2
H I J K
Satisfies the condition1, since
every leaf node is either at level 2
or at level 3.
Almost complete binary tree