lecture 19: trees

38
1 Lecture 19: Trees Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science

Upload: asha

Post on 04-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

CompSci 105 SS 2005 Principles of Computer Science. Lecture 19: Trees. Lecturer: Santokh Singh. Revision - Sorting. O ( 2 n ). O ( n 3 ). Selection Sort. O ( n 2 ). Merge Sort. O ( n log n ). Faster Code. O ( n ). O (log n ). O (1). 0. 1. 2. 3. 4. 5. 6. 7. C. O. M. P. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 19: Trees

1

Lecture 19: Trees

Lecturer: Santokh Singh

CompSci 105 SS 2005

Principles of Computer Science

Page 2: Lecture 19: Trees

2

Revision - Sorting

O(n)

FasterCode

O(n2)

O(n3)

O(n log n)

O(log n)

O(1)

O(2n)

Merge Sort

Selection Sort

Page 3: Lecture 19: Trees

3

Real Java Code, Textbook, p. 394-396

mergeSort( theArray, first, last )

if (first < last )

{

mid = (first + last ) / 2

mergesort(theArray, first, mid)

mergesort(theArray, mid+1, last)

merge(theArray(first, mid, last )

}

MOC P0 1 2 3

ETU R4 5 6 7

Page 4: Lecture 19: Trees

4

Revisision - Merge Sort

Analysis, Textbook, p. 393-398

1 1

2

1 1

2

4

1 1

2

1 1

2

4

8 items

Page 5: Lecture 19: Trees

5

Quick Sort

Algorithm

Analysis

Trees

Introduction

General Tree Structures

Binary Trees

Reference-Based Implementation

Page 6: Lecture 19: Trees

6

Revision - Partitioning (as seen in L8)

<p p ≥p

938 710 1 2 3 4

713 98

Textbook, p. 399

Page 7: Lecture 19: Trees

7

Quicksort

<p p ≥p

Textbook, p. 399

Page 8: Lecture 19: Trees

8

MOC P0 1 2 3

ETU R4 5 6 7

Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

Page 9: Lecture 19: Trees

9

MOC P0 1 2 3

ETU R4 5 6 7

Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

MOC P0 1 2 3

ETU R4 5 6 7

Page 10: Lecture 19: Trees

10

MOC P0 1 2 3

ETU R4 5 6 7

Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

MOC P0 1 2 3

ETU R4 5 6 7

ME O1 2 3

ETU R4 5 6 7

Page 11: Lecture 19: Trees

11

MOC P0 1 2 3

ETU R4 5 6 7

Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

MOC P0 1 2 3

ETU R4 5 6 7

ME O1 2 3

ETU R4 5 6 7

ME1 2

RET U4 5 6 7

Page 12: Lecture 19: Trees

12

MOC P0 1 2 3

ETU R4 5 6 7

Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

MOC P0 1 2 3

ETU R4 5 6 7

ME O1 2 3

ETU R4 5 6 7

ME1 2

M2

RET U4 5 6 7

RET4 5 6

Page 13: Lecture 19: Trees

13

MOC P0 1 2 3

ETU R4 5 6 7

Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

MOC P0 1 2 3

ETU R4 5 6 7

ME O1 2 3

PTU R4 5 6 7

ME1 2

M2

RPT U4 5 6 7

RPT4 5 6

TP4 5

Page 14: Lecture 19: Trees

14

Quicksort

MOC P0 1 2 3

ETU R4 5 6 7

MOC P ETU R

MEC O PTU R

MEC O RPT U

MEC O TPT R

Textbook, pp. 398-400

MEC O UTP R

MEC O UTP R

Page 15: Lecture 19: Trees

15

Quicksort Complexity

CBA D0 1 2 3

GFE H4 5 6 7

Textbook, pp. 408-410

Page 16: Lecture 19: Trees

16

Partitioning

Textbook, p. 401-404

MO P ETU R

Page 17: Lecture 19: Trees

17

Partitioning

Textbook, p. 401-404

MO P ETU R

MO P ETU R

Page 18: Lecture 19: Trees

18

Partitioning

Textbook, p. 401-404

MO P ETU R

MO P ETU R

Page 19: Lecture 19: Trees

19

Partitioning

Textbook, p. 401-404

MO P ETU R

MO P ETU R

MO P ETU R

Page 20: Lecture 19: Trees

20

Partitioning

Textbook, p. 401-404

MO P ETU R

MO P ETU R

MO P ETU R

MO P ETU R

Page 21: Lecture 19: Trees

21

Partitioning

Textbook, p. 401-404

MO P ETU R

MO P ETU R

MO P ETU R

MO P ETU R

MO E PTU R

Page 22: Lecture 19: Trees

22

Partitioning

Textbook, p. 401-404

MO P ETU R

MO P ETU R

MO P ETU R

MO P ETU R

MO E PTU R

MO E PTU R

Page 23: Lecture 19: Trees

23

Partitioning

Textbook, p. 401-404

MO P ETU R

MO P ETU R

MO P ETU R

MO P ETU R

MO E PTU R

MO E PTU R

ME O PTU R

Page 24: Lecture 19: Trees

24

Mergesort:

Quicksort:

Efficiency: Quicksort vs. Mergesort

Page 25: Lecture 19: Trees

25

Sorting

O(n2)O(n log n)

Merge Sort Selection Sort

Quicksort (Worst)Quicksort (Average)

Page 26: Lecture 19: Trees

26

Comparing Sorting Algorithms

Merge Sort

Bubble Sort

Quicksort

Selection Sort

Insertion Sort

Page 27: Lecture 19: Trees

27

Quick Sort

Algorithm

Analysis

Trees

Introduction

General Tree Structures

Binary Trees

Reference-Based Implementation

Page 28: Lecture 19: Trees

28

A Tree

Page 29: Lecture 19: Trees

29

Terminology

A

B DC

E GF IH

Textbook, p. 423

NodesEdgesRootLeafParentChildSiblingsAnscestorDescendantSubtrees Height

Page 30: Lecture 19: Trees

30

Recurisve Definition

A

B DC

E GF IH

A tree is a root node attached to a set of trees

Page 31: Lecture 19: Trees

31

Node: Subtree Referencespublic class TreeNode {

Object item;

TreeNode[] subTrees;

}A

B DC

E GF IH

Page 32: Lecture 19: Trees

32

Node: First Child Next Siblingpublic class TreeNode {

Object item;

TreeNode firstChild;

TreeNode nextSibling;

} A

B DC

E GF IH

Page 33: Lecture 19: Trees

33

Binary Trees

Textbook, p. 423-4

Page 34: Lecture 19: Trees

34

Binary Trees

Textbook, p. 423-4

A binary tree is eitherempty

or is a root node storing an item attached to

a binary tree called the left subtreeand

a binary tree called the right subtree

Page 35: Lecture 19: Trees

35

Binary Trees

Textbook, p. 423-4

A binary tree is eitherempty

or is a root node storing an item attached to

a binary tree called the left subtreeand

a binary tree called the right subtree

Page 36: Lecture 19: Trees

36

Binary Tree Node (Ref based)public class TreeNode {

Object item;

TreeNode left;

TreeNode right;

}

Page 37: Lecture 19: Trees

37

Binary Tree ADTTreeNode createBinaryTree( )

Object getRootItem( )

TreeNode getLeft ( )

TreeNode getRight ( )

setLeft ( TreeNode )

setRight ( TreeNode )

setRootItem( Object )

B

A C

Alternative definition, Textbook, p. 430-431

Page 38: Lecture 19: Trees

38// Example of painting beautiful binary trees in java applications:-public void paint(Graphics g){

if(root!= null) draw(1, getWidth()/2, 40,180,80,root, g ); // Recursive method

}public void draw(int order, int x, int y, int xGap, int yGap,BinaryTreeNode

e,Graphics g){if (e.left()!=null){

int leftX = x-xGap; // draws to left now int leftY = // How do we draw child downwards in the application?g.drawLine(x,y,leftX,leftY); // draw the connecting line draw( order+1,leftX, leftY, xGap/2, yGap,e.left(),g); // recursion // int order need not be used – but can be used for depth

}if (e.right()!=null){

// just do similarly for right child now }g.setColor(Color…..); // What circle border color do you like?g.fillOval(x-size, y-size, 2*size, 2*size);g.setColor(Color…..); // Inner color of circleg.fillOval(x-size+1, y-size+1, 2*size-2, 2*size-2);g.setColor(Color….); // Color of values displayedg.drawString(""+e.value(),…, …); // display the value correctly

}