Download presentation
Presentation is loading. Please wait.
Published byPreston Marshall Modified over 9 years ago
1
1 Lecture 14: Trees & Insertion Sort CompSci 105 SS 2006 Principles of Computer Science
2
2 Insertion Sort
3
3 A Tree
4
4 Terminology A BDC EGFIH
5
5 Recurisve Definition A BDC EGFIH C is a subtree of one node D, H & I form a subtree of three nodes
6
6 A BDC EGFIH Each piece of data in the tree is referred to as a node A is the root of the tree They are connected by edges E, F, G, C, H & I all have no children and are therefore called leaves of the tree
7
7 A BDC EGFIH Level 1 Level 2 Level 3 B, C and D are children of A A is their parent We refer to each level of the tree numerically B, C and D are siblings This tree has a height of 3
8
8 A BDC EGFIH 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
9 Node: Subtree References public class TreeNode { Object item; TreeNode[] subTrees; } A BDC EGFIH
10
10 Node: First Child Next Sibling public class TreeNode { Object item; TreeNode firstChild; TreeNode nextSibling; } A BDC EGFIH
11
11 Binary Trees
12
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
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
14 Binary Tree Node (Ref based) public class TreeNode { Object item; TreeNode left; TreeNode right; }
15
15 Binary Tree ADT TreeNode createBinaryTree( ) Object getRootItem( ) TreeNode getLeft ( ) TreeNode getRight ( ) setLeft ( TreeNode ) setRight ( TreeNode ) setRootItem( Object ) B AC
16
16 What is the output? void printTree( TreeNode root) if ( root != null ) println( root.getRootItem()); printTree( getLeft () ); printTree( getRight() ); } A BD GIH
17
17 Sorted List ADT EBAJ 0123 N 4567 A B N head E J
18
18 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. Binary Search Trees (BSTs) K >K <K
19
19 Searching M BQ JUO
20
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
21 Add as a leaf M BQ JUO
22
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
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 CA C B A
24
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 }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.