0% found this document useful (0 votes)
19 views

Binary Search Tree

The document discusses binary search trees (BSTs). A BST is a binary tree where each node's key is larger than all keys in its left subtree and smaller than all keys in its right subtree. This ordering property allows BSTs to efficiently support search, minimum/maximum, floor/ceiling, rank, and select operations in O(log N) time by doing binary search. Insertion and deletion in a BST have O(N) worst-case time complexity due to potential tree restructuring.

Uploaded by

dastanktl26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Binary Search Tree

The document discusses binary search trees (BSTs). A BST is a binary tree where each node's key is larger than all keys in its left subtree and smaller than all keys in its right subtree. This ordering property allows BSTs to efficiently support search, minimum/maximum, floor/ceiling, rank, and select operations in O(log N) time by doing binary search. Insertion and deletion in a BST have O(N) worst-case time complexity due to potential tree restructuring.

Uploaded by

dastanktl26
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

Binary Week 10

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

Reverse DNS Find domain name IP address Domain name

Genomics Find markers DNA string Known positions


File system Find file on disk Filename Location on disk

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)

Challenge. Efficient implementations of both search and insert.


Binary search in an ordered array
 Data structure. Maintain an ordered array of key-value pairs. entries in white
are a[lo..hi]
 Rank helper function. How many keys < k ?
Key [ ]
successful search for
0 1 2 3 4 5 6 7 8 9
P
lo hi m
0 9 4 A C E H L M P R S X
5 9 7 A C E H L M P R S X
5 6 5 A C E H L M P R S X
6 6 6 A C E H L M P R S X

entry in Yellow is
loop exits with keys[m] = P: return 6 a[m]
Trace of binary search for rank in an ordered
array

unsuccessful search for Q 0 1 2 3 4 5 6 7 8 9


lo hi m
0 9 4 A C E H L M P R S X
5 9 7 A C E H L M P R S X
5 6 5 A C E H L M P R S X
7 6 6 A C E H L M P R S X

loop exits with lo > hi: return


7
Binary search: trace of standard indexing
client
Problem. To insert, need to shift all greater keys over.
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)
binary search
log N N log N N/2
(ordered array)
SYMBOL TABLES
 API
 Elementary implementations
 Ordered operations
Examples of ordered symbol table API
Ordered symbol table API
Binary search: ordered symbol table
operations summary
Sequential Binary search

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.

A binary tree is either:


 Empty.
 Two disjoint binary trees (left and right).

Symmetric order. Each node has a key, and


every node’s key is:
 Larger than all keys in its left subtree.
 Smaller than all keys in its right subtree.
Binary Tree & Binary Search Tree
 A binary search tree (BST) and a binary tree (BT) are both data structures used in
computer science and programming, but they have distinct differences in terms of their
structure and the operations they support.

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

A Compare H with R (go left) R

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

C H Compare H with H (search hit


left)

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

A Compare R and G (go left) 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

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

No more nodes (insert here) G M


BST insert
 Put. Associate value with key.
Search for key, then two cases:
 Key in tree ⇒ reset value.
 Key not in tree ⇒ add new node.
Tree shape
 Many BSTs correspond to same set of keys.
 Number of compares for search/insert is equal to 1 + depth of node.

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:

Depth First Search or DFS


 Inorder Traversal
 Preorder Traversal
 Postorder Traversal
Inorder Traversal
 Traverse left subtree.
 Visit the root.
 Traverse right subtree.

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

// Define a class to represent a binary tree


class BinaryTree // Print the key value of the current node
Node root print node.key

// Constructor to initialize an empty binary tree


// Recursively traverse the right subtree
BinaryTree() call printInorder(node.right)
root = null
Preorder Traversal
 Visit the root.
 Traverse the left subtree.
 Traverse the right subtree.

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

 Postorder traversal is useful in getting the postfix expression from an expression


tree.
 It is also used for tree deletion
Pseudocode representation performs a
postorder traversal
// Define a class to represent a node in the // Function to perform a postorder traversal of
binary tree the binary tree
function printPostorder(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)
key = item // Recursively traverse the left subtree
left = right = null call printPostorder(node.left)
// Define a class to represent a binary tree
class BinaryTree // Recursively traverse the right subtree
Node root call printPostorder(node.right)

// 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

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 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!

You might also like