Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following...

27
1 Binary Trees (1) Binary Trees (1) Outline and Required Reading: Outline and Required Reading: COSC 2011, Fall 2003, Section A Instructor: N. Vlajic Binary Trees (§ 6.3) Data Structures for Representing Trees (§ 6.4)

Transcript of Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following...

Page 1: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

1

Binary Trees (1)Binary Trees (1)

Outline and Required Reading:Outline and Required Reading:

COSC 2011, Fall 2003, Section AInstructor: N. Vlajic

• Binary Trees (§ 6.3)• Data Structures for Representing Trees (§ 6.4)

Page 2: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

2

Binary Tree

Binary Tree Binary Tree – tree with the following propertiesProper and Ordered !Proper and Ordered ! •••• each internal node has two children

•••• children of internal nodes form orderedpairs: left node – 1st, right node – 2nd

A

B C

D E F G

I J

1st childof A

2nd childof A

ApplicationApplication – representation of arithmetic expression, decision process, …

Page 3: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

3

Binary Tree (cont.)

Example 1 [ binary tree for decision process ]

Are you a mammal?

NoYes

Are you bigger than cat? Do you live under water?

Kangaroo Mouse

Yes No

Trout Can you fly?

Yes No

Robin Snake

Yes No

Page 4: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

4

Properties of Binary Trees

A

B C

E

Binary Tree NotationBinary 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.

Proof Let us annotate max number of nodes at level d with mn(d).

Clearly, mn(0) = 1, andmn(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

Page 5: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

5

Property 1.2 A full binary tree of height h has (2h+1 – 1) nodes.

Proof

n = mn(0) + mn(1) + .. + mn(d) =

= 20 + 21 + 22 + … + 2h =

=

Properties of Binary Trees (cont.)

Full binary tree.

1−=−

− ++

1h1h

221

21

Page 6: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

6

Properties of Binary Trees (cont.)

Property 2 In a binary tree, the number of external nodes is 1 morethan the number of internal nodes, i.e. e = i + 1.

Proof Clearly true for one node.Clearly true for tree nodes.

Assume true for trees with up to n nodes.

Induction as a Proof Technique Induction as a Proof Technique 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)

Page 7: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

7

pa

Properties of Binary Trees (cont.)

Let T be a tree with n+1 nodes (top diagram).

1. Choose a leaf and its parent (which, ofcourse, is internal). For example, the leafa and parent p.

2. Remove the leaf and its parent (middlediagram).

3. Splice the tree back without the twonodes (bottom diagram).

4. Since S has n-1 nodes, S satisfies initialassumption.

5. T is just S + one leaf + one internal so italso satisfies the assumption.

Page 8: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

8

Properties of Binary Trees (cont.)

Approach (2):

Assume true for a tree with n nodes (e = i + 1). Now, wewant to add new external nodes:

1. Cannot add only one external – that would violatethe property of proper binary tree. Hence, it cannotbe: e = i + 2

2. Add two externals. In this case, one old externalbecomes internal, so we have:

enew = (e-1)+2 = e+1 = i+1+1 = i+2inew = i+1

Hence, enew = inew + 1

Page 9: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

9

Properties of Binary Trees (cont.)

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 internal node.

Full binary tree.

Page 10: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

10

Properties of Binary Trees (cont.)

Property 5 The total number of nodes (n) satisfies: 2h+1 ≤≤≤≤ n ≤≤≤≤ 2h+1-1.

Proof Based on Property 3: (h+1) ≤≤≤≤ e ≤≤≤≤ 2h

Based on Property 2 and n=i+e: (h+1) ≤≤≤≤ (n+1)/2 ≤≤≤≤ 2h …

2h+1 ≤≤≤≤ n ≤≤≤≤ 2h+1-1

2h+1 ≤≤≤≤ nh ≤≤≤≤ (n-1)2

Property 6 The height (h) satisfies: log2(n+1)-1 ≤≤≤≤ h ≤≤≤≤ (n-1)/2.

ProofBased on Property 5, the following two inequalities hold:

n ≤≤≤≤ 2h+1-1n+1 ≤≤≤≤ 2h+1

log2(n+1) ≤≤≤≤ h + 1log2(n+1) - 1 ≤≤≤≤ h

Page 11: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

11

Properties of Binary Trees (cont.)

Property 7 The height (h) satisfies: log2(e) ≤≤≤≤ h ≤≤≤≤ e-1.

Proof Based on Property 6: log2(n+1)-1 ≤≤≤≤ h ≤≤≤≤ (n-1)/2

Based on Property 2: log2(2e-1+1)-1 ≤≤≤≤ h ≤≤≤≤ (2e-1-1)/2

log2(2e)-1 ≤≤≤≤ h ≤≤≤≤ e-1

log2(2)+log2(e)-1 ≤≤≤≤ h ≤≤≤≤ e-1

log2(e) ≤≤≤≤ h ≤≤≤≤ e-1

Page 12: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

12

Properties of Binary Trees (cont.)

Summary of PropertiesSummary 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 three.

Page 13: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

13

Binary Tree ADT: Interface

Additional MethodsAdditional Methods public Position leftChild(Position v);/* return the left child of a node *//* error occurs if v is an external node */

public Position rightChild(Position v);/* return the right child of a node *//* error occurs if v is an external node */

public Position sibling(Position v);/* return the sibling of a node *//* error occurs if v is the root */

Binary Tree ADT Binary Tree ADT – extends Tree ADT, i.e. inherits all its methods

Page 14: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

14

Binary Tree: Array-Based Implementation

A

B C

D E F G

Indexing Scheme Indexing Scheme – for every node v of T, let its index/rank p(v) bedefined as follows•••• if v is the root: p(v) = 1•••• if v is the left child of node u: p(v) = 2p(u)•••• if v is the right child of node u: p(v) = 2p(u) + 1

A

2 3

4 5 6 7

B C D E F G

1

Advantages Advantages – simple implementation, easy access

No element at rank 0! 0 1 2 3 4 5 6 7

Page 15: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

15

Space Complexity Space Complexity – let use the following notation•••• n – number of nodes in T•••• pM – maximum value of p(v)•••• N – array size (N=pM+1), i.e. space usage

1) Best Case: full, balanced tree ⇒⇒⇒⇒ all array slots occupiedN = pM+1 = n+1 = O(n)

2) Worst Case: highly unbalanced tree ⇒⇒⇒⇒ many slots empty

height: h = (n-1)/2

max p(v): pM = 2h+1 – 1 = 2(n+1)/2 – 1

required: N = pM+1 = 2(n+1)/2 = O(2n)array size

1

2 3

6 7

14 15

max pM - as if this was

full binary tree

Binary Tree: Array-Based Implementation (cont.)

Page 16: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

16

Array-Based Binary Tree: Performance

Method

position, elementsswapElements, replaceElement

root, parent, childrenleftChild, rightChild, siblingisInternal, isExternal, isRoot

expandExternal, removeAboveExternal

Time

O(n)O(1)O(1)O(1)O(1)O(1)

Run TimesRun Times – GoodGood! all methods, except positions and elements run inconstant O(1) time

Space UsageSpace Usage – Poor! Poor! (in general)best case (full balanced tree): O(n)worst case (highly unbalanced tree): O(2n)

Page 17: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

17

Link Structure - Based Implementation of Binary Tree

Node in Linked Structure Node in Linked Structure – object containingfor Binary Treesfor Binary Trees 1) element

2) reference to parent 3) reference to the right child4) reference to the left child

•••• if node is the root: reference to parent = null•••• if node is external: references to children = null

parent

elementleft child right child

Page 18: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

18

Link Structure - Based Implement. of Binary Tree (cont.)

∅∅∅∅

∅∅∅∅ ∅ ∅∅∅ ∅∅∅∅ ∅ ∅∅∅

∅∅∅∅ ∅∅∅∅

A

A

B C

D E

B C

D E

Example 4 [ binary tree and its linked list implementation ]

Page 19: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

19

BTNode ADT: Implementation

public class BTNode implements Position {

private Object element;private BTNode left, right, parent;

public BTNode(Object o, BTNode u, BTNode v, BTNode w) {setElement(o);setParent(u);setLeft(v);setRight(w);

}

public Object element() { return element; }public void setElement(Object o) { element = o; }

BTNode BTNode Class Class – generalization of Position ADT, i.e. implementsPosition interface

Page 20: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

20

public BTNode getLeft() { return left; }public void setLeft(BTNode v) { left = v; }

public BTNode getRight() { return right; }public void setRight(BTNode v) { right = v; }

public BTNode getParent() { return parent; }public void setParent(BTNode v) { parent = v; }

}

BTNode ADT: Implementation (cont.)

Root Node: Root Node: BTNode root = BTNode(o,null,v, w)

External Node:External Node: BTNode root = BTNode(o, u, null, null)

Page 21: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

21

BTNode ADT: Implementation (cont.)

A

Null_Node Null_Node – contains no elements, no children, has only referenceto the parent•••• implements Position interface !

Extended BT Extended BT – every external node reference becomes reference toNull_Node

•••• with this approach, there is never a need to check whether a reference to a child is null

•••• implementation presented here, and in the textbook, doesnot employ Null_Node

A

(A.getLeft()).element();

null null null null

null null

(A.getLeft()).element();

Page 22: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

22

LinkedBinaryTree ADT: Implementation

LinkedBinaryTreeLinkedBinaryTree Class Class – implements BinaryTree interface, and alsoprovides 2 additional methods1) expandExternal2) removeAboveExternal

public class LinkedBinaryTree implements BinaryTree {private Position root; /* reference to the root */private int size; /* number of nodes */

public LinkedBinaryTree() {root = new BTNode(null, null, null, null);size = 1; }

public void expandExternal (Position v) { … }public void removeAboveExternal (Position v) { … }…

}

For other methods, and their implementation details, see pp. 268-269 of the textbook.

Page 23: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

23

LinkedBinaryTree ADT: Implementation (cont.)

public void expandExternal (Position v) { if (isExternal(v)) {((BTNode) v ).setLeft(new BTNode(null, (BTNode) v, null null);((BTNode) v ).setRigth(new BTNode(null, (BTNode) v, null null);size += 2; }

}

expandExternalexpandExternal() Method () Method – transforms v from external into internalnode, by creating 2 new external nodesand making them the children of v•••• error occurs if v is internal

A

∅∅∅∅ ∅∅∅∅

expandExternal(A)

Application ofApplication of expandExternalexpandExternal()() – used for building a tree – see pp. 27

A

Page 24: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

24

removeAboveExternalremoveAboveExternal() Method () Method – removes external node w togetherwith its parent v, replacing v withthe sibling of w•••• error occurs if w is internal

LinkedBinaryTree ADT: Implementation (cont.)

removeAboveExternal(w)vz

z

Application of Application of removeAboveExternalremoveAboveExternal()() – used to dismantle a tree

w

Page 25: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

25

LinkedBinaryTree ADT: Implementation (cont.)

public void removeAboveExternal (Position v) { if (isExternal(v)) {BTNode p = (BTNode) parent(v);BTNode s = (BTNode) sibling(v);if (isRoot(p)) {

s.setParent(null);root = s; }

else {BTNode g = (BTNode) parent(p);if (p == lefChild(g)) g.setLeft(s);else g.setRight(s);s.setParent(g);

}; size-=2;

}…

}

psv

psv

g

Page 26: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

26

LinkedBinaryTree ADT: Implementation (cont.)

Space Complexity Space Complexity – Good! only O(n), since there is one BTNode objectper every node of the tree •••• no empty slots as in array-based implementation

Method

position, elementsswapElements, replaceElement

root, parent, childrenleftChild, rightChild, siblingisInternal, isExternal, isRoot

expandExternal, removeAboveExternal

Time

O(n)O(1)O(1)O(1)O(1)O(1)

Only immediatechildren !

Run TimesRun Times – GoodGood! all methods, except positions and elements run inconstant O(1) time

Page 27: Binary Trees (1) - York University · 2 Binary Tree Binary Tree – tree with the following properties Proper and Ordered ! •each internal node has two children •children of internal

27

LinkedBinaryTree ADT: Implementation (cont.)

“Albert”

“Chris”“Betty”

“Elvis”“David”

Example 5 [ creating a tree ]

LinkedBinaryTree t = new LinkedBinaryTree();t.root().setElement(“Albert”);t.expandExternal(tree.root());t.root().leftChild().setElement(“Betty”);t.root().rightChild().setElement(“Chris”);t.expandExternal(tree.root().leftChild());t.root().leftChild().leftChild().setElement(“David”);t.root().leftChild().rightChild().setElement(“Elvis”);