Writeup (2)
Writeup (2)
Writeup (2)
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.
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 key (value),
A left child with keys less than the node’s key, and
Properties of a BST
Binary Tree Structure: Each node can have up to two children.
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
Search Operation
3. Continue until the value is found or a null reference is encountered, indicating the value is not in the
tree.
Time Complexity:
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.
Deletion Operation
Goal: To remove a value from the tree while preserving the BST property.
Procedure: There are three cases:
2. Node with one child: Remove the node and replace it with its child.
Lecture 11: September 5 11-3
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.
Height of a BST
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.