Upload
thetis
View
28
Download
0
Embed Size (px)
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 Presentation
Citation preview
1
Lecture 20: Binary Search Trees
Lecturer: Santokh Singh
CompSci 105 SS 2005
Principles of Computer Science
2
Recurisve Definition
A
B DC
E GF IH
A tree is a root node attached to a set of trees
3
Binary Trees
Textbook, p. 423-4
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
5
Binary Tree Node (Ref based)public class TreeNode {
Object item;
TreeNode left;
TreeNode right;
}
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
7
What is the output?void printTree( TreeNode root)
if ( root != null )
println( root.getRootItem());
printTree( getLeft () );
printTree( getRight() );
}A
B D
G IH
8
Sorted List ADT
EBA J0 1 2 3
N 4 5 6 7
A B N
head
E J
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
10
Searching
M
B Q
J UO
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
12
Add as a leaf
M
B Q
J UO
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
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
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.
}…}