150 likes | 271 Views
This lecture, part of the Principles of Computer Science series, explores Binary Search Trees (BSTs) and their properties. Lecturer Santokh Singh delves into the definition of binary trees and nodes, outlining recursive methods to manipulate tree structures. The lecture covers key operations, including searching and inserting items in a BST, along with relevant code snippets that illustrate these concepts. Exercises are provided to solidify understanding by drawing different BSTs from various sets of values.
E N D
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 trees
Binary Trees Textbook, p. 423-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
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 Alternative definition, Textbook, p. 430-431
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 Textbook, p. 423-4
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 } Textbook, p. 459-460
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; } Textbook, p. 459-460
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
// 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. } … }