Professional Documents
Culture Documents
Project Name:
1 Project Members:
Binary Search Tree
Saeed Ahmad
Saad Ahmad
Contents:
1. Introduction
2. Designing a binary search tree
3. Inserting an element
4. Searching an element
2 5. In Order Traversal
6. Pre Order Traversal
7. Post Order Traversal
8. Finding the no. of nodes in tree
Introduction
A binary search tree, BST, is either empty or;
BST has a special node called the root node;
BST has two sets of nodes, LBST and RBST , called the left
subtree and right subtree respectively
The key in the root node is larger than every key in the left
3 subtree and smaller than every key in the right subtree.
Binary Search Tree time Complexity
Searching Time Complexity:
Searching in binary tree has worst case complexity of O(h), where h is
the height of the tree. Generally, it has O(n) complexity.
5
Designing Binary Search Tree
10
7
Designing Binary Search Tree
Suppose we have given the following element for designing BST:
10, 5, 14, 7, 12, 20
10
7
Designing Binary Search Tree
Suppose we have given the following element for designing BST:
10, 5, 14, 7, 12, 20
10
5 14
7
Designing Binary Search Tree
Suppose we have given the following element for designing BST:
10, 5, 14, 7, 12, 20
10
5 14
7 7
Designing Binary Search Tree
Suppose we have given the following element for designing BST:
10, 5, 14, 7, 12, 20
10
5 14
7 7 12
Designing Binary Search Tree
Suppose we have given the following element for designing BST:
10, 5, 14, 7, 12, 20
10
5 14
7 7 12 20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right node:
Let’s consider the inorder traversal for the below tree;
10
14
8 5
12 20
3 7
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
10
14
8 5
12 20
7
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
10
14
8 5
12 20
7
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
10
14
8
12 20
7
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
10
14
8
12 20
7
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
10
14
8
12 20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
10
14
8
12 20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
14
8
12 20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
14
8
12 20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
14
8
20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
14
8
20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
8
20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
8
20
Inorder Traversal
In this strategy we think of nodes as falling freely starting from left
and then passing through root node and end at most right :
Let’s consider the inorder traversal for the below tree;
8
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
10
14
9 5
12 20
3 7
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9 5
12 20
3 7
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9 5
12 20
3 7
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9
12 20
3 7
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9
12 20
3 7
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9
12 20
7
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9
12 20
7
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9
12 20
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
14
9
12 20
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
9
12 20
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
9
12 20
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
9
20
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
9
20
Preorder Traversal
In this method we start from root node and go to left subtree and
visiting every node one by one and then go to the right subtree…
Let’s consider the preorder traversal for the below tree;
9
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10 5
12 20
3 7
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10 5
12 20
7
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10 5
12 20
7
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
10 5 14
12 20
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10 5
12 20
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10
12 20
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10
12 20
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10
20
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10
20
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
14
10
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
10
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
10
Postorder Traversal
The postorder traversal is just like a leaf falling, starting from left
subtree, then goes to right subtree and finally root node.
Let’s consider the order traversal for the below tree;
10
Searching an Element
We have to follow the same procedure in searching of an element as
we did in inserting an element to a binary tree.
o First we will compare the given element with root node, we have
three possibilities, that are;
a. Either the given value is equal to the value at root node, if that’s
the condition then we have found the element
b. If the given value is greater than the value at root node then we
will go to the right subtree
11 c. If the given value is less than the value at root node, then we
will follow the left subtree
o Now we will consider the new node as our root and we will
follow again the above procedure until we goes to the leaf node
or we find the given element.
Searching an Element
Suppose we have given the below tree and we have to search 13 in
it. Let’s do that:
10 13 Not equal, the given value
is greater than the root
node, so go to the right
5 14
12 3 7 12 20
6 8 13
Searching an Element
Suppose we have given the below tree and we have to search 13 in
it. Let’s do that:
We have moved one step to
10
the right, but now the given
value is less than the node
5 14 13 value, so go to the left.
12 3 7 12 20
6 8 13
Searching an Element
Suppose we have given the below tree and we have to search 13 in
it. Let’s do that:
10
12 3 7 12 20
6 8 13
Searching an Element
Suppose we have given the below tree and we have to search 13 in
it. Let’s do that:
10
12 3 7 12 20
13
13
6 8
Deleting an Element
There are many cases of deleting an element, Let’s
discuss some of them. Suppose we have given the tree
we have to delete the element 12.
13
13 18
2 9
12
Deleting an Element
There are many cases of deleting an element, Let’s
discuss some of them. Suppose we have given the tree
we have to delete the element 12.
13
13 18
2 9
Deleting an Element
Let’s discuss another case, Suppose we are given to delete
the node having value 13. Let’s do that:
12
Deleting an Element
Let’s discuss another case, Suppose we are given to delete
the node having value 13. Let’s do that:
7 14
14
18
2 9 12
Deleting an Element
Let’s discuss a more complicated case: Let’s delete 14
node.
As the node having value 14
10
has two child left as well as
right. In this case we will
7 14
check the first right node
having smallest left node
18 leaf, in this case the node
2 9 12
having value 15 is suitable
15 22
to replace 15…
15
Deleting an Element
Let’s discuss a more complicated case: Let’s delete 14
node.
10
7 15
Wow! We have done that…
18
2 9 12
15 22
Binary Search Tree Implementation in C++
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <conio.h>
#include <cstdbool>
template<class Type>
bool BST<Type> :: SearchData(Type data) const{
Node<Type>* current = root;
while(current){
if(current->data == data) return true;
else if(current->data < data)
current = current->rightnode;
19 else
current = current->leftnode;
}
return false; }
Binary Search Tree Implementation in C++
template<class Type>
void BST<Type> :: InOrderTraversal(Node<Type>* node) const{
if(node->leftnode) InOrderTraversal(node->leftnode);
20 cout <<" "<< node->data << " " <<endl;
if(node->rightnode) InOrderTraversal(node->rightnode);
}
Binary Search Tree Implementation in C++
template<class Type>
void BST<Type> :: PreOrderTraversal(Node<Type>* node) const{
cout <<" "<< node->data << " " <<endl;
21 if(node->leftnode) InOrderTraversal(node->leftnode);
if(node->rightnode) InOrderTraversal(node->rightnode);
}
Binary Search Tree Implementation in C++