CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.

Post on 20-Dec-2015

213 views 0 download

Transcript of CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.

CSE 326: Data StructuresBinary Search Trees

Ben Lerner

Summer 2007

2

Tree CalculationsRecall: height is max

number of edges from root to a leaf

Find the height of the tree...

t

Can you do this in less than O(n) time?

3

Tree Calculations Example

A

E

B

D F

C

G

IH

KJ L

M

L

N

How high is this tree?

4

Binary Trees• Binary tree is

– a root– left subtree (maybe

empty) – right subtree (maybe

empty)

• Representation:

A

B

D E

C

F

HG

JI

Data

right pointer

leftpointer

5

Binary Tree: RepresentationA

right pointer

leftpointer A

B

D E

C

F

Bright

pointerleft

pointer

Cright

pointerleft

pointer

Dright

pointerleft

pointer

Eright

pointerleft

pointer

Fright

pointerleft

pointer

6

Binary Tree: Special Cases

A

B

D E

C

GF

IH

A

B

D E

C

F

A

B

D E

C

GF

Full Tree

Complete Tree Perfect Tree

7

Binary Tree: Some Numbers!For binary tree of height h:

– max # of leaves:

– max # of nodes:

– min # of leaves:

– min # of nodes:

8

More Recursive Tree Calculations:

Tree TraversalsA traversal is an order for

visiting all the nodes of a tree

Three types:• Pre-order: Root, left subtree, right

subtree

• In-order: Left subtree, root, right subtree

• Post-order: Left subtree, right subtree, root

+

*

2 4

5

(an expression tree)

9

Traversals

void traverse(BNode t){

if (t != NULL)

traverse (t.left);

print t.element;

traverse (t.right);

}

} Which kind of traversal is this?

10

ADTs Seen So Far

• Stack– Push– Pop

• Queue– Enqueue– Dequeue

What about decreaseKey?

• Priority Queue– Insert– DeleteMin

11

The Dictionary ADT

• Data:– a set of

(key, value) pairs

• Operations:– Insert (key,

value)– Find (key)– Remove (key) The Dictionary ADT is sometimes

called the “Map ADT”

• blernerBen Lerner,OH: Tues, 10:45—12:00CSE 212

• ioannisIoannis Giotis,OH: M 11:00CSE 430

• gyanitGyanit Singh,OH: M 11:00CSE 430

insert(blerner, ….)

find(ioannis)• ioannis Ioannis Giotis, …

12

A Modest Few Uses

• Sets

• Dictionaries

• Networks : Router tables

• Operating systems : Page tables

• Compilers : Symbol tables

Probably the most widely used ADT!

13

Implementations

• Unsorted Linked-list

• Unsorted array

• Sorted array

insert deletefind

14

Binary Search Tree Data Structure

4

121062

115

8

14

13

7 9

• Structural property– each node has 2 children– result:

• storage is small• operations are simple• average depth is small

• Order property– all keys in left subtree smaller

than root’s key– all keys in right subtree larger

than root’s key– result: easy to find any given key

• What must I know about what I store?

15

Example and Counter-Example

3

1171

84

5

4

181062

115

8

20

21BINARY SEARCH TREE NOT ABINARY SEARCH TREE

7

15

16

Find in BST, RecursiveNode Find(Object key, Node root) { if (root == NULL) return NULL;

if (key < root.key) return Find(key, root.left); else if (key > root.key) return Find(key, root.right); else return root;}

2092

155

10

307 17

Runtime:

17

Find in BST, IterativeNode Find(Object key, Node root) {

while (root != NULL && root.key != key)

{ if (key < root.key) root = root.left; else root = root.right; }

return root;}

2092

155

10

307 17

Runtime:

18

Insert in BST

2092

155

10

307 17

Runtime:

Insert(13)Insert(8)Insert(31)

Insertions happen only at the leaves – easy!

19

BuildTree for BST• Suppose keys 1, 2, 3, 4, 5, 6, 7, 8, 9 are inserted

into an initially empty BST. Runtime depends on the order!

– in given order

– in reverse order

– median first, then left median, right median, etc.

20

Bonus: FindMin/FindMax

• Find minimum

• Find maximum2092

155

10

307 17

21

Deletion in BST

2092

155

10

307 17

Why might deletion be harder than insertion?

22

Delete Operation

• Delete is a bit trickier…Why?• Suppose you want to delete 10• Strategy:

– Find 10– Delete the node containing 10

• Problem: When you delete a node,what do you replace it by?

94

10 97

5 24

11

17

23

Delete Operation

• Problem: When you delete a node,what do you replace it by?

• Solution:– If it has no children, by NULL– If it has 1 child, by that child– If it has 2 children, by the node with

the smallest value in its right subtree(the successor of the node)

94

10 97

5 24

11

17

24

Delete “5” - No children

Find 5 node

Then Freethe 5 node and NULL the pointer to it

94

10 97

5 24

11

17

94

10 97

5 24

11

17

25

Delete “24” - One child

Find 24 node

Then Freethe 24 node and replace the pointer to it witha pointer to itschild

94

10 97

5 24

11

17

94

10 97

5 24

11

17

26

Delete “10” - two children

Find 10,Copy the smallestvalue inright subtreeinto the node

Then recursivelyDelete node with smallest valuein right subtreeNote: it does nothave two children

94

10 97

5 24

11

17

94

11 97

5 24

11

17

27

Delete “11” - One child

Remember11 node

Then Freethe 11 node and replace the pointer to it witha pointer to itschild

94

11 97

5 24

11

17

94

11 97

5 24

11

17

28

Runtimes

• Find? Insert? Delete?

• What is the average height of a BST?

• What is the maximum height?

• What happens when we insert nodes in sorted order?

29

Balanced BST

Observation• BST: the shallower the better!• For a BST with n nodes

– Average height is O(log n)– Worst case height is O(n)

• Simple cases such as insert(1, 2, 3, ..., n)lead to the worst case scenario

Solution: Require a Balance Condition that1. ensures depth is O(log n) – strong enough!2. is easy to maintain – not too strong!

30

Potential Balance Conditions1. Left and right subtrees of the root

have equal number of nodes

2. Left and right subtrees of the roothave equal height

31

Potential Balance Conditions3. Left and right subtrees of every node

have equal number of nodes

4. Left and right subtrees of every nodehave equal height

32

The AVL Balance ConditionLeft and right subtrees of every nodehave equal heights differing by at most 1

Define: balance(x) = height(x.left) – height(x.right)

AVL property: –1 balance(x) 1, for every node x

• Ensures small depth– Will prove this by showing that an AVL tree of height

h must have a lot of (i.e. O(2h)) nodes• Easy to maintain

– Using single and double rotations

33

The AVL Tree Data Structure

4

121062

115

8

14137 9

Structural properties

1. Binary tree property

2. Balance property:balance of every node isbetween -1 and 1

Result:

Worst case depth isO(log n)

Ordering property– Same as for BST

15