1 / 38

Lecture 19: Trees

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.

asha
Download Presentation

Lecture 19: Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Revision - Sorting O(2n) O(n3) Selection Sort O(n2) Merge Sort O(n log n) Faster Code O(n) O(log n) O(1)

  3. 0 1 2 3 4 5 6 7 C O M P U T E R 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 ) } Real Java Code, Textbook, p. 394-396

  4. 4 2 2 2 1 1 1 1 1 1 Revisision - Merge Sort 8 items 4 2 1 1 Analysis, Textbook, p. 393-398

  5. Quick Sort Algorithm Analysis Trees Introduction General Tree Structures Binary Trees Reference-Based Implementation

  6. Revision - Partitioning (as seen in L8) ≥p p <p 0 1 2 3 4 8 3 9 1 7 3 1 7 8 9 Textbook, p. 399

  7. Quicksort ≥p p <p Textbook, p. 399

  8. 0 1 2 3 4 5 6 7 C O M P U T E R Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

  9. 0 1 2 3 4 5 6 7 C O M P U T E R 0 1 2 3 4 5 6 7 C O M P U T E R Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

  10. 0 1 2 3 4 5 6 7 C O M P U T E R 0 1 2 3 4 5 6 7 C O M P U T E R 1 2 3 4 5 6 7 E M O U T E R Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

  11. 0 1 2 3 4 5 6 7 C O M P U T E R 0 1 2 3 4 5 6 7 C O M P U T E R 1 2 3 4 5 6 7 E M O U T E R 1 2 4 5 6 7 E M T E R U Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

  12. 0 1 2 3 4 5 6 7 C O M P U T E R 0 1 2 3 4 5 6 7 C O M P U T E R 1 2 3 4 5 6 7 E M O U T E R 1 2 4 5 6 7 E M T E R U 4 5 6 2 T E R M Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

  13. 0 1 2 3 4 5 6 7 C O M P U T E R 0 1 2 3 4 5 6 7 C O M P U T E R 1 2 3 4 5 6 7 E M O U T P R 1 2 4 5 6 7 E M T P R U 4 5 6 2 T P R M 4 5 P T Java Code, Textbook, pp. 405-407, Description, Textbook, pp. 398-400

  14. Quicksort 0 1 2 3 4 5 6 7 C O M P U T E R C O M P U T E R C E M O U T P R C E M O T P R U C E M O T P T R C E M O P T U R C E M O P T U R Textbook, pp. 398-400

  15. Quicksort Complexity 0 1 2 3 4 5 6 7 A B C D E F G H Textbook, pp. 408-410

  16. Partitioning O M P U T E R Textbook, p. 401-404

  17. Partitioning O M P U T E R O M P U T E R Textbook, p. 401-404

  18. Partitioning O M P U T E R O M P U T E R Textbook, p. 401-404

  19. Partitioning O M P U T E R O M P U T E R O M P U T E R Textbook, p. 401-404

  20. Partitioning O M P U T E R O M P U T E R O M P U T E R O M P U T E R Textbook, p. 401-404

  21. Partitioning O M P U T E R O M P U T E R O M P U T E R O M P U T E R O M E U T P R Textbook, p. 401-404

  22. Partitioning O M P U T E R O M P U T E R O M P U T E R O M P U T E R O M E U T P R O M E U T P R Textbook, p. 401-404

  23. Partitioning O M P U T E R O M P U T E R O M P U T E R O M P U T E R O M E U T P R O M E U T P R E M O U T P R Textbook, p. 401-404

  24. Mergesort: Efficiency: Quicksort vs. Mergesort Quicksort:

  25. Sorting O(n log n) O(n2) Merge Sort Selection Sort Quicksort (Worst) Quicksort (Average)

  26. Comparing Sorting Algorithms Quicksort Merge Sort Bubble Sort Selection Sort Insertion Sort

  27. Quick Sort Algorithm Analysis Trees Introduction General Tree Structures Binary Trees Reference-Based Implementation

  28. A Tree

  29. Nodes Edges Root Leaf Parent Child Siblings Anscestor Descendant Subtrees Height Terminology A B C D E F G H I Textbook, p. 423

  30. A B C D E F G H I Recurisve Definition A tree is a root node attached to a set of trees

  31. A B C D E F G H I Node: Subtree References public class TreeNode { Object item; TreeNode[] subTrees; }

  32. Node: First Child Next Sibling public class TreeNode { Object item; TreeNode firstChild; TreeNode nextSibling; } A B C D E F G H I

  33. Binary Trees Textbook, p. 423-4

  34. Binary Trees 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 Textbook, p. 423-4

  35. Binary Trees 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 Textbook, p. 423-4

  36. Binary Tree Node (Ref based) public class TreeNode { Object item; TreeNode left; TreeNode right; }

  37. Binary Tree ADT TreeNode createBinaryTree( ) Object getRootItem( ) TreeNode getLeft ( ) TreeNode getRight ( ) setLeft ( TreeNode ) setRight ( TreeNode ) setRootItem( Object ) B A C Alternative definition, Textbook, p. 430-431

  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  }

More Related