Download presentation
Presentation is loading. Please wait.
Published byDominick Brown Modified over 9 years ago
1
1 Lecture 19: Trees Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science
2
2 Revision - Sorting O(n)O(n) Faster Code O(n2)O(n2) O(n3)O(n3) O(n log n) O(log n) O(1) O(2n)O(2n) Merge Sort Selection Sort
3
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 ) } MOCP 0123 ETUR 4567
4
4 Revisision - Merge Sort Analysis, Textbook, p. 393-398 11 2 11 2 4 11 2 11 2 4 8 items
5
5 Quick Sort Algorithm Analysis Trees Introduction General Tree Structures Binary Trees Reference-Based Implementation
6
6 Revision - Partitioning (as seen in L8) <p p ≥p≥p 93871 01234 71398 Textbook, p. 399
7
7 Quicksort <p p ≥p≥p Textbook, p. 399
8
8 MOCP 0123 ETUR 4567 Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400
9
9 MOCP 0123 ETUR 4567 MOCP 0123 ETUR 4567
10
10 MOCP 0123 ETUR 4567 Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400 MOCP 0123 ETUR 4567 MEO 123 ETUR 4567
11
11 MOCP 0123 ETUR 4567 Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400 MOCP 0123 ETUR 4567 MEO 123 ETUR 4567 ME 12 RETU 4567
12
12 MOCP 0123 ETUR 4567 Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400 MOCP 0123 ETUR 4567 MEO 123 ETUR 4567 ME 12 M 2 RETU 4567 RET 456
13
13 MOCP 0123 ETUR 4567 Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400 MOCP 0123 ETUR 4567 MEO 123 PTUR 4567 ME 12 M 2 RPTU 4567 RPT 456 TP 45
14
14 Quicksort MOCP 0123 ETUR 4567 MOCPETUR MECOPTUR MECORPTU MECOTPTR Textbook, pp. 398-400 MECOUTPR MECOUTPR
15
15 Quicksort Complexity CBAD 0123 GFEH 4567 Textbook, pp. 408-410
16
16 Partitioning Textbook, p. 401-404 MOPETUR
17
17 Partitioning Textbook, p. 401-404 MOPETUR MOPETUR
18
18 Partitioning Textbook, p. 401-404 MOPETUR MOPETUR
19
19 Partitioning Textbook, p. 401-404 MOPETUR MOPETUR MOPETUR
20
20 Partitioning Textbook, p. 401-404 MOPETUR MOPETUR MOPETUR MOPETUR
21
21 Partitioning Textbook, p. 401-404 MOPETUR MOPETUR MOPETUR MOPETUR MOEPTUR
22
22 Partitioning Textbook, p. 401-404 MOPETUR MOPETUR MOPETUR MOPETUR MOEPTUR MOEPTUR
23
23 Partitioning Textbook, p. 401-404 MOPETUR MOPETUR MOPETUR MOPETUR MOEPTUR MOEPTUR MEOPTUR
24
24 Mergesort: Quicksort: Efficiency: Quicksort vs. Mergesort
25
25 Sorting O(n2)O(n2) O(n log n) Merge Sort Selection Sort Quicksort (Worst) Quicksort (Average)
26
26 Comparing Sorting Algorithms Merge Sort Bubble Sort Quicksort Selection Sort Insertion Sort
27
27 Quick Sort Algorithm Analysis Trees Introduction General Tree Structures Binary Trees Reference-Based Implementation
28
28 A Tree
29
29 Terminology A BDC EGFIH Textbook, p. 423 Nodes Edges Root Leaf Parent Child Siblings Anscestor Descendant Subtrees Height
30
30 Recurisve Definition A BDC EGFIH A tree is a root node attached to a set of trees
31
31 Node: Subtree References public class TreeNode { Object item; TreeNode[] subTrees; } A BDC EGFIH
32
32 Node: First Child Next Sibling public class TreeNode { Object item; TreeNode firstChild; TreeNode nextSibling; } A BDC EGFIH
33
33 Binary Trees Textbook, p. 423-4
34
34 Binary Trees Textbook, p. 423-4 A binary tree is either empty or is a root node storing an item attached to a binary tree called the left subtree and a binary tree called the right subtree
35
35 Binary Trees Textbook, p. 423-4 A binary tree is either empty or is a root node storing an item attached to a binary tree called the left subtree and a binary tree called the right subtree
36
36 Binary Tree Node (Ref based) public class TreeNode { Object item; TreeNode left; TreeNode right; }
37
37 Binary Tree ADT TreeNode createBinaryTree( ) Object getRootItem( ) TreeNode getLeft ( ) TreeNode getRight ( ) setLeft ( TreeNode ) setRight ( TreeNode ) setRootItem( Object ) B AC Alternative definition, Textbook, p. 430-431
38
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 circle g.fillOval(x-size+1, y-size+1, 2*size-2, 2*size-2); g.setColor(Color….); // Color of values displayed g.drawString(""+e.value(),…, …); // display the value correctly }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.