1 / 15

Lecture 20: Binary Search Trees

CompSci 105 SS 2005 Principles of Computer Science. Lecture 20: Binary Search Trees. Lecturer: Santokh Singh. A. B. C. D. E. F. G. H. I. Recurisve Definition. A tree is a root node attached to a set of tree s. Binary Trees. Textbook, p. 423-4. Binary Trees.

thetis
Download Presentation

Lecture 20: Binary Search 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 20: Binary Search Trees Lecturer: Santokh Singh

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

  3. Binary Trees Textbook, p. 423-4

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

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

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

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

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

  9. 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 Textbook, p. 423-4

  10. Searching M B Q J O U

  11. 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 } Textbook, p. 459-460

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

  13. 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; } Textbook, p. 459-460

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

  15. // Revision - some important sections about Applets and Assignment 3 import java.awt.*; import java.awt.event.*; import java.applet.*; import structure.BinaryTreeNode; import java.util.*; public class Expression extends Applet implements ActionListener{//discussded 12 days ago!! public void init(){ //set left-aligned flow layout setLayout(new FlowLayout(FlowLayout.LEFT)); /* Initialise the Buttons and TextFields here. …. */ …. }//end of init method public void actionPerformed(ActionEvent e){ …. repaint(); } } public void paint(Graphics g){ /* If the binary tree data structure (BinaryTreeNode - discussed on the day before yesterday) is not null then call the draw method. */ // draw method() - discussed yesterday. } … }

More Related