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

33
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    213
  • download

    0

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

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

CSE 326: Data StructuresBinary Search Trees

Ben Lerner

Summer 2007

Page 2: CSE 326: Data Structures Binary 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?

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

3

Tree Calculations Example

A

E

B

D F

C

G

IH

KJ L

M

L

N

How high is this tree?

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

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

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

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

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

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

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

7

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

– max # of leaves:

– max # of nodes:

– min # of leaves:

– min # of nodes:

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

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)

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

9

Traversals

void traverse(BNode t){

if (t != NULL)

traverse (t.left);

print t.element;

traverse (t.right);

}

} Which kind of traversal is this?

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

10

ADTs Seen So Far

• Stack– Push– Pop

• Queue– Enqueue– Dequeue

What about decreaseKey?

• Priority Queue– Insert– DeleteMin

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

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, …

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

12

A Modest Few Uses

• Sets

• Dictionaries

• Networks : Router tables

• Operating systems : Page tables

• Compilers : Symbol tables

Probably the most widely used ADT!

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

13

Implementations

• Unsorted Linked-list

• Unsorted array

• Sorted array

insert deletefind

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

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?

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

15

Example and Counter-Example

3

1171

84

5

4

181062

115

8

20

21BINARY SEARCH TREE NOT ABINARY SEARCH TREE

7

15

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

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:

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

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:

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

18

Insert in BST

2092

155

10

307 17

Runtime:

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

Insertions happen only at the leaves – easy!

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

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.

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

20

Bonus: FindMin/FindMax

• Find minimum

• Find maximum2092

155

10

307 17

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

21

Deletion in BST

2092

155

10

307 17

Why might deletion be harder than insertion?

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

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

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

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

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

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

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

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

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

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

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

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

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

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?

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

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!

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

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

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

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

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

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

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

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