20
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 22: Efficiency of BST Operations Lecturer: Santokh Singh

CompSci 105 SS 2005 Principles of Computer Science

  • Upload
    lynton

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

CompSci 105 SS 2005 Principles of Computer Science. Lecture 22: Efficiency of BST Operations. Lecturer: Santokh Singh. Inorder Traversal. void printTree( TreeNode root) if ( root != null ) printTree( root.getLeft () ); println(root.getRootItem()); printTree( root.getRight() ); }. - PowerPoint PPT Presentation

Citation preview

Page 1: CompSci 105 SS 2005 Principles of Computer Science

1CompSci 105 SS 2005

Principles of Computer Science

Lecture 22: Efficiency of BST Operations

Lecturer: Santokh Singh

Page 2: CompSci 105 SS 2005 Principles of Computer Science

2

Inorder Traversalvoid printTree( TreeNode root)

if ( root != null )

printTree( root.getLeft () );

println(root.getRootItem());

printTree( root.getRight() );

}

Textbook, p. 434

M

B Q

J UO

Page 3: CompSci 105 SS 2005 Principles of Computer Science

3

Deleting the root

M M

B

J

M

Q

UO

M

B Q

UOJ

root root root root

No children

No rightchild

No leftchild

TwochildrenTextbook, p. 462

Page 4: CompSci 105 SS 2005 Principles of Computer Science

4

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 5: CompSci 105 SS 2005 Principles of Computer Science

5

Treesort

Please refer to the prescribed Textbook, pages 478-479, now.

Page 6: CompSci 105 SS 2005 Principles of Computer Science

6

ADT Table

City Country Population

Auckland New Zealand

1,300,000

London United Kingdom

9,400,000

Shanghai China 13,053,000

Textbook, p. 498-499

Page 7: CompSci 105 SS 2005 Principles of Computer Science

7

ADT Table Operations

void create ()

int length()

void insert( item)

void delete( key )

item find(key)

City Country Population

Auckland New Zealand

1,300,000

London United Kingdom

9,400,000

Shanghai China 13,053,000

Textbook, p. 498-499

Page 8: CompSci 105 SS 2005 Principles of Computer Science

8

ADT Table

UnsortedArray

SortedArray

UnsortedLinked List

SortedLinked List

BinarySearch Tree

ADT Table

Program that

uses a table

Textbook, p. 504-517

Page 9: CompSci 105 SS 2005 Principles of Computer Science

9

BST Efficiency

M

B Q

J UO

Textbook, p. 474-478

Page 10: CompSci 105 SS 2005 Principles of Computer Science

10

BST Efficiency

M

B Q

J UO

U

Q

O

M

J

B

Textbook, p. 474-478

Page 11: CompSci 105 SS 2005 Principles of Computer Science

11

Average Case?

M

B Q

J UO

U

Q

O

M

J

B

Textbook, p. 474-478

Page 12: CompSci 105 SS 2005 Principles of Computer Science

12

Full and Complete Trees

M

Q

UO

G

JB

Textbook, p. 427

A full tree

Page 13: CompSci 105 SS 2005 Principles of Computer Science

13

Full and Complete Trees

M

Q

UO

G

JB

M

QG

B

Textbook, p. 427

A full tree A complete tree

Page 14: CompSci 105 SS 2005 Principles of Computer Science

14

Minimising Tree Height

Textbook, p. 480-481

M

B Q

OJA

Page 15: CompSci 105 SS 2005 Principles of Computer Science

15

ADT Priority Queue

To do:

1. Study for 105 Exam

2. Play

3. Eat

4. Sleep

Textbook, p. 517

Page 16: CompSci 105 SS 2005 Principles of Computer Science

16

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 17: CompSci 105 SS 2005 Principles of Computer Science

17

ADT Table

UnsortedArray

SortedArray

UnsortedLinked List

SortedLinked List

BinarySearch Tree

ADTPriorityQueue

Program that uses a priority

queue

Page 18: CompSci 105 SS 2005 Principles of Computer Science

18

Heap

Like a binary search tree, but

• Always keep it a complete tree

• Don’t completely sort data

Textbook, p. 520

Page 19: CompSci 105 SS 2005 Principles of Computer Science

19

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 20: CompSci 105 SS 2005 Principles of Computer Science

20

M

Q

U

G

JB

BST heap

BST’s vs Heaps

U

JQ

MB G