1 trees 3: the binary search tree section 4.3. 2 binary search tree a binary tree b is called a...

29
1 Trees 3: The Binary Search Tree • Section 4.3

Upload: myra-holland

Post on 24-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

1

Trees 3: The Binary Search Tree• Section 4.3

Page 2: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

2

Binary Search Tree• A binary tree B is called a binary search tree iff:

– There is an order relation < defined for the vertices of B– For any vertex v, and any descendant u in the subtree v.left,

u < v– For any vertex v, and any descendent w in the subtree

v.right, v < w

4

2 6

1 3 75

root

Page 3: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

3

Binary Search Tree

Which one is NOT a BST?

Page 4: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

4

Binary Search Tree

• Consequences– The smallest element in a binary search tree

(BST) is the “left-most” node– The largest element in a BST is the “right-most”

node– Inorder traversal of a BST encounters nodes in

increasing order

4

2 6

1 3 75

root

Page 5: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

5

Binary Search using BST

• Assumes nodes are organized in a binary search tree– Begin at root node– Descend using comparison to make left/right

decision• if (search_value < node_value) go to the left child• else if (search_value > node_value) go to the right child• else return true (success)

– Until descending move is impossible– Return false (failure)

Page 6: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

6

Binary Search using BST

• Runtime <= descending path length <= depth of tree• If tree has “enough” branching, runtime is O(log n)

– Worst case is O(n)

Page 7: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

7

BST Class Template

Page 8: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

8

BST Class Template (contd.)

Internal functionsused in recursive calls

Pointer passed by reference (why?)

Page 9: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

9

BST: Public members calling private recursive functions

Page 10: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

10

BST: Searching for an element

Page 11: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

11

BST: Find the smallest element

Tail recursion

Page 12: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

12

BST: Find the biggest element

Non-recursive

Page 13: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

13

BST: Insertion (5)

Before insertionAfter insertion

Page 14: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

14

BST: Insertion (contd.)

Strategy:

•Traverse the tree as in searching for t with contains()

•Insert if you cannot find t

Page 15: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

15

BST: Deletion of Leaf

Before deleting (3)After deleting (3)

Deleting a node with no child

Deletion Strategy: Delete the node

Page 16: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

16

BST: Delete a Node with One Child

Before deleting (4)After deleting (4)

Deleting a node with one child

Deletion Strategy: Bypass the node being deleted

Page 17: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

17

BST: Delete a Node with Two Children

Before deleting (2) After deleting (2)

Deleting a node with two children

Replace the node with smallest node in the right subtree

Page 18: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

18

BST: Deletion Code

Page 19: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

19

BST Deletion

2

5

3

Element: 5

Left: 208 Right: 0

Element: 3

Left: 0 Right: 160

Element: 4

Left: 0 Right: 0

4

3

Address 208

Address 160

Element: 5

Left: 160 Right: 0

Page 20: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

20

BST: Lazy Deletion

• Another deletion strategy– Don’t delete!– Just mark the node as deleted– Wastes space– But useful if deletions are rare or space is not a

concern

Page 21: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

21

BST: Insertion Bias

• Start with an empty tree• Insert elements in sorted order• What tree do you get?• How do you fix it?

Page 22: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

22

BST: Deletion Bias

After large number of alternating insertions and deletions

Why this bias? How do you fix it?

Page 23: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

23

BST: Search using function objects

Page 24: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

24

Average Search/Insert Time - 1

• Average time is the average depth of a vertex – Let us compute the sum of the depths of all vertices and

divide by the number of vertices– The sum of the depths is called the internal path length

• Give the internal path lengths for the following trees

2 2

31

2

5

3

1

6

9

8

7

0

6

4

2

Page 25: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

25

Average Search/Insert Time - 2

2

51

7

9

8

3

2

Let D(N) be the internal path length of a tree with N verticesIf the root has a left subtree with i nodes, then •D(N) = D(i) + D(N-i-1) + N-1because the depth of each vertex in the subtrees increases by 1

2

51

7

9

811

6

Page 26: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

26

Average Search/Insert Time - 3

Root

Subtree with N-1 nodes

The average value of D(N) is given by the recurrence•D(1) = 0•D(N) = 1/N[ i=0

N-1 D(i) + D(N-i-1)] + N - 1• = 2/N i=0

N-1 D(i) + N - 1

Root

Subtree with N-2 nodes

Subtree with 1 node

Root

Subtree with N-3 nodes

Subtree with 2 nodes

Page 27: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

27

Average Search/Insert Time - 4

•D(N) = 2/N i=0N-1 D(i) + N - 1

•N D(N) = 2 i=0N-1 D(i) + N(N - 1) (1)

•(N-1)D(N-1) = 2 i=0N-2 D(i) + (N-1)(N - 2) (2)

(2) - (1) gives•ND(N) - (N-1)D(N-1) = 2D(N-1) + 2(N-1)•ND(N) = (N+1)D(N-1) + 2(N-1)•D(N)/(N+1) = D(N-1)/N + 2(N-1)/[N(N+1)]• < D(N-1)/N + 2/N

•D(N)/(N+1) < D(N-1)/N + 2/N•D(N-1)/(N) < D(N-2)/(N-1) + 2/(N-1)•D(N-2)/(N-1) < D(N-3)/(N-2) + 2/(N-2)•...•D(2)/(3) < D(1)/2 + 2/2

Page 28: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

28

Average Search/Insert Time - 5

•D(N)/(N+1) < D(N-1)/N + 2/N• < D(N-2)/(N-1) + 2/(N-1) + 2/N• < D(N-3)/(N-2) + 2/(N-2) + 2/(N-1) + 2/N•...• < D(1)/(2) + 2/2 + ... + 2/(N-2) + 2/(N-1) + 2/N• = 2 i=2

N 1/iIf we show that i=2

N 1/i is O(log N), then we can prove that average D(N) = O(N Log N) and so the average depth is O(log N)

•D(N)/(N+1) < D(N-1)/N + 2/N•D(N-1)/(N) < D(N-2)/(N-1) + 2/(N-1)•D(N-2)/(N-1) < D(N-3)/(N-2) + 2/(N-2)•...•D(2)/(3) < D(1)/2 + 2/2

Page 29: 1 Trees 3: The Binary Search Tree Section 4.3. 2 Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation

29

Deriving Time Complexity Using Integration

•Integration can be used to derive good bounds for sums of the formi=a

N f(i) when f(i) is monotonically increasing or decreasing if you knowhow to integrate f(x)

1 2 3 4

1/2

f(x) = 1/x

1/3

1/4

•Area under the rectangles is smaller than that under 1/x• i=2

4 1/i < ∫14 1/x dx

• i=2N 1/i < ∫1

N 1/x dx = ln (N) - ln (1) = O(log N)