binary trees -2 chapter 6 1. 2 threaded trees (depth first) binary trees have a lot of wasted space:...

65
Binary trees -2 Chapter 6 1

Upload: cora-cunningham

Post on 04-Jan-2016

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

1

Binary trees -2

Chapter 6

Page 2: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

2

Page 3: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

3

Threaded trees (depth first)

• Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers

• We can use these pointers to help us in inorder traversals

• We have the pointers reference the next node in an inorder traversal; called threads

• We need to know if a pointer is an actual link or a thread, so we keep a Boolean for each pointer

Page 4: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

4

Threaded Tree Example

8

75

3

11

13

1

6

9

Page 5: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

5

Threaded Tree Traversal

• Start at the leftmost node in the tree, print it, and follow its right thread– following a thread to the right, will output

the node and continue to its right– following a link to the right, will go to the

leftmost node, print it, and continue

Page 6: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

6

Threaded Tree Traversal -1

8

75

3

11

13

1

6

9

Start at leftmost node, print it

Output1

Page 7: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

7

Threaded Tree Traversal -2

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output13

Page 8: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

8

Threaded Tree Traversal -3

8

75

3

11

13

1

6

9

Follow link to right, go to leftmost node and print

Output135

Page 9: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

9

Threaded Tree Traversal -4

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output1356

Page 10: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

10

Threaded Tree Traversal -5

8

75

3

11

13

1

6

9

Follow link to right, go to leftmost node and print

Output13567

Page 11: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

11

Threaded Tree Traversal - 6

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output135678

Page 12: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

8/8/02 12Amir Kamil

Threaded Tree Traversal - 7

8

75

3

11

13

1

6

9

Follow link to right, go to leftmost node and print

Output1356789

Page 13: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

13

Threaded Tree Traversal -8

8

75

3

11

13

1

6

9

Follow thread to right, print node

Output135678911

Page 14: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

14

Threaded Tree Traversal -9

8

75

3

11

13

1

6

9

Follow link to right, go to leftmost node and print

Output13567891113

Page 15: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

15

Threaded Tree Traversal CodeNode leftMost(Node n) { Node ans = n; if (ans == null) { return null; } while (ans.left != null) { ans = ans.left; } return ans;}

void inOrder(Node n) { Node cur = leftmost(n); while (cur != null) { print(cur); if (cur.rightThread) { cur = cur.right; } else { cur = leftmost(cur.right); } }}

Page 16: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

16

Traversal Through Tree Transformation (read only)

• Systematic visiting of all nodes in the tree.

• Each node visited once, does not specify order.

• Breadth-first traversal: visit each node, starting from the lowest level and moving down by level, visiting nodes from left to right.

• Depth-first traversals: go in subtree as deep as you can, backtrack. Differ on the order of visiting root, left, right subtrees.

• preorder: VLR.

• inorder: LVR.

• postorder: LRV.

Page 17: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

17

Insertion

• searching does not modify the tree.• To insert a new node with key el(new), a tree node with a

dead end has to be reached, new node attached to it.• found using same procedure as searching: compare key

of currently scanned node el(cur) to el(new).

• If el(new) less than the key el(cur) try left child; otherwise try right child.

• If the child is empty, discontinue search and make the child point to a new node of key el.

Page 18: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

18

Page 19: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

19

Inserting in threaded tree

•for inorder, only takes care of successors.•Node with a right child: has its successor somewhere in the right subtree, does not need a thread.•Why ? Threads are for "climbing up the tree", not for going down.•A node with no right child has its successor somewhere. Inherits successor from parent.•If a node becomes a left node, its parent is successor.

Page 20: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

20

Page 21: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

21

Deleting (try)

Page 22: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

22

Balancing a tree

• The main idea of DSW algorithm is a rotation• There are two types of rotation, left and right• Rotate Right( Gr, Par, Ch )

– If Par is not the root of the tree• Grandparent Gr of child Ch, becomes Ch’s parent by replacing Par;

– Right subtree of Ch becomes left subtree of Ch’s parent Par;

– Node Ch acquires Par as its right child

Page 23: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

23

A picture will help

Page 24: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

24

Page 25: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

25

Page 26: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

26

AVL tree

• The height of left and right subtree of every node differ by at most one.

Page 27: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 27

Balanced and unbalanced BST

4

2 5

1 3

1

5

2

4

3

7

6

4

2 6

5 71 3

Is this “balanced”?

Page 28: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 28

Perfect Balance

• Want a complete tree after every operation– tree is full except possibly in the lower right

• This is expensive– For example, insert 2 in the tree on the left and

then rebuild as a complete tree

Insert 2 &complete tree

6

4 9

81 5

5

2 8

6 91 4

Page 29: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 29

AVL - Good but not Perfect Balance

• AVL trees are height-balanced binary search trees

• Balance factor of a node– height(left subtree) - height(right subtree)

• An AVL tree has balance factor calculated at every node– For every node, heights of left and right subtree

can differ by no more than 1– Store current heights in each node

Page 30: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 30

Node Heights

1

00

2

0

6

4 9

81 5

1

height of node = hbalance factor = hleft-hright

empty height = -1

0

0

height=2 BF=1-0=1

0

6

4 9

1 5

1

Tree A (AVL) Tree B (AVL)

Page 31: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 31

Node Heights after Insert 7

2

10

3

0

6

4 9

81 5

1

height of node = hbalance factor = hleft-hright

empty height = -1

1

0

2

0

6

4 9

1 5

1

0

7

0

7

balance factor 1-(-1) = 2

-1

Tree A (AVL) Tree B (not AVL)

Page 32: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 32

Insert and Rotation in AVL Trees

• Insert operation may cause balance factor to become 2 or –2 for some node – only nodes on the path from insertion point to

root node have possibly changed in height– So after the Insert, go back up to the root node by

node, updating heights– If a new balance factor (the difference hleft-hright) is

2 or –2, adjust tree by rotation around the node

Page 33: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 33

Single Rotation in an AVL Tree

2

10

2

0

6

4 9

81 5

1

0

7

0

1

0

2

0

6

4

9

8

1 5

1

0

7

Page 34: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 34

Let the node that needs rebalancing be .

There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of .

The rebalancing is performed through four separate rotation algorithms.

Insertions in AVL Trees

Page 35: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 35

j

k

X Y

Z

Consider a validAVL subtree

AVL Insertion: Outside Case

h

hh

Page 36: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 36

j

k

XY

Z

Inserting into Xdestroys the AVL property at node j

AVL Insertion: Outside Case

h

h+1 h

Page 37: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 37

j

k

XY

Z

Do a “right rotation”

AVL Insertion: Outside Case

h

h+1 h

Page 38: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 38

j

k

XY

Z

Do a “right rotation”

Single right rotation

h

h+1 h

Page 39: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 39

jk

X Y Z

“Right rotation” done!(“Left rotation” is mirror symmetric)

Outside Case Completed

AVL property has been restored!

h

h+1

h

Page 40: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 40

j

k

X Y

Z

AVL Insertion: Inside Case Consider a validAVL subtree

h

hh

Page 41: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 41

Inserting into Y destroys theAVL propertyat node j

j

k

XY

Z

AVL Insertion: Inside Case

Does “right rotation”restore balance?

h

h+1h

Page 42: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 42

jk

X

YZ

“Right rotation”does not restorebalance… now k isout of balance

AVL Insertion: Inside Case

hh+1

h

Page 43: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 43

Consider the structureof subtree Y… j

k

XY

Z

AVL Insertion: Inside Case

h

h+1h

Page 44: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 44

j

k

XV

Z

W

i

Y = node i andsubtrees V and W

AVL Insertion: Inside Case

h

h+1h

h or h-1

Page 45: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 45

j

k

XV

Z

W

i

AVL Insertion: Inside CaseWe will do a left-right “double rotation” . . .

Page 46: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 46

j

k

X V

ZW

i

Double rotation : first rotationleft rotation complete

Page 47: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 47

j

k

X V

ZW

i

Double rotation : second rotation

Now do a right rotation

Page 48: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 48

jk

X V ZW

i

Double rotation : second rotation

right rotation complete

Balance has been restored

hh h or h-1

Page 49: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 49

Implementation

balance (1,0,-1)

key

rightleft

No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations

Once you have performed a rotation (single or double) you won’t need to go back up the tree

Page 50: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 50

Example of Insertions in an AVL Tree

1

0

2

20

10 30

25

0

35

0

Insert 5, 40

Page 51: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 51

Example of Insertions in an AVL Tree

1

0

2

20

10 30

25

1

35

0

50

20

10 30

25

1

355

40

0

0

01

2

3

Now Insert 45

Page 52: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 52

Single rotation (outside case)

2

0

3

20

10 30

25

1

35

2

50

20

10 30

25

1

405

40

0

0

0

1

2

3

45

Imbalance35 45

0 0

1

Now Insert 34

Page 53: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

AVL Trees 53

Double rotation (inside case)

3

0

3

20

10 30

25

1

40

2

50

20

10 35

30

1

405

45

0 1

2

3

Imbalance

45

0

1

Insertion of 34

35

34

0

0

1 25 340

Page 54: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

54

Heap

• A heap is a binary tree T that stores a key-element pairs at its internal nodes.

• Properties :

• Min Heap: key(parent) < key(child)• Max Heap: key(parent) > key(child)]

Page 55: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

55

Example

Page 56: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

56

Example of a complete binary max-heap

• if B is a child node of A, then key(A) ≥ key(B).

• This implies that an element with the greatest key is always in the root node, and so such a heap is sometimes called a max-heap.

• Alternatively, if the comparison is reversed, the smallest element is always in the root node, which results in a min-heap.

Page 57: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

57

Page 58: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

58

Page 59: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

59

Page 60: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

60

Max heap

• Terminate heap when– reach root– key parent is greater than key child

Page 61: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

61

Page 62: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

62

Page 63: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

63

Page 64: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

64

Page 65: Binary trees -2 Chapter 6 1. 2 Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can

65

6.8 and 6.10 no need