lecture 20: binary search trees
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/1.jpg)
1
Lecture 20: Binary Search Trees
Lecturer: Santokh Singh
CompSci 105 SS 2005
Principles of Computer Science
![Page 2: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/2.jpg)
2
Recurisve Definition
A
B DC
E GF IH
A tree is a root node attached to a set of trees
![Page 3: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/3.jpg)
3
Binary Trees
Textbook, p. 423-4
![Page 4: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/4.jpg)
4
Binary Trees
Textbook, p. 423-4
A binary tree is eitherempty
or is a root node storing an item attached to
a binary tree called the left subtreeand
a binary tree called the right subtree
![Page 5: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/5.jpg)
5
Binary Tree Node (Ref based)public class TreeNode {
Object item;
TreeNode left;
TreeNode right;
}
![Page 6: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/6.jpg)
6
Binary Tree ADTTreeNode createBinaryTree( )
Object getRootItem( )
TreeNode getLeft ( )
TreeNode getRight ( )
setLeft ( TreeNode )
setRight ( TreeNode )
setRootItem( Object )
B
A C
Alternative definition, Textbook, p. 430-431
![Page 7: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/7.jpg)
7
What is the output?void printTree( TreeNode root)
if ( root != null )
println( root.getRootItem());
printTree( getLeft () );
printTree( getRight() );
}A
B D
G IH
![Page 8: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/8.jpg)
8
Sorted List ADT
EBA J0 1 2 3
N 4 5 6 7
A B N
head
E J
![Page 9: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/9.jpg)
9
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
Textbook, p. 423-4
>K<K
![Page 10: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/10.jpg)
10
Searching
M
B Q
J UO
![Page 11: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/11.jpg)
11Search( 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
![Page 12: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/12.jpg)
12
Add as a leaf
M
B Q
J UO
![Page 13: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/13.jpg)
13public 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
![Page 14: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/14.jpg)
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
CA
C
B
A
![Page 15: Lecture 20: Binary Search Trees](https://reader035.vdocuments.us/reader035/viewer/2022062322/56815201550346895dc041fb/html5/thumbnails/15.jpg)
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 layoutsetLayout(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.
}…}