1 / 24

Lecture 14: Trees & Insertion Sort

CompSci 105 SS 2006 Principles of Computer Science. Lecture 14: Trees & Insertion Sort. Insertion Sort. A Tree. Terminology. A. B. C. D. E. F. G. H. I. A. B. C. D. E. F. G. H. I. Recurisve Definition. C is a subtree of one node. D, H & I form a subtree of three nodes.

snoddy
Download Presentation

Lecture 14: Trees & Insertion Sort

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 2006 Principles of Computer Science Lecture 14: Trees & Insertion Sort

  2. Insertion Sort

  3. A Tree

  4. Terminology A B C D E F G H I

  5. A B C D E F G H I Recurisve Definition C is a subtree of one node D, H & I form a subtree of three nodes

  6. A B C D E F G H I Each piece of data in the tree is referred to as a node They are connected by edges A is the root of the tree E, F, G, C, H & I all have no children and are therefore called leaves of the tree

  7. A B C D E F G H I B, C and D are children of A A is their parent B, C and D are siblings Level 1 Level 2 Level 3 We refer to each level of the tree numerically This tree has a height of 3

  8. A B C D E F G H I A is the ancestor of every node in this tree, whereas B is the ancestor of only E, F and G Every node in the tree is descendant of A. E, F & G are descendants of both B and A

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

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

  11. Binary Trees

  12. 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

  13. 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

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

  15. Binary Tree ADT TreeNode createBinaryTree( ) Object getRootItem( ) TreeNode getLeft ( ) TreeNode getRight ( ) setLeft ( TreeNode ) setRight ( TreeNode ) setRootItem( Object ) B A C

  16. What is the output? void printTree( TreeNode root) if ( root != null ) println( root.getRootItem()); printTree( getLeft () ); printTree( getRight() ); } A B D G H I

  17. Sorted List ADT 0 1 2 3 4 5 6 7 A B E J N A E N B J head

  18. 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 Nodes in right subtree have keys > the root’s key. K >K <K

  19. Searching M B Q J O U

  20. Search( TreeNode root, Key searchKey) { if ( root==null ) // item not found else if ( root.getKey == searchKey ) // item is found! else // search in appropriate subtree if ( searchKey < root.getKey()) // search in left subtree else // search in right subtree }

  21. Add as a leaf M B Q J O U

  22. public TreeNode insertItem ( TreeNode root, Object item ) { if ( root==null ) // add to root of tree else { // add to appropriate subtree if ( item.getKey() < root.getKey()) // add to left subtree else // add to right subtree } return root; }

  23. Exercises • Draw the BST that results from inserting the values 4, 3, 2, 7, 5, 6 in order. • Draw as many different BST’s as you can think of with values A, B, and C. B C A C B A

  24. // 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