Вы находитесь на странице: 1из 73

Discrete Mathematics Project

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.

 Insertion Time Complexity:


Insertion in binary tree has worst case complexity of O(n). In general, it has
worst case complexity of O(h), where h is the height of the tree.
4
 Deletion Time Complexity:
Deletion in binary tree has worst case complexity of O(n).In general it has time
complexity of O(h),where h is the height of the tree.
Designing Binary Search Tree

5
Designing Binary Search Tree

First set the first element as root node then


follow the following procedure:
 If the next element is less than the root node
then go to left child otherwise go to the right
child
6  Continue the above step for every next node
until you design a whole tree.
Designing Binary Search Tree
Suppose we have given the following element for designing BST:
10, 5, 14, 7, 12, 20 First we have set
the root node

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

Now again the given value


5 14 is greater than the current
13 node value so move to the
right

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

Wow, we have finally


5 14 searched the element and
found it.

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.

10 As the 12 node is leaf,


having no child so we will
7 14 simply remove it.

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.

10 So we have deleted the


given node…
7 14

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:

As the given node has one


10 left child so we will replace
that node with the value of
7 14 the child…
14
18
2 9 13

12
Deleting an Element
Let’s discuss another case, Suppose we are given to delete
the node having value 13. Let’s do that:

So we deleted the node


10 having the value 13…

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>

using namespace std;


template <class Type>
class Node{
public:
Type data;
16 Node<Type>* rightnode;
Node<Type>* leftnode;
//constructors
Node(): rightnode(NULL), leftnode(NULL) {}
Node(Type data): data(data), rightnode(NULL), leftnode(NULL) {}
~Node() {}
};
Binary Search Tree Implementation in C++
template<class Type>
class BST{
private:
Node<Type>* root;
size_t nodes;
bool IsEmpty(void);
public:
BST(): root(NULL), nodes(0) {}
~BST() { Clear(root); }
void InsertData(Type data);
bool SearchData(Type data) const;
void PrintInOrderTraversal(void) const;
17 void PrintPreOrderTraversal(void) const;
void PrintPostOrderTraversal(void) const;
void InOrderTraversal(Node<Type>* node) const;
void PreOrderTraversal(Node<Type>* node) const;
void PostOrderTraversal(Node<Type>* node) const;
void SortElements(void) const;
void Clear(Node<Type>* node); };
Binary Search Tree Implementation in C++

template <class Type>


void BST<Type> :: InsertData(Type data){
Node<Type>* newnode = new Node<Type>(data);
Node<Type>* parent = NULL;

if(IsEmpty()){ root = newnode;


++nodes; return; }

Node<Type>* current = root;


while(current){ parent = current;

18 if(newnode->data > current->data) current = current->rightnode;


else current = current->leftnode; }

if(newnode->data > parent->data)


parent->rightnode = newnode;
else parent->leftnode = newnode;
++nodes; }
Binary Search Tree Implementation in C++

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++

//code for in order traversal


template<class Type>
void BST<Type> :: PrintInOrderTraversal(void) const{
BST<Type> :: InOrderTraversal(root);
}

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++

//Code for Pre Order Traversal


template<class Type>
void BST<Type> :: PrintPreOrderTraversal(void) const{
BST<Type> :: PreOrderTraversal(root);
}

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++

//Code for Post Order Traversal


template<class Type>
void BST<Type> :: PrintPostOrderTraversal(void) const{
BST<Type> :: PostOrderTraversal(root);
}
template<class Type>
void BST<Type> :: PostOrderTraversal(Node<Type>* node) const{
if(node->leftnode) InOrderTraversal(node->leftnode);
22 if(node->rightnode) InOrderTraversal(node->rightnode);
cout <<" "<< node->data << " " <<endl;
}
THANK YOU
22

Вам также может понравиться