240 likes | 250 Views
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.
E N D
CompSci 105 SS 2006 Principles of Computer Science Lecture 14: Trees & Insertion Sort
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
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
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
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
A B C D E F G H I Node: Subtree References public class TreeNode { Object item; TreeNode[] subTrees; }
Node: First Child Next Sibling public class TreeNode { Object item; TreeNode firstChild; TreeNode nextSibling; } A B C D E F G H I
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
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
Binary Tree Node (Ref based) public class TreeNode { Object item; TreeNode left; TreeNode right; }
Binary Tree ADT TreeNode createBinaryTree( ) Object getRootItem( ) TreeNode getLeft ( ) TreeNode getRight ( ) setLeft ( TreeNode ) setRight ( TreeNode ) setRootItem( Object ) B A C
What is the output? void printTree( TreeNode root) if ( root != null ) println( root.getRootItem()); printTree( getLeft () ); printTree( getRight() ); } A B D G H I
Sorted List ADT 0 1 2 3 4 5 6 7 A B E J N A E N B J head
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
Searching M B Q J O U
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 }
Add as a leaf M B Q J O U
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; }
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
// 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 }