‫ال‬
ُ‫م‬ ‫اَل‬َّ‫س‬
ْ
‫م‬ُ
‫ك‬ْ‫ي‬َ‫ل‬َ
‫ع‬
BST
BINARY
SEARCH TREE
Syed Hasnat Ali and Hassan Butt
Agenda
 Binary Search Tree
 Insertion
 Deletion
 Tree Traversal Types
 Time Complexity
BST properties
• Maximum 2 child nodes.
• Smaller values on the left side.
• Bigger values on the right side.
• Leaves have no further nodes.
• Levels define the height of the tree.
• Balanced tree has equal elements on both sides.
Binary Search Tree
10
16
5
6
2 50
12
40
18
Root node & Leaves
10
16
5
6
2 50
12
40
18
Root Node
Leave Node
Levels of Tree
10
16
5
6
2 50
12
40
18
Root Node
Leave Node
Level 1
Level 2
Level 3
10,13,17,18,20,25
10
13
17
18
20
25
99,53,23,13,7,6
99
53
23
13
7
6
Insertion
Search Code
Deletion
Search for the Node
•Start at the root.
•Move left if d is less than current node.
•Move right if d is greater.
•Keep going until you find the node or reach nullptr.
Time Complexity: O(h) → height of tree
Handle Deletion Cases
Case 1: Node has no children (leaf)
•Simply remove the node by setting
its parent's pointer to nullptr.
Time Complexity: O(1)
Handle Deletion Cases
Case 2: Node has one child
•Replace the node with its child.
•Update the parent's pointer.
Time Complexity: O(1)
Handle Deletion Cases
Case 3: Node has two children
•Find the in-order successor (smallest node in right subtree).
•Copy successor's data into the current node.
•Delete the successor node (which has at most one child).
Time Complexity: O(h) → finding successor takes height of
tree
Time Complexity
Operation Time Complexity
Search node O(h) (height of tree)
Delete node O(h)
Overall
O(h) = O(log n) for balanced BST,
O(n) for skewed BST
Time Complexity of Insertion in BST
Case Explanation Time Complexity
Best Case
Inserting at root or near
root (very small tree)
O(1)
Average Case
Tree is balanced. Height
is log (n)
₂
O(log n)
Worst Case
Tree is skewed (like linked
list: all left or all right) O(n)
Time Complexity of Search in BST
Case Explanation Time
Complexity
Best Case Element is at the root O(1)
Average Case
Tree is balanced, and we
go down log (n)
₂ levels
O(log n)
Worst Case
Tree is skewed, we
check every node
O(n)
Binary Search TreeTraversal Types
 Inorder
 Pre order
 Post order
Inorder Traversal in BST
• Explore Left side
Traverse the left subtree recursively.
• Print
Visit and print the current node's data.
• Explore Right side
Traverse the right subtree recursively.
Preorder Traversal in BST
•Print
Visit and print the current node's data.
•Explore Left side
Traverse the left subtree recursively.
•Explore Right side
Traverse the right subtree recursively.
Postorder Traversal in BST
• Explore Left side
Traverse the left subtree recursively.
• Explore Right side
Traverse the right subtree recursively.
• Print
Visit and print the current node's data.
Time Complexity of Tree Traversals
Traversal
Type
Best Case
Average
Case
Worst Case
Time
Complexity
Inorder O(n) O(n) O(n) O(n)
Preorder O(n) O(n) O(n) O(n)
Postorder O(n) O(n) O(n) O(n)
Binary Search Tree (BST) - A Complete pt

Binary Search Tree (BST) - A Complete pt

  • 1.
  • 2.
  • 3.
    Agenda  Binary SearchTree  Insertion  Deletion  Tree Traversal Types  Time Complexity
  • 4.
    BST properties • Maximum2 child nodes. • Smaller values on the left side. • Bigger values on the right side. • Leaves have no further nodes. • Levels define the height of the tree. • Balanced tree has equal elements on both sides.
  • 5.
  • 6.
    Root node &Leaves 10 16 5 6 2 50 12 40 18 Root Node Leave Node
  • 7.
    Levels of Tree 10 16 5 6 250 12 40 18 Root Node Leave Node Level 1 Level 2 Level 3
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
    Deletion Search for theNode •Start at the root. •Move left if d is less than current node. •Move right if d is greater. •Keep going until you find the node or reach nullptr. Time Complexity: O(h) → height of tree
  • 13.
    Handle Deletion Cases Case1: Node has no children (leaf) •Simply remove the node by setting its parent's pointer to nullptr. Time Complexity: O(1)
  • 14.
    Handle Deletion Cases Case2: Node has one child •Replace the node with its child. •Update the parent's pointer. Time Complexity: O(1)
  • 15.
    Handle Deletion Cases Case3: Node has two children •Find the in-order successor (smallest node in right subtree). •Copy successor's data into the current node. •Delete the successor node (which has at most one child). Time Complexity: O(h) → finding successor takes height of tree
  • 16.
    Time Complexity Operation TimeComplexity Search node O(h) (height of tree) Delete node O(h) Overall O(h) = O(log n) for balanced BST, O(n) for skewed BST
  • 17.
    Time Complexity ofInsertion in BST Case Explanation Time Complexity Best Case Inserting at root or near root (very small tree) O(1) Average Case Tree is balanced. Height is log (n) ₂ O(log n) Worst Case Tree is skewed (like linked list: all left or all right) O(n)
  • 18.
    Time Complexity ofSearch in BST Case Explanation Time Complexity Best Case Element is at the root O(1) Average Case Tree is balanced, and we go down log (n) ₂ levels O(log n) Worst Case Tree is skewed, we check every node O(n)
  • 19.
    Binary Search TreeTraversalTypes  Inorder  Pre order  Post order
  • 20.
    Inorder Traversal inBST • Explore Left side Traverse the left subtree recursively. • Print Visit and print the current node's data. • Explore Right side Traverse the right subtree recursively.
  • 21.
    Preorder Traversal inBST •Print Visit and print the current node's data. •Explore Left side Traverse the left subtree recursively. •Explore Right side Traverse the right subtree recursively.
  • 22.
    Postorder Traversal inBST • Explore Left side Traverse the left subtree recursively. • Explore Right side Traverse the right subtree recursively. • Print Visit and print the current node's data.
  • 23.
    Time Complexity ofTree Traversals Traversal Type Best Case Average Case Worst Case Time Complexity Inorder O(n) O(n) O(n) O(n) Preorder O(n) O(n) O(n) O(n) Postorder O(n) O(n) O(n) O(n)