Binary Search Tree
Binary Search Tree
Search Tree
> Symbol Table
> Traverse (Inorder, Preorder,
Postorder)
Department: Computer science
Course Code: CSS-215
Course Instructor: Asst. Prof. Dr. Mohammed
Ala’anzy
Office no.: G-405
SYMBOL TABLES
API
Elementary implementations
Ordered operations
API
Definition: A symbol table is a structured data container designed for key-value pairs,
specifically tailored to provide support for two core operations. It allows for the
insertion (put) of new key-value pairs into the table and facilitates the retrieval (get) of
the associated value based on a specified key.
API: stands for "Application Programming Interface." It is a set of rules and protocols
that allows different software applications to communicate with each other. APIs
define the methods and data formats that applications can use to request and exchange
information, enabling them to work together and share data seamlessly. APIs are
essential for integrating various software components, services, and systems, making it
easier for developers to build and extend applications.
Symbol tables
Key-value pair abstraction.
Insert a value with specified key.
Given a key, search for the corresponding domain name IP address
value. www.cs.princeton.edu 128.112.136.11
www.princeton.edu 128.112.128.15
Ex. DNS lookup. www.yale.edu 130.132.143.21
Insert domain name with specified IP www.harvard.edu 128.103.060.55
www.simpsons.com 209.052.165.60
address.
Given domain name, find corresponding IP
address. Key Value
Symbol table applications
Application Purpose of search Key Value
Dictionary Find definition Word Definition
Book index Find relevant pages Term List of page numbers
File share Find sound to download Name of sound Computer ID
Financial account Process transactions Account number Transaction details
Web search Find relevant web pages Keyword List of page names
Compiler Find properties of variables Variable name Type and value
Routing table Route internet packets Destination Best route
Dns Find IP address Domain name IP address
Key Value
Basic symbol table API
Associative array abstraction. Associate one value with each key.
public class ST<Key, Value>
ST() create a symbol table
void put(Key key, Value val) put key-value pair into the table
(remove key from table if value is null)
Value get(Key key) value paired with key (null if key is absent)
void delete(Key key) remove key (and its value) from table
boolean contains(Key key) is there a value paired with key?
boolean isEmpty () is the table empty?
int size() number of key-value pairs in the table
Iterable<Key> keys() all the keys in the table
Conventions
Values are not null.
Method get() returns null if key not present.
Method put() overwrites old value with new value.
Intended consequences.
Easy to implement contains()
public boolean contains(Key key)
{ return get(key) != null; }
Can implement lazy version of delete().
public void delete(Key key)
{ put(key, null); }
SYMBOL TABLES
API
Elementary implementations
Ordered operations
Sequential search in a linked list
Data structure. Maintain an (unordered) linked list of key-value pairs.
Search. Scan through all keys until find a match.
Insert. Scan through all keys until find a match; if no match add to front.
Elementary ST implementations: summary
Worst-case cost Average case
Implementation (After N inserts) (After N random inserts)
Search Insert Search hit Insert
Sequential search
N N N/2 N
(Unordered list)
entry in Yellow is
loop exits with keys[m] = P: return 6 a[m]
Trace of binary search for rank in an ordered
array
Search N Log N
Insert/Delete N N
Min/Max N 1
Floor/Ceiling N Log N
Rank N Log N
Select N 1
Ordered
N log N N
iteration
Binary Search Tree
> BST
> Ordered operation
> Deletion
Binary search trees
Definition. A BST is a binary tree in symmetric order.
Binary Tree: 5
A binary tree is a hierarchical data structure in which each node has at most two
children, known as the left child and the right child.
There are no specific rules or constraints on the values of nodes in a binary tree. Nodes 3 8
can have any values and can be arranged in any way.
Binary trees can have unbalanced structures, which means they may not be efficient for 1 9
certain operations like searching or sorting, as the structure depends on how nodes are
inserted.
Binary Tree & Binary Search Tree
Binary Search Tree (BST):
A binary search tree is a specific type of binary tree that has additional
rules and constraints on the values of its nodes. In a BST, for each node:
All nodes in the left subtree have values less than the current node's
5
value.
All nodes in the right subtree have values greater than the current node's
value. 3 8
These rules ensure that a BST maintains a sorted order, making it
suitable for efficient searching and sorting operations. 1 4
A properly balanced BST has a height of O(log n), making search,
insertion, and deletion operations very efficient.
Binary Tree & Binary Search Tree
In the binary tree, there are no specific rules governing the
values of nodes, so it's just a general tree structure. In the
binary search tree, the nodes are organized in a way that
satisfies the binary search property, making it suitable for
searching and maintaining sorted data.
BST representation in Java
private class Node{
Java definition. A BST is a reference to a root Node. private Key key;
private Value val;
A Node is comprised of four fields: private Node left, right;
public Node(Key key, Value val){
A Key and a Value. this.key = key;
this.val = val;
A reference to the left and right subtree. }
}
//Key and Value are generic types; Key is
Comparable
Smaller Larger keys
keys
Binary search tree demo
Search. If less, go left; if greater, go right; if equal, search hit.
S
Search for H
E X
A R
C H
M
Binary search tree demo
Search. If less, go left; if greater, go right; if equal, search hit.
Compare H with S (go left)
S
Search for H
E X
A R
C H
M
Binary search tree demo
Search. If less, go left; if greater, go right; if equal, search hit.
S
Search for H
Compare H with E (go
right)
E X
A R
C H
M
Binary search tree demo
Search. If less, go left; if greater, go right; if equal, search hit.
S
Search for H
E X
C H
M
Binary search tree demo
Search. If less, go left; if greater, go right; if equal, search hit.
S
Successful
search for H
E X
A R
M
Binary search tree demo
Insert. If less, go left; if greater, go right; if null, insert.
Compare S and G (go
left)
S
Insert for G
E X
A R
C H
M
Binary search tree demo
Insert. If less, go left; if greater, go right; if null, insert.
S
Insert for G
Compare E and G (go
right)
E X
A R
C H
M
Binary search tree demo
Insert. If less, go left; if greater, go right; if null, insert.
S
Insert for G
E X
C H
M
Binary search tree demo
Insert. If less, go left; if greater, go right; if null, insert.
S
Insert for G
E X
A R
Compare H
and G (go
C left) H
M
Binary search tree demo
Insert. If less, go left; if greater, go right; if null, insert.
S
Insert for G
E X
A R
C H
best case
H
typical case
S A worst case
C
C S E X
E
A E R X A R H
R
Remark. Tree shape depends on C H
order of insertion.
BST insertion: random order
Ex. Insert keys in random order.
ST implementations: summary
Worst-case cost Average case
Implementation (After N inserts) (After N random inserts)
Search Insert Search hit Insert
Sequential search
(Unordered list) N N N/2 N
binary search
(ordered array) log N N log N N/2
BST N N 1.39 log N 1.39 log N
Binary Search Tree
> BST
> Ordered operation
> Deletion
Binary Search Tree
> BST
> Ordered operation
> Deletion
Minimum and maximum
Minimum. Smallest key in table.
Maximum. Largest key in table.
C S Max
Min
A E X
Q. How to find the min / max?
Floor and ceiling
Floor. Largest key ≤ a given key.
Ceiling. Smallest key ≥ a given key.
S
Floor(G)
E X
A R Ceiling (Q)
C H
Tree Traversal Techniques
A Tree Data Structure can be traversed in following ways:
In the case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order. To get nodes of BST in non-increasing order, a variation of Inorder
traversal where Inorder traversal is reversed can be used.
Pseudocode representation performs a
Inorder traversal
// Define a class to represent a node in the // Function to perform a inorder traversal of
binary tree the binary tree
function printInorder(node)
class Node
int key
// If the current node is null, return
Node left, right if node is null
return
// Constructor to initialize a node with a key
Node(int item) // Recursively traverse the left subtree
key = item call printInorder(node.left)
left = right = null
Uses of Preorder:
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used
to get prefix expressions on an expression tree.
Pseudocode representation performs a
preorder traversal
// Define a class to represent a node in the // Function to perform a preorder traversal of
binary tree the binary tree
function printPreorder(node)
class Node
int key
// If the current node is null, return
Node left, right if node is null
return
// Constructor to initialize a node with a key
Node(int item) // Print the key value of the current node
key = item print node.key
left = right = null
// Recursively traverse the left subtree
// Define a class to represent a binary tree
class BinaryTree call printPreorder(node.left)
Node root
// Recursively traverse the right subtree
// Constructor to initialize an empty binary tree call printPreorder(node.right)
BinaryTree()
root = null
Postorder Traversal
Traverse the left subtree.
Traverse the right subtree.
Visit the root
// Constructor to initialize an empty binary tree // Print the key value of the current node
print node.key
BinaryTree()
root = null
Binary Search Tree
> BST
> Ordered operation
> Deletion
Binary Search Tree
> BST
> Ordered operation
> Deletion (Next lecture)
BST deletion: lazy approach
To remove a node with a given key:
Set its value to null.
Leave the key in the tree to guide the search (but don't consider it equal in the
search).
Removing a node with a given key
E
Implementing Deletion A S
for Node I
C ☻
I Tombstone
H R
N
Cost. ~ 2 ln N' per insert, search, and delete (if keys in random order), where N' is the number of
key-value pairs ever inserted in the BST. Unsatisfactory solution. Tombstone (memory) overload.
Deleting the minimum
To delete the minimum key:
Go left until you find a node with a null left link. public void deleteMin() {
root = deleteMin(root);
Replace that node with its right link.
}
Update subtree counts. private Node deleteMin(Node x) {
S if (x.left == null)
return x.right;
x.left = deleteMin(x.left);
E X x.count = 1 + size(x.left) + size(x.right);
return x;
}
min A R
C H
Hibbard deletion
To delete a node with key k: search for node t containing key k.
Case 0. [0 children] Delete t by setting the parent link to null.
S
Deleting C
E X
Available for garbage
collection A R
C H
Hibbard deletion
To delete a node with key k: search for node t containing key k.
Case 1. [1 child] Delete t by replacing parent link.
S
Deleting R
A R
C H
Hibbard deletion
To delete a node with key k: search for node t containing key k.
Case 2. [1 children]
Find successor x of t.
Delete the minimum in t's right subtree.
S
Put x in t's spot.
E X
Deleting E
A R
C H
M
Hibbard deletion
To delete a node with key k: search for node t containing key k.
Case 2. [1 children]
Find successor x of t.
Delete the minimum in t's right subtree.
S
Put x in t's spot.
E X
Deleting E
x Go right, then go
A R left until reaching
null left link
C H successor min(t.right)
M
Hibbard deletion
To delete a node with key k: search for node t containing key k.
Case 2. [1 children]
Find successor x of t.
Delete the minimum in t's right subtree.
S
Put x in t's spot.
Update links
E X
Deleting E
A R
C H
M
Hibbard deletion
To delete a node with key k: search for node t containing key k.
Case 2. [1 children]
Find successor x of t.
Delete the minimum in t's right subtree.
S
Put x in t's spot.
H X
Deleting E while it is still BST
A R
C M
Hibbard deletion: Java implementation
public void delete(Key key) {
root = delete(root, key);
}
private Node delete(Node x, Key key) {
if (x == null)
return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = delete(x.left, key); //search for key
else if (cmp > 0) x.right = delete(x.right, key);
else {
if (x.right == null) // no right child
return x.left;
if (x.left == null) //no left child
return x.right;
Node t = x; x = min(t.right); x.right = deleteMin(t.right); x.left = t.left; //replace with
successor
}
x.count = size(x.left) + size(x.right) + 1; //update subtree counts
Thank you!