27
1 Lecture 21: Binary Search Tree CompSci 105 SS 2006 Principles of Computer Science

Lecture 21: Binary Search Tree

Embed Size (px)

DESCRIPTION

CompSci 105 SS 2006 Principles of Computer Science. Lecture 21: Binary Search Tree. Binary Search Trees (BSTs). As with a Binary Tree, except Root node has a special data element called a key Nodes in left subtree have keys < the root’s key - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 21: Binary Search Tree

1

Lecture 21: Binary Search Tree

CompSci 105 SS 2006

Principles of Computer Science

Page 2: Lecture 21: Binary Search Tree

2

As with a Binary Tree, except

Root node has a special data element called a key

Nodes in left subtree have keys < the root’s key

Nodes in right subtree have keys > the root’s key.

Binary Search Trees (BSTs)

K

>K<K

Page 3: Lecture 21: Binary Search Tree

3

Searching

M

B Q

J UO

Page 4: Lecture 21: Binary Search Tree

4

Add as a leaf

M

B Q

J UO

Page 5: Lecture 21: Binary Search Tree

5• Draw the BST that results from inserting the values 4, 3, 2, 7, 5, 6 in order.

• Draw as many different BST’s shapes as possible with nodes A, B, and C.

B

CA

C

B

A

C

A

B

A

B

C

A

C

B

4

3

2

7

5

6

Page 6: Lecture 21: Binary Search Tree

6

Inorder Traversalvoid printTree( TreeNode root)

if ( root != null )

printTree( root.getLeft () );

println(root.getRootItem());

printTree( root.getRight() );

}M

B Q

J UO

Page 7: Lecture 21: Binary Search Tree

7

Inorder Traversalvoid printTree( TreeNode root)

if ( root != null )

printTree( root.getLeft () );

println(root.getRootItem());

printTree( root.getRight() );

}

B

CA

C

B

A

C

A

B

A

B

C

A

C

B

Page 8: Lecture 21: Binary Search Tree

8

Preorder Traversalvoid printTree( TreeNode root)

if ( root != null )

println( root.getRootItem());

printTree( root.getLeft () );

printTree( root.getRight() );

}M

B Q

J UO

Page 9: Lecture 21: Binary Search Tree

9

Postorder Traversalvoid printTree( TreeNode root)

if ( root != null )

printTree( root.getLeft () );printTree( root.getRight() );

println(root.getRootItem());

}M

B Q

J UO

Page 10: Lecture 21: Binary Search Tree

10

Exercise

• For each of the trees below, what is the preorder traversal? What is the postorder traversal?

B

CA

C

B

A

C

A

B

A

B

C

A

C

B

Page 11: Lecture 21: Binary Search Tree

11

Level Order Traversal

• Difficult

• print nodes as if reading tree from a book (left to right, top to bottom)

• Use a queue to keep track...”homework”M

B Q

J UO

Page 12: Lecture 21: Binary Search Tree

12

BST Delete

M

B Q

J UO

Page 13: Lecture 21: Binary Search Tree

13

Deleting the root

M M

B

J

M

Q

UO

M

B Q

UOJ

root root root root

No children

No rightchild

No leftchild

Twochildren

Page 14: Lecture 21: Binary Search Tree

14

Exercise

• Draw the BST that results from inserting the values D, C, G, B, E, F in order. Now draw the result of deleting the root.

Page 15: Lecture 21: Binary Search Tree

15

BST Efficiency

M

B Q

J UO

Page 16: Lecture 21: Binary Search Tree

16

BST Efficiency

M

B Q

J UO

U

Q

O

M

J

B

Page 17: Lecture 21: Binary Search Tree

17

Average Case?

M

B Q

J UO

U

Q

O

M

J

B

Page 18: Lecture 21: Binary Search Tree

18

Full and Complete Trees

M

Q

UO

G

JB

A full tree

Page 19: Lecture 21: Binary Search Tree

19

Full and Complete Trees

M

Q

UO

G

JB

M

QG

B

A full tree A complete tree

Page 20: Lecture 21: Binary Search Tree

20

Minimising Tree Height

M

B Q

OJA

Page 21: Lecture 21: Binary Search Tree

21

Priority Queue Operations

void create ()

boolean isEmpty()

voidinsert( item)

item delete()

To do:

1. Study for 105 Exam

2. Play

3. Eat

4. Sleep

Textbook, p. 518

Page 22: Lecture 21: Binary Search Tree

22

Heap

Like a binary search tree, but

• Always keep it a complete tree

• Don’t completely sort data … just make sure that each node’s key is bigger than its children’s keys.

Textbook, p. 520

Page 23: Lecture 21: Binary Search Tree

23

ADT Table

UnsortedArray

SortedArray

UnsortedLinked List

SortedLinked List

BinarySearch Tree

ADTPriorityQueue

Program that uses a priority

queue

Page 24: Lecture 21: Binary Search Tree

24

M

Q

U

G

JB

BST heap

BST’s vs Heaps

U

JQ

MB G

Page 25: Lecture 21: Binary Search Tree

25

Heap

• A complete binary tree

• Each node’s key is bigger than its children’s keys.

Textbook, p. 520

U

JQ

MB G

Maxheap vs. minheap

Page 26: Lecture 21: Binary Search Tree

26

Which are maxheaps?M

Q

UO

G

JB

8

5

4

7

21

M

J

C

G

BA

J

EC

DA

Page 27: Lecture 21: Binary Search Tree

27

M

Q

U

G

JB

BST heap

BST’s vs Heaps

U

JQ

MB G