0% found this document useful (0 votes)
27 views9 pages

Binary Search Trees

This document provides an overview of Binary Search Trees (BSTs) and their advantages over traditional linear data structures like arrays and linked lists. It explains the structure and operations of BSTs, including insertion, deletion, and various tree traversal methods. Additionally, it introduces AVL Trees as a balanced alternative to BSTs to maintain efficiency in operations.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

Binary Search Trees

This document provides an overview of Binary Search Trees (BSTs) and their advantages over traditional linear data structures like arrays and linked lists. It explains the structure and operations of BSTs, including insertion, deletion, and various tree traversal methods. Additionally, it introduces AVL Trees as a balanced alternative to BSTs to maintain efficiency in operations.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Binary Search Trees (BSTs) - Simplified Explanation for Lecture

Introduction to Search Trees


A search tree is a special type of data structure used to organize and manage data efficiently. In
this lecture, we will focus on Binary Search Trees (BSTs) and give a brief overview of AVL
Trees. Both are useful when we need to keep data in order, making it easier to search, insert, or
delete data.

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.

The Need for a Better Structure


We want a data structure that combines the fast searching of arrays with the quick insertion
and deletion of linked lists.
This is where Binary Search Trees (BSTs) and AVL Trees come in.
Binary Search Trees Explained
A Binary Search Tree is structured in a way that allows:
• Fast Search
Similar to binary search: every comparison eliminates half of the remaining data.
• Efficient Insertion and Deletion
Because the tree structure adjusts dynamically, inserting or deleting a node doesn’t
require shifting elements like an array.
How it works:
Each node in a BST has:
1. Value (data).
2. Left child: Contains values less than the node’s value.
3. Right child: Contains values greater than the node’s value.
Example for Understanding
Let’s say we have these numbers: 30, 20, 40, 10, 25. We want to organize them in a Binary
Search Tree:
1. Start with 30 as the root.
2. Insert 20:
o
Since 20 < 30, it goes to the left of 30.
3. Insert 40:
o Since 40 > 30, it goes to the right of 30.
4. Insert 10:
o Since 10 < 30 and 10 < 20, it goes to the left of 20.
5. Insert 25:
o Since 25 < 30 and 25 > 20, it goes to the right of 20.
Resulting BST Diagram:
(Draw the following tree on the board for better understanding)
30
/ \
20 40
/ \
10 25

Advantages of Binary Search Trees


• Efficient Searching: Each step narrows down the search space.
• Efficient Insertions/Deletions: Only certain parts of the tree need adjustment.
Disadvantage:
• BSTs can become unbalanced (e.g., if data is inserted in sorted order like 10, 20, 30, 40),
making search slower. This is where AVL Trees help by keeping the tree balanced.

Practice Questions: BST Construction


1. Construct a BST from the following sequence:
45, 20, 60, 10, 25, 50, 70
• Draw the final BST.
• What is the height of the tree?
• Identify the leaf nodes.

2. Construct a BST from this sequence:


30, 15, 50, 10, 25, 40, 60
• Insert the values one by one and draw the BST after each step.
• What is the root of the tree?
• What is the in-order traversal of the tree?

3. Given the following preorder traversal of a BST:


35, 20, 10, 25, 50, 40, 60
• Construct the BST.
• Write down the post-order traversal of the constructed tree.
• What is the value of the leftmost node?

Basic Concepts of Binary Search Trees (BSTs)

What is a Binary Search Tree (BST)?


A Binary Search Tree (BST) is a special type of binary tree that follows these important rules:
1. Left Subtree Rule:
o
All nodes in the left subtree of a node have values less than the value of the root
node.
2. Right Subtree Rule:
o All nodes in the right subtree of a node have values greater than or equal to the
value of the root node.
3. Subtree Rule:
o Each subtree (left or right) must itself be a binary search tree.
Example:
Consider the following BST structure:
15
/\
10 20
/ \ / \
5 12 17 25
• The left subtree of 15 contains values less than 15: {10, 5, 12}.
• The right subtree of 15 contains values greater than or equal to 15: {20, 17, 25}.
• The same rules apply recursively to all nodes (e.g., 10’s left subtree is {5}, right subtree
is {12}).
Structure of Nodes in a BST
Each node typically holds a record, which includes:
• Key: Used for comparisons and determining the structure of the tree.
• Other data fields.
Balanced and Complete Trees
• A balanced tree ensures the left and right subtrees have approximately the same height.
This keeps operations efficient (e.g., searching, insertion, deletion).
• A complete tree is one where all levels are fully filled except possibly the last, which is
filled from left to right.
Examples of Binary Search Trees (Figure 7-2)

• Figure 7-2(a) and 7-2(b):


These trees are complete and balanced, meaning they are optimized for efficient
operations.

• 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.

Examples of Invalid Binary Search Trees (Figure 7-3)


Let’s look at some trees that do not follow BST rules:

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.

Traversals in Binary Search Trees

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:

1. Preorder Traversal (Root, Left, Right)


2. Post-order Traversal (Left, Right, Root)
3. In-order Traversal (Left, Root, Right)

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.

4. Right-Node-Left Traversal (Reverse In-order)

• 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

2. Search Operations in BST

Searching is fundamental in BSTs, allowing us to locate specific nodes efficiently.

a. Find the Smallest Node

• Logic: The smallest value is always found at the far-left node.


• Steps:
o Start at the root.
o Keep moving to the left until you reach a node with no left child.
• Example: For the BST in Figure , the smallest node is 12.
b. Find the Largest Node

• Logic: The largest value is always at the far-right node.


• Steps:
o Start at the root.
o Keep moving to the right until you reach a node with no right child.
• Example: The largest node is 52.

c. Search for a Specific Node

• 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:

1. Start at the root.


2. Compare the new value with the current node.
3. Move left if the value is smaller; move right if larger.
4. Insert the new node when you reach a null position.

Key Points:

• All insertions happen at a leaf or a leaf-like node.


• Example:
o Insert 19 into the BST.
o Follow the path 23 → 18 → 20.
o Insert 19 as the left child of 20 (see Figure 7-8).

4. Deletion in BST

Deletion in BSTs is more complex, with three primary cases:

Case 1: Node with No Children (Leaf)

• Simply remove the node.


• Example: Deleting 12 removes a leaf node.

Case 2: Node with One Child

• Replace the node with its only child.


• Example: If node 20 has only a right child, replace 20 with its right child.

Case 3: Node with Two Children

• Find a replacement for the deleted node:


1. The largest node in the left subtree.
2. The smallest node in the right subtree.
• Example:
o Deleting 23:
▪ Find the largest node in the left subtree (22).
▪ Replace 23 with 22.
▪ Remove 22 from its original position (see Figure 7-10).

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.

You might also like