Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has...

21
Introduction to Algorithms Binary Search Trees My T. Thai @ UF

Transcript of Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has...

Page 1: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Introduction to Algorithms

Binary Search Trees

My T. Thai @ UF

Page 2: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Motivation

Given a sequence of values:

How to get the max, min value efficiently?

How to find the location of a given value?

Trivial solution: linearly check elements one by

one

Search tree data structure supports better

SEARCH, MINIMUM, MAXIMUM,

PREDECESSOR, SUCCESSOR, INSERT, and

DELETE operations of dynamic setsMy T. Thai

[email protected]

2

Page 3: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Binary Search Tree

Each node has at most two children

Each node contains:

key and data

left: points to the left child

right: points to the right child

p (parent): point to parent

Binary-search-tree property:

y is a node in the left subtree of x:

y is a node in the right subtree of x:

Height: hMy T. Thai

[email protected]

3

Page 4: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Example

My T. Thai

[email protected]

4

Page 5: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Print out keys

preorder tree walk:

print key of node before printing keys in subtrees

(node left right)

inorder tree walk:

print key of node after printing keys in its left

subtree and before printing keys in its right subtree

(left node right)

postorder tree walk:

print key of node after printing keys in subtrees (left

right node)

My T. Thai

[email protected]

5

Page 6: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Example

Preorder: F, B, A, D, C, E, G, I, H

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

(why?)

Postorder: A, C, E, D, B, H, I, G, F

My T. Thai

[email protected]

6

Page 7: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Inorder tree walk

Visit and print each node once

Time:

My T. Thai

[email protected]

7

Page 8: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Operations

Querying operations

Search: get node of given key

Minimum: get node having minimum key

Maximum: get node having maximum key

Successor: get node right after current node

Predecessor: get node right before current node

Updating operations

Insertion: insert a new node

Deletion: delete a node with given key

My T. Thai

[email protected]

8

Page 9: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Search

Time = the length of path from root to found

node

Time:

My T. Thai

[email protected]

9

Page 10: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Minimum and maximum

Minimum: left most node

Maximum: right most node

Time:

My T. Thai

[email protected]

10

Page 11: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Successor

Time:

My T. Thai

[email protected]

11

Go up in left direction until turn right

Successor in right subtree

Page 12: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Example

Successor of 15 is 17

Successor of 13 is 15

My T. Thai

[email protected]

12

Page 13: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Insertion

Attach new

node z as a leaf

Travel down

from root to

find the

attached

position

Time:

My T. Thai

[email protected]

13

Page 14: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Example: insert item with key 13

My T. Thai

[email protected]

14

Page 15: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Deletion

Key point: choose a node in subtree rooted at z

to replace the deleted node z

Node to replace z: predecessor or successor of z

Three cases:

z has no child: remove it

z has only left child or right child (a, b)

z has both left and right child

Right child has no left subtree (c)

Right child has left subtree (d)

My T. Thai

[email protected]

15

Page 16: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

z has one child

My T. Thai

[email protected]

16

Replace z by its child

Page 17: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Right child has no left subtree

Replace z by its successor y

My T. Thai

[email protected]

17

Page 18: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Right child has left subtree

1. Find successor y of z

2. Replace y by its child

3. Replace z by y

My T. Thai

[email protected]

18

Page 19: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Replace a node by its child

Replace the subtree

rooted at node u with

the subtree rooted at

node v

Running time: O(1)

My T. Thai

[email protected]

19

Page 20: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Deletion Algorithm

Main running

time: find z’s

successor

Time:

My T. Thai

[email protected]

20

Page 21: Introduction to Algorithms - University of Florida · PDF fileBinary Search Tree Each node has at most two children Each node contains: key and data left: points to the left child

Summary

Binary search tree stores data hierarchically

Support SEARCH, MINIMUM, MAXIMUM,

PREDECESSOR, SUCCESSOR, INSERT, and

DELETE operations

Running time of all operation is

Question: What is the lower bound of h? How

to achieve it?

My T. Thai

[email protected]

21