Binary Search Trees
Binary Search Trees
Introduction
Types of Binary Search Trees
Traversals
Binary Search Trees Operations
Application Of Binary Trees
Advatages and Disadvantages
Group 8
1. Natasha C Kanyungwe N02422855A
2. Laura Mkandla N02418635V
3. Trevor Moyo N02418878S
4. Honest Tshuma N02419879W
5. Ayanda C Mhlanga N02421204X
6. Blessing Mhlanga N02420768F
7. Patrice Kasere N02422708B
8.Mcvey Anesuishe Shayachimwe N02428574W
9. Priatah Mukuche N02418666B
10. Fortune T Sithole N02419127M
INTRODUCTION IN BINARY SEARCH TREES
What is a Binary Search Tree?
A binary search tree includes nodes arranged in a predetermined sequence. The value of the root in a binary
search tree is higher than the value of the nodes in the left sub-tree. Meanwhile, the root’s value usually equals
or exceeds all the nodes in the right subtree.
The Anatomy of Binary Search Trees
The different components of a binary search tree in the data structure are as follows:
Nodes: Node refers to a termination point in any binary search tree in data structures.
Roots: The node on top of a binary tree is called the root.
Leaf Node: The external nodes with no child are called the leaf nodes.
Internal Node: All inner nodes with at least one child node are called internal nodes.
Parent: Apart from the root, all other nodes of the binary tree with a minimum of one subnode are a parent.
Child: The node rising straight from the parent node is the child.
Edge: An edge can indicate a relationship between two nodes by connecting them.
Depth/ Height of a Tree: The root or tree height signifies the highest number of edges from the farthest lead
node to the edges.
The different types of binary search trees
Searching for a value in a BST is very similar to how we found a value using Binary Search on an array.
For Binary Search to work, the array must be sorted already, and searching for a value in an array can then be done
really fast.
How it works:
How it works:
If the node is a leaf node, remove it by removing the link to it.
If the node only has one child node, connect the parent node of the node you want to remove to that child node.
If the node has both right and left child nodes: Find the node's in-order successor, change values with that node, then
delete it.
Program to Implement Deleting
def delete(node, data):
if not node:
return None
if data < node.data:
node.left = delete(node.left, data)
elif data > node.data:
node.right = delete(node.right, data)
else:
# Node with only one child or no child
if not node.left:
temp = node.right
node = None
return temp
elif not node.right:
temp = node.left
node = None
return temp
# Node with two children, get the in-order successor
node.data = minValueNode(node.right).data
node.right = delete(node.right, node.data)
return node
Insertion in Binary Tree
• Inserting elements means add a new node into the binary tree. We would first creates a root node in case of empty
tree. Then subsequent insertions involve iteratively searching for an empty place at each level of the tree. When an
empty left or right child is found then new node is inserted there. By convention, insertion always starts with the left
child node
Insertion Process
• Begin at the root node
• Compare the value to be inserted with the current node’s value
if the value is less, move to the left child
If the value is greater, move to the right child
• Repeat this process until you find an empty spot (a null child) where the new value can be inserted as a new
node
Steps for insertion
1. Start the root: check if the tree is empty. If it is, create a new node and make it the root
2. Traverse the tree(visiting all nodes of the binary search tree): depending on comparison, move left or right
3. Insert the node: when a null position is found, insert the new node
Program to Implement Insertion
Using Python
else:
if data < node.data:
node.left = insert(node.left, data)
elif data > node.data:
node.right = insert(node.right, data)
return node
Application of Binary Search Trees
Binary Search Trees (BSTs) are a fundamental data structure in computer science with a wide range of applications due to their efficient search, insertion, and deletion operations (average time
complexity of O(log n)). Below are some key applications of BSTs:
1. Preorder traversal is a tree traversal technique where the current node is visited first, followed by its left subtree, and then its right subtree.
Example:
Suppose we have the following binary tree:
4
/ \
2 6
/ \ / \
135 7
4, 2, 1, 3, 6, 5, 7
Postorder traversal
Postorder traversal is a tree traversal technique where the left subtree and the right subtree are visited first, followed by the current node.
Example:
Suppose we have the following binary tree:
4
/ \
2 6
/\ /\
13 5 7
The postorder traversal of this tree would be:
1, 3, 2, 5, 7, 6, 4
Inorder Traversal
Inorder traversal is a tree traversal technique where the left subtree is visited first, followed by the current node, and finally the right subtree.
Example:
Suppose we have the following binary tree:
4
/ \
2 6
/\ /\
13 5 7