Binary Search Trees
Binary Search Trees
Key Points:
1. Binary Search Trees:
o BSTs are not balanced.
o They are simpler but may become inefficient if not properly structured.
2. AVL Trees (to be discussed later):
o AVL trees are a type of balanced tree, meaning they maintain a more uniform
structure to optimize operations.
o
The Problem with Traditional Linear Lists
When we design a linear data structure, like a list, we typically have two main choices:
• Array:
o Good for searching using binary search (very fast).
o But insertion and deletion are slow since elements may need to shift.
• Linked List:
o Excellent for insertion and deletion (since nodes are linked).
o However, searching is slow because you have to check each node one by one.
• Figure 7-2(d):
This tree is nearly complete and balanced—a good structure but not perfect.
•
• Figure 7-2(c) and 7-2(e):
These are neither complete nor balanced, leading to potentially slower operations.
1. Figure 7-3(a):
o Violation: The left subtree contains 22, which is greater than the root value 17.
o Rule Broken: Left subtree must have values less than the root.
2. Figure 7-3(b):
o Violation: The right subtree contains 11, which is less than the root value 17.
o Rule Broken: Right subtree must have values greater than or equal to the root.
3. Figure 7-3(c):
o Violation: While 6 < 17 and 19 > 17, the left subtree contains 11, which is greater
than 6.
o Rule Broken: Each subtree must be a valid BST. The left subtree of 6 is invalid.
4. Figure 7-3(d):
o Hint: The largest key in the left subtree breaks the BST rule. Identify this key.
Binary Search Tree (BST) Operations
In this topic, we'll cover essential operations on Binary Search Trees (BSTs). These include tree
traversals, simple search algorithms, and tree-building methods.
Traversal means visiting all the nodes of a tree in a specific order. You might recall from Chapter
6 that there are three main types of tree traversals:
Let’s examine these traversals and their outputs using the tree in Figure
1. Pre-order Traversal
• Order of nodes visited: Visit the root first, then the left subtree, followed by the right
subtree.
• Example: For the tree in Figure, if we apply preorder traversal, we might get:
• Observation: This traversal might not always produce a meaningful sequence, especially
for ordered data retrieval.
2. Post-order Traversal
• Order of nodes visited: Visit the left subtree first, then the right subtree, and finally the
root.
• Example: Post-order traversal might produce:
• Observation: Again, while this is valid, it may not serve well for ordering or searching.
3. In-order Traversal
• Order of nodes visited: Visit the left subtree first, then the root, and finally the right
subtree.
• Example: Applying in-order traversal results in:
• Key Point: In-order traversal of a BST produces a sorted (ascending) list of values.
This makes it highly useful for ordered data processing.
• Order of nodes visited: Visit the right subtree first, then the root, and finally the left
subtree.
• Example: Reverse in-order traversal would give:
52 44 35 23 20 18 12
• Algorithm:
o Start at the root.
o Compare the target value to the root.
▪ If smaller, go to the left subtree.
▪ If larger, go to the right subtree.
o Repeat until the target is found or you reach a null pointer.
• Example: Searching for 20 involves the path 23 → 18 → 20 (see Figure 7-6).
3. Insertion in BST
To insert a node:
Key Points:
4. Deletion in BST
Class Activity
1. Draw a BST: Use the nodes 23, 18, 44, 12, 20, 35, 52.
2. Perform Traversals: Apply all four traversal techniques.
3. Insertion Task: Insert 19 and see the result.
4. Deletion Task: Remove 23 and analyze the changes.