0% found this document useful (0 votes)
19 views

Binary Search Tree

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Binary Search Tree

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Binary Search Tree

Definition of Binary Search Tree (BST)


Definition

A Binary tree T is a Binary Search Tree (BST), if each node N of T has the following
property : The value at N is greater than every value in the left subtree of N and is less
than every value in the right subtree of N.

A Binary Search Tree (BST) is a special type of binary tree in which the left child of a
node has a value less than the node’s value and the right child has a value greater than
the node’s value. This property is called the BST property and it makes it possible to
efficiently search, insert, and delete elements in the tree.
BST are used to maintain a dynamically changing dataset in sorted order, which
allows for quick lookup, addition, and removal of items.

A Binary Search Tree (BST) properties

 Binary Tree: Each node has at most two children, commonly referred to as
the left child and the right child.
 Ordered Structure: For every node N, the values of all the nodes in the left
subtree of N are less than the value of N, and the values of all the nodes in
the right subtree of N are greater than the value of N. This is known as the
binary search property.
 No Duplicate Nodes: Typically, BSTs do not contain duplicate values. Each
node has a distinct key or value.

Operations in a Binary Search Tree


BSTs support several fundamental operations, which are efficient due to their ordered
nature. Here’s an overview of the primary operations:

1. Insertion Operations of BST


Insertion in a BST involves adding a new node while maintaining the BST property:
for any given node, all elements in its left subtree are less than the node, and all
elements in its right subtree are greater.

Steps for Insertion:

1. Start at the Root: Begin at the root node.


2. Compare and Traverse:
1. If the tree is empty, the new node becomes the root.
2. Otherwise, compare the new node's value with the current node's
value.
3. If the new value is smaller, move to the left child; if larger, move to the
right child.
3. Repeat the Comparison: Continue this process recursively down the tree
until you reach a leaf node (a node with no children).
4. Insert the Node: Attach the new node as the left or right child of the leaf
node, depending on its value.
Algorithm

Function Insert(root, value):

If root is NULL:

root = CreateNode(value)

return root

If value < root.value:

If root.left is NULL:

root.left = CreateNode(value)

Else:

Insert(root.left, value)

Else If value > root.value:

If root.right is NULL:

root.right = CreateNode(value)

Else:

Insert(root.right, value)

return root

2. Search Operations of BST


Searching in a BST is efficient due to its sorted nature. You can determine the
direction to move at each step (left or right).

Steps for Searching:

1. Start at the Root: Begin at the root node.


2. Compare and Traverse:
1. Compare the search value with the value of the current node.
2. If they match, you've found the node.
3. If the search value is smaller, move to the left child; if larger, move to
the right child.
3. Repeat Until Found or End: Continue this process. If you reach a null
pointer (end of the path), the item isn't in the tree.
Algorithm

Function Search(root, value):

If root is NULL or root.value is value:

return root

If value < root.value:

return Search(root.left, value)

Else:

return Search(root.right, value)

3. Deletion Operations of BST


Deletion in a BST can be a bit tricky, as it needs to ensure that the BST property is
maintained after removing a node. There are three cases to consider

 Case 1 - Leaf Node


 Case 2 - One Child
 Case 3 - Two Children

Steps for Deletion:

1. Find the Node: Use the search operation to find the node to be deleted.
2. Case 1 - Leaf Node: If the node is a leaf (no children), simply remove it.
3. Case 2 - One Child: If the node has only one child, remove the node and
replace it with its child.
4. Case 3 - Two Children:

o Find the node's in-order successor (the smallest node in its right
subtree) or in-order predecessor (the largest node in its left subtree).
o Replace the node's value with the in-order successor/predecessor's
value.
o Delete the in-order successor/predecessor (which will be a simpler
case, as it has at most one child).
Algorithm
Function Delete(root, value):

If root is NULL:

return root

If value < root.value:

root.left = Delete(root.left, value)

Else If value > root.value:

root.right = Delete(root.right, value)

Else:

// Node with only one child or no child

If root.left is NULL:

temp = root.right

Free(root)

return temp

Else If root.right is NULL:

temp = root.left

Free(root)

return temp

// Node with two children: Get the inorder successor

temp = MinValueNode(root.right)

// Copy the inorder successor's content to this node

root.value = temp.value

// Delete the inorder successor


root.right = Delete(root.right, temp.value)

return root

Function MinValueNode(node):

current = node

// Loop down to find the leftmost leaf

While current.left is not NULL:

current = current.left

return current

Example is explained in class

You might also like