Binary Trees (1) - York · PDF file8 Assume true for a tree with n nodes: E = I + 1. Now, we...
Transcript of Binary Trees (1) - York · PDF file8 Assume true for a tree with n nodes: E = I + 1. Now, we...
1
Binary Trees (1)
Outline and Required Reading:
CSE 2011, Winter 2017, Section ZInstructor: N. Vlajic
• Binary Trees (§ 8.2)
2Binary Tree
Binary Tree – ordered tree with the following properties• each internal node has at most two children• children of internal nodes form ordered pairs:
left node – 1st, right node – 2nd
A
B C
D E F G
I J
1st childof A
2nd childof A
Application – representation of arithmetic expression, decision treeswith yes / no answers , …
3Binary Tree (cont.)
Example 1 [ binary tree for ‘yes/no’ decision process ]
Part of a decision tree containing yes-no questions that diagnose a problem with a TV …
4Properties of Proper Binary Trees
A
B C
E
Binary Tree Notation• n – number of nodes
• e – number of external nodes
• i – number of internal nodes
• h – height of the tree
• level – set of nodes with the same depth
Property 1.1 Level d has at most 2d nodes. (Applies to any binary tree.)
Proof Let us annotate max number of nodes at level d with mn(d).
Clearly, mn(0) = 1 = 20,mn(1) = 2 = 21,mn(2) = 4 = 22,mn(d) = 2*mn(d-1) for ∀d≥0 .
Hence, mn(d) = 2*mn(d-1) = 2*2*mn(d-2) = 2*[2*[..2*mn(0)]] = 2d
D
I J
F G
level 1
level 2
level 3
level 0
5
Property 1.2 A full binary tree of height h has (2h+1 – 1) nodes.
Proof
n = mn(0) + mn(1) + .. + mn(d=h) =
= 20 + 21 + 22 + … + 2h =
=
Full binary tree.
11−=
− ++
1h1h
21-2
2
Properties of Proper Binary Trees (cont.)
6
Property 2 In a proper binary tree, the number of external nodes is 1 more than the number of internal nodes, i.e. e = i + 1.
i
e e
Proof Clearly true for BT with one internal node.
i
i
e
i
eee
Mathematical Induction Assume we want to verify the correctness of a statement (P(n)).(1) First, prove that P(n) holds for n=1 (2, 3);(2) assume it holds for an arbitrary n and try to prove it holds for (n+1).
i
i
e
e
e
Properties of Proper Binary Trees (cont.)
Clearly true for BT with two internal nodes.
Clearly true for BT with three internal nodes.
Assume true for trees with up to n nodes.
7E = I + 1
should prove Enew = (I+1) + 1 = Inew + 1
In the new tree: Inew = I+1Enew = (E-1)+2 = E+1 = (I+1)+1 = Inew+ 1
8
Assume true for a tree with n nodes: E = I + 1.Now, we want to extend the tree by adding new nodes:
1. Cannot add only one node.• add one internal – impossible! • add one external – that would violate the propertyof proper binary tree
2. Add two externals. In this case, one old externalbecomes internal, so we have:
Inew = I+1Enew = (E-1)+2 = E+1 = (I+1)+1 = I+2
Hence, Enew = Inew + 1
ie i
ee
ii i
eeee
What is the next bigger proper binary tree??
Properties of Proper Binary Trees (cont.)
tree with n nodes
next larger proper tree
9
Property 3 The number of external nodes (e) satisfies: (h+1) ≤ e ≤ 2h.
Proof
Property 4 The number of internal nodes (i) satisfies: h ≤ i ≤ 2h-1.Proof
Based on Property 3: h+1 ≤ e ≤ 2h
Based on Property 2: h+1 ≤ i+1 ≤ 2h
h ≤ i ≤ 2h-1
At every level (except last)there is only one external node.
Full binary tree.
If the tree-height (h) is known, what is the number of external nodes?!
If the tree-height (h) is known, what is the number of internal nodes?!
Properties of Proper Binary Trees (cont.)
i+1
h=3
10
Property 5 The total number of nodes (n) satisfies: 2h+1 ≤ n ≤ 2h+1-1.
ProofBased on Property 3: (h+1) ≤ e ≤ 2h
Based on Property 4: h ≤ i ≤ 2h-1Based on n=i+e: 2h+1 ≤ n ≤ 2h+1-1
2h+1 ≤ n ⇒ h ≤ (n-1)/2
Property 6 The height (h) satisfies: log2(n+1)-1 ≤ h ≤ (n-1)/2.
Proof Based on Property 5, the following two inequalities hold:
n ≤ 2h+1-1 ⇒ n+1 ≤ 2h+1 ⇒ log2(n+1) ≤ h + 1 ⇒ log2(n+1) - 1 ≤ h
Properties of Proper Binary Trees (cont.)
If the number of nodes in a tree is known, what is the height of the tree?!
If the tree-height (h) is known, what is the overall number of nodes?!
11
Property 7 The height (h) satisfies: log2(e) ≤ h ≤ e-1.
ProofBased on Property 3: (h+1) ≤ e ≤ 2h
From 1st inequality: h ≤ e-1From 2nd inequality: log2(e) ≤ h
Property 8 The height (h) satisfies: log2(i+1) ≤ h ≤ i.
ProofBased on Property 2: e = i + 1Apply Property 2 to Property 7.
Properties of Proper Binary Trees (cont.)
If the number of external nodes is known, what is the height of the tree?!
If the number of internal nodes is known, what is the height of the tree?!
12
Summary of Properties
n = e + i
e = i + 1
(h+1) ≤ e ≤ 2h
h ≤ i ≤ 2h-1
2h+1 ≤ n ≤ 2h+1-1
log2(n+1)-1 ≤ h ≤ (n-1)/2
log2(e) ≤ h ≤ e-1
log2(i+1) ≤ h ≤ i
Number of external, internal, and overall nodes as a
function of tree’s height
Tree’s height as a function of number of external, internal,
of overall nodes
All other expressionscan be obtained from
these 3.
Properties of Proper Binary Trees (cont.)
13Binary Tree ADT: Interface
Additional Methods public Position<E> left(Position<E> v) throws…;/* return left child of a node; null if no child exists */
public Position<E> right(Position<E> v) throws…;/* return right child of a node, null if no child exists */
public Position<E> sibling(Position<E> p) throws…;/* returns position of p’s sibling, null if no sibling exists */
Binary Tree ADT – extends Tree ADT, i.e. inherits all its methods:size, isEmpty, iterator, positions, replace, root, parent,children, isInternal, isExternal, isRoot
14Binary Tree: Array-Based Implementation
A
B C
D E F G
Indexing Scheme – for every node v of T, let its index/rank rank(v) bedefined as follows:
• if v is the root: rank(v) = 1• if v is the left child of node u: rank(v) = 2*rank(u)• if v is the right child of node u: rank(v) = 2*rank(u) + 1
A
2 3
4 5 6 7
B C D E F G
1
Advantages – simple implementation, easy access
No element at rank 0! 0 1 2 3 4 5 6 7
How could the parent-rank be
found?!
15
Space Complexity – let use the following notation• n – number of nodes in T• rankmax – maximum value of rank(v)• N – array size (i.e. space usage): N=rankmax+1
1) Best Case: full, balanced tree ⇒ all array slots occupied
rankmax = n ⇒ N = O(n)
2) Worst Case: highly unbalanced tree ⇒ many slots empty
height: h = (n-1)/2
rankmax: rankmax = 2h+1 – 1 = 2(n+1)/2 – 1
required: N = rankmax+1 = 2(n+1)/2 = O(2n)array size
1
2 3
6 7
14 15
rankmax -as if the treeof this height
was full
Binary Tree: Array-Based Implementation
16Array-Based Binary Tree: Performance
Method
position, iteratorsize, isEmpty
replaceroot, parent, children, left, right
hasLeft, hasRightisInternal, isExternal, isRoot
Time
O(n)O(1)O(1)O(1)O(1)O(1)
Run Times – Good! all methods, except iterator and positions run inO(n) time
Space Usage – Poor! (in general)best case (full balanced tree): O(n)worst case (highly unbalanced tree): O(2n)
Explain!