30
Review of Chapter 5 張張張

Review of Chapter 5

  • Upload
    dora

  • View
    19

  • Download
    0

Embed Size (px)

DESCRIPTION

Review of Chapter 5. 張啟中. Threaded Binary Tree. 利用 Binary Tree 節點中的 0-links ,指向中序的先行者或後繼者,以方便中序追蹤。 Threading Rules - PowerPoint PPT Presentation

Citation preview

Page 1: Review of Chapter 5

Review of Chapter 5

張啟中

Page 2: Review of Chapter 5

Threaded Binary Tree

利用 Binary Tree 節點中的 0-links ,指向中序的先行者或後繼者,以方便中序追蹤。

Threading Rules A 0 RightChild field at node p is replaced by a pointer to the

node that would be visited after p when traversing the tree in inorder. That is, it is replaced by the inorder successor of p. ( 右樹指標指向該節點中序的後繼者 )

A 0 LeftChild link at node p is replaced by a pointer to the node that immediately precedes node p in inorder (i.e., it is replaced by the inorder predecessor of p). ( 左樹指標指向該節點中序的先行者 )

Page 3: Review of Chapter 5

Threaded Binary Tree

A

H I

B

D E

C

GF

Inorder sequence: H, D, I, B, E, A, F, C, G

Page 4: Review of Chapter 5

f - f

f A f

f B f

f D f

t H t t I t

t E t

f B f

f D f t E t

Threaded Binary Tree with Head Node

TRUE FALSE

LeftThread LeftChild RightChild RightThreaddata

Head Node

Page 5: Review of Chapter 5

Manipulation of Threaded Binary Tree Traversal (Inorder 不需 Stack) Insert a Node Delete a Node

Page 6: Review of Chapter 5

Insert a Node to Threaded Binary Tree

s

r

s

r

before after

Page 7: Review of Chapter 5

Insert a Node to Threaded Binary Tree

rr

s

r

s

before after

Page 8: Review of Chapter 5

Heaps

Definition A max (min) tree is a tree in which the key value

in each node is no smaller (larger) than the key values in its children (if any).

A max heap is a complete binary tree that is also a max tree.

A min heap is a complete binary tree that is also a min tree.

We can use the max heap to implement the priority Queues.

Page 9: Review of Chapter 5

Priority Queues

A data structure supports the below two operations is called max (min) priority queue. In a priority queue, the element to be deleted is

the one with highest (or lowest) priority. An element with arbitrary priority can be inserted

into the queue according to its priority.

Page 10: Review of Chapter 5

Priority Queues 的運用

Suppose a server that serve multiple users. Each user may request different amount of server time. A priority queue is used to always select the request with the smallest time. Hence, any new user’s request is put into the priority queue. This is the min priority queue.

If each user needs the same amount of time but willing to pay more money to obtain the service quicker, then this is max priority queue.

Page 11: Review of Chapter 5

Representation of The Priority Queues Unorder Linear List

Addition complexity: O(1) Deletion complexity: O(n)

Chain Addition complexity: O(1) Deletion complexity: O(n)

Ordered List Addition complexity: O(n) Deletion complexity: O(1)

Page 12: Review of Chapter 5

Max Heap Examples

14

12 7

10 8 6

9

6 3

5

Page 13: Review of Chapter 5

Manipulation of The Heap

Create of an empty heap Insertion of a new element into the heap. Deletion of the largest element from the heap

Please see book p286 ADT 5.2

Page 14: Review of Chapter 5

Insertion into a Max Heap

52

20

15 2

14 10

20

15

14 10 5

5

2

O(logn)

Page 15: Review of Chapter 5

Insertion into a Max Heap

52

20

15 2

14 10

20

15

14 10 21

請自己練習

Page 16: Review of Chapter 5

Deletion from a Max Heap

20

15 2

14 10

20

15 2

14 10

請同學自己繼續練習刪除 15

O(logn)

Page 17: Review of Chapter 5

Binary Search Tree

Definition A binary serach tree is a binary tree. It may be em

pty. If it is not empty then it satisfies the following properties: Every element has a key and no two elements have the

same key (i.e., the keys are distinct) The keys (if any) in the left subtree are smaller than the

key in the root. The keys (if any) in the right subtree are larger than the

key in the root. The left and right subtrees are also binary search trees.

Page 18: Review of Chapter 5

Binary Search Tree Examples

20

15 25

14 10 22

30

5 40

2

60

70

8065

Not binary search tree

Binary search trees

Yes!

Page 19: Review of Chapter 5

Manipulation of The Binary Search Tree Searching a Binary Search Tree Insertion into a Binary Search Tree Deletion from a Binary Search Tree Joining and Splitting Binary Search Tree

Page 20: Review of Chapter 5

Searching A Binary Search Tree If the root is 0, then this is an empty tree. No

search is needed. If the root is not 0, compare the x with the key

of root. If x equals to the key of the root, then it’s done. If x is less than the key of the root, then only need

to search the left tree. If x larger than the key of the root, only the right su

btree is to be searched.

Page 21: Review of Chapter 5

Insertion into a Binary Search Tree

30

5 40

2 80

30

5 40

2 8035

Page 22: Review of Chapter 5

Deletion from a Binary Search Tree Delete a leaf node

A leaf node which is a right child of its parent A leaf node which is a left child of its parent

Delete a non-leaf node A node that has one child A node that has two children

Replaced by the largest element in its left subtree, or Replaced by the smallest element in its right subtree

Again, the delete function has complexity of O(h)

Page 23: Review of Chapter 5

Deletion from a Binary Search Tree

5

30

40

2 803535 80

5

Page 24: Review of Chapter 5

Deletion from a Binary Search Tree

30

5 40

2 8035

32 37 90

30

5

2 8035

32

37

90

Page 25: Review of Chapter 5

Deletion from a Binary Search Tree

30

5 40

2 8035

32 37 90

30

5

2

80

35

32 37

90

Page 26: Review of Chapter 5

Joining and Splitting Binary Trees C.ThreeWayJoin(A, x, B)

Creates a binary search tree C that consists of binary search tree A, B, and element x.

C.TwoWayJoin(A, B) Joins two binary search trees A and B to obtain a single bi

nary search tree C. A.Split(i, B, x, C)

Binary search tree A splits into three parts: B (a binary search tree that contains all elements of A that have key less than i); if A contains a key i than this element is copied into x and a pointer to x returned; C is a binary search tree that contains all records of A that have key larger than i

Page 27: Review of Chapter 5

ThreeWayJoin(A, x, B)

30

5 40

2 8035

90

85 94

84 92

81

A Bx

81

Page 28: Review of Chapter 5

C.TwoWayJoin(A, B)

30

5 40

2 8035

A

90

85 94

84 92

B

8084

Page 29: Review of Chapter 5

A.Split(i, B, x, C)

30

5 40

2 8035

A

8175

i = 30

B

C

x

Page 30: Review of Chapter 5

A.Split(i, B, x, C)

30

5 40

2 8035

A

8175

i = 80

B

C

ZY

30

Lt

t

5

2

L

t40

35 80

x

L

75

81

R

R