CS 05
CS 05
7
An arithmetic express tree
• ((x + 5) % 3) − (3x + 8) —
% +
+ 3 3x 8
x 5
8
Decision Tree
Stipend at least
• Should I accept an internship offer 10K a month
Decline Offer
Offers free coffee
9
Complete Binary Tree
i
• Level i has 2 nodes
• In a tree of height h
( 2 )
h h h+1 n+1
• Total # nodes n = 2 + 2 − 1 = 2 −1 height = log2
n+1
• Total # leaves = 2 10
Binary Tree
• A binary tree can be obtained from an appropriate complete binary tree
by pruning
11
Minimum height of a binary tree
• A binary tree of height h has
i
• At most 2 nodes at level i
2 h h+1
• At most 1 + 2 + 2 + … + 2 = 2 − 1 nodes
12
Minimum height of a binary tree
• A binary tree of height h has
i
• At most 2 nodes at level i
2 h h+1
• At most 1 + 2 + 2 + … + 2 = 2 − 1 nodes
• h ≥ log2(n + 1)/2
13
Maximum height of a binary tree
• A binary tree of n nodes has height at most n − 1
14
Number of leaves in a binary tree
• Number of leaves is ≤ 1+ number of internal nodes
• Proof is by induction on number of internal nodes
• Tree with 1 node has a leaf but no internal node
• Assume the statement is true for a tree with at most k − 1 internal
nodes
• A tree with k internal nodes has k1 internal nodes in left subtree and
(k − k1 − 1) internal nodes in right subtree.
15
Leaves in a binary tree
• For a binary tree on n nodes
• # leaves + # internal nodes = n
• Minimum #leaves = 1
16
Abstract Data Types (ADTs) for Trees
• size() • positions ()
• isEmpty() • swapElements (p, q)
• elements() • replaceElement (p, e)
• isRoot (p)
• root ()
• isInternal (p, q)
• parent (p)
• replaceElement (p)
• children (p)
• isExternal(p)
17
Abstract Data Types (ADTs) for Binary Trees
• root ()
• parent (p)
• children (p)
• leftChild (p)
• rightChild (p)
• sibling(p)
18
The Node Structure for a Binary Tree p(x)
Key Element
• Each node in the tree contains
left(x) right(x)
• key(x) ⟶ key
• element(x) ⟶ element p(x)
• left(x) ⟶ pointer to the left child Key Element
• right(x) ⟶ pointer to the right child left(x) right(x)
• firstChild →
UnboundedTree
• nextSibling →
UnboundedTree
20
Tree Walks/Traversals
• A tree walk is a way of visiting all the nodes in a tree in a specified order.
• A visit to a node means we are doing some processing on the data of
that node.
• A preorder tree walk processes each node before processing its children.
• A postorder tree walk processes each node after processing its children.
21
Preorder walk A
• Preorder
Traversal
B C D E F G
H I J K L M N
22
Preorder walk A
• Preorder
Traversal
B C D E F G
H I J K L M N
Algorithm preOrder(v)
visit node v
for each child w of v
recursively perform preOrder (w)
23
Postorder walk
• Postorder
traversal
24
Postorder walk
Algorithm postOrder(v)
for each child w of v
recursively perform postOrder (w)
visit node v
25
Traversals of Binary Trees
26
Examples
A
• Preorder traversal
A, B, C, D, F, G, E
B E
• Postorder traversal C D
C, F, G, D, B, E, A
F G
27
Evaluating Mathematical Expression
• ((x + 5) % 3) − (3x + 8) —
% +
+ 3 3x 8
x 5
28
Evaluating Mathematical Expression
• Inorder traversal
C, B, F, D, G, A, E
30
Euler Tour Traversal
• Generic Traversal of a binary tree
—
• Walk around the tree and visit each node 3 times
• On the left
• From below
% +
• On the right
+ 3 3x 8
x 5
31
Printing an arithmetic expression ((x + 5) % 3) − (3x + 8)
• Use Euler Walk
—
• Print ′(′ before traversing the left subtree.
• Traverse the left subtree
• Print the element at the node
% +
• Traverse the right subtree
• Print ′)′ 3 8
+ 3x
x 5
32