Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Writeup (2)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CS212: Design & Analysis of Algorithms 3rd Semester

Lecture 11: September 5


Instructor: Prof. Prateek Vishnoi Indian Institute of Technology, Mandi

It is widely understood that the two main data structures are arrays and linked lists. Arrays enable efficient
random access, allowing for logarithmic time complexity in search operations, assuming the array is sorted.
The time complexity of the INSERT operation is O(n).

However, linked lists have a time complexity of O(n) for searching, whereas the insertion operation has a
time complexity of O(1) if the correct position for insertion is known.

Can we do better??
We seek to identify an alternative data structure that surpasses the current one in terms of both operations.
BST(Binary Search Tree) is a specific endeavor to accomplish that goal.

Introduction to Binary Search Trees (BSTs)

A Binary Search Tree (BST) is a type of binary tree that maintains a specific order of elements, allowing
efficient search, insertion, and deletion operations.

Definition

A BST is a binary tree, where each node has:

ˆ A key (value),

ˆ A left child with keys less than the node’s key, and

ˆ A right child with keys greater than the node’s key.

Properties of a BST
ˆ Binary Tree Structure: Each node can have up to two children.

ˆ Ordered Structure: For any node N in the tree:

– All keys in the left subtree are smaller than N ’s key.


– All keys in the right subtree are greater than N ’s key.

ˆ No duplicate values: By convention, BSTs generally do not contain duplicate values. However there
is no restriction on it. For duplicate keys, we can assume any convention that for any node N in the
tree, all keys in the left subtree are smaller than or equal to N ’s key or all keys in the right subtree
are greater than or equal to N ’s key. But we stick to only one convention throughout.

11-1
11-2 Lecture 11: September 5

Basic Operations on BSTs

Search Operation

Goal: To find a specific value in the tree.


Procedure:

1. Start at the root node.

2. Compare the target value x with the current node’s key:

ˆ If x is smaller, search in the left subtree.


ˆ If x is larger, search in the right subtree.
ˆ If x equals the current node’s key, the value is found.

3. Continue until the value is found or a null reference is encountered, indicating the value is not in the
tree.

Time Complexity:

ˆ Best case: O(1) (root contains the target).

ˆ Average/Worst case: O(h), where h is the height of the tree.


The proof can be checked from CLRS.

Insertion Operation

Goal: To insert a new value into the BST while maintaining its ordered structure.
Procedure:

1. Start at the root and traverse the tree like the search operation.

2. Once you reach a null reference (where the target would have been if it existed), insert the new value
at that position as a new node.

Time Complexity: O(h), where h is the height of the tree.


Author is encouraged to check the proof from CLRS, as this has been already covered in prerequisite course.

Deletion Operation

Goal: To remove a value from the tree while preserving the BST property.
Procedure: There are three cases:

1. Leaf node: Simply remove the node.

2. Node with one child: Remove the node and replace it with its child.
Lecture 11: September 5 11-3

3. Node with two children:


ˆ Find the inorder successor (the smallest value in the right subtree) or the inorder predecessor
(the largest value in the left subtree).
ˆ Replace the node’s value with the successor/predecessor’s value.
ˆ Recursively delete the successor/predecessor.

Time Complexity: O(h), where h is the height of the tree.


Check the proof from CLRS.

Traversal Methods
ˆ Inorder Traversal: Left → Root → Right.
– Result: Sorted sequence of node keys.
ˆ Preorder Traversal: Root → Left → Right.
– Use: Can be used to create a copy of the tree.
ˆ Postorder Traversal: Left → Right → Root.
– Use: Often used in deletion or freeing memory in tree-like structures.

Properties and Performance

Time Complexity of BST Operations


ˆ Best Case: If the tree is balanced, height h ≈ log(n) where n is the number of nodes.
– Search, Insertion, and Deletion: O(log n).
ˆ Worst Case: If the tree is skewed (i.e., all nodes are either to the left or right), the height is n.
– Search, Insertion, and Deletion: O(n).

Height of a BST

The height of a BST is a critical factor in determining its efficiency.

ˆ A perfectly balanced BST has a height of O(log n), leading to efficient operations.
ˆ An unbalanced (skewed) BST has a height of O(n), making the operations inefficient.

All the details above is more or less known to you from prerequisite course. But one question that we want
to focus is
Are BST really too bad ??

More precisely, for how many instances height of BST becomes Ω(n). What is the average height of the
randomly built BST? There can be many random models for BSTs. However, we only consider only one
random model which we discussed in class. Go through the slides for the complete result and proof.
11-4 Lecture 11: September 5

Judgment Question

Since we are fascinated from the sorting techniques from the beginning of the course. Let’s focus on one
question :

ˆ Sort a given set of n numbers by building a BST containing these numbers (using INSERT repeatedly
to insert the numbers one by one) and then printing the numbers by an inorder traversal. What are
the worst-case and best-case running times for this sorting algorithm.

You might also like