12 CS253 CH8 Tree Part 2 BinaryTree
12 CS253 CH8 Tree Part 2 BinaryTree
Binary Tree
Dr. Ameera Jaradat
ameera@yu.edu.jo
Root node
B C
D F H
I I J I J
Binary Tree application: An arithmetic expression
• Represent a number
of different
outcomes that can
result from
answering a series of
yes-or-no questions.
• Each internal node is
associated with a
question.
Complete , full
Full
A B
C D
Skewed Binary Tree
• Each node has only one left child
• Each node has only one right child
• skewed binary tree of n nodes has a depth of (n-1).
Properties of Binary Trees
• In general, level d has at most 2d nodes
B C 1 21 = 2
D F G H 2 22 = 4
I J K L M N O P 3 23 = 8
h
Maximum number of nodes in a binary tree with height = h is
∑ 2𝑘=2 h+1 − 1
𝑘=0
Perfect Binary Tree has Maximum number of nodes
Properties of Binary Trees
• In perfect binary tree
– Number of nodes at depth d is 2d.
– Total number of nodes of tree with height h = 2h+1 -1.
– Number of leaf nodes in a tree of height h = 2h
– Number of internal nodes in a tree of height h = 2h − 1
– The height of a tree with n nodes = log(n+1) -1.
• In general
– The minimum number of nodes in a binary tree of height h = h + 1
– The maximum number of nodes in a binary tree of height h = 2h+1 − 1
– The minimum height of a binary tree with n nodes = log(n+1) -1.
– The maximum height of a binary tree with n nodes = n-1.
log ( 𝑁 +1 ) − 1 ≤ 𝒉 ≤ 𝑁 − 1
h +1
h+ 1≤ 𝑵 ≤ 2 −1
Perfect BT has the max
number of nodes and the min
height
: Next
IMPLEMENTING TREES
TREE TRAVERSAL