Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Tree.

Similar presentations


Presentation on theme: "Binary Search Tree."— Presentation transcript:

1 Binary Search Tree

2 Binary Search Tree Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently. Binary search tree is either empty or each node N of tree satisfies the following property The Key value in the left child is not more than the value of root The key value in the right child is more than or identical to the value of root All the sub-trees, i.e. left and right sub-trees follow the two rules mention above.

3 Examples A binary search tree Not a binary search tree

4 Example 2 Two binary search trees representing the same Data set:

5 Example Input list of numbers: 14

6 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 15

7 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15

8 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 9

9 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 9 7

10 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 9 18 7

11 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 18 7

12 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 18 7 5

13 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 18 16 7 5

14 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 18 16 7 5 4

15 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 18 16 20 7 5 4

16 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 18 16 20 7 17 5 4

17 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 18 16 20 7 9 17 5 4

18 Example Input list of numbers: 14 15 4 9 7 18 3 5 16 4 20 17 9 14 5 14
14 4 15 3 9 14 18 16 20 7 9 17 5 4

19 “Binary Search Tree” of a given data set
Example Input list of numbers: 14 4 15 3 9 14 18 16 20 7 9 17 5 “Binary Search Tree” of a given data set 4 5

20 Binary Tree Implementation
Class BinaryTree{ struct node{ int data; node* LTree; node* RTree; }; node* root; BinaryTree( ){ root = NULL; } node* Insert(node* , int); void Search(node* , int); void InorderTraversal(node*); void PreorderTraversal(node*); void PostorderTraversal(node*); };

21 Inorder Traversal Function
void BinaryTree::InorderTraversal(node* temp) { if(temp!=NULL) InorderTraversal(temp->LTree); cout<< temp->data; InorderTraversal(temp->RTree); }

22 Postorder Traversal Function
void BinaryTree::PostorderTraversal(node* temp) { if(temp!=NULL) PostorderTraversal(temp->LTree); PostorderTraversal(temp->RTree); cout<< temp->data; }

23 Preorder Traversal Function
void BinaryTree::PreorderTraversal(node* temp) { if(temp!=NULL) cout<<temp->data; PreorderTraversal(temp->LTree); PreorderTraversal(temp->RTree); }

24 Searching in Binary Search Tree
Three steps of searching The item which is to be searched is compared with the root node. If the item is equal to the root, then we are done. If its less than the root node then we search in the left sub-tree. If its more than the root node then we search in the right sub-tree. The above process will continue till the item is found or you reached end of the tree.

25

26 Search Function void BinaryTree::Search(node* temp, int num) { if(temp==NULL) cout<<“Number not found"; else if(temp->data == num) cout<<"Number found"; else if(temp->data > num) Search(temp->LTree, num); else if(temp->data < num) Search(temp->RTree, num); }

27 Find Min This operation requires returning a pointer to the node in tree that has minimum value. To perform a FindMin, start at the root and go left as long as there is a left child. The stopping point is the smallest element!

28 Node* BinaryTree::FindMin(node* temp)
{ if(temp==NULL) return NULL; else if(temp->Left == NULL) return temp; else return FindMin(temp -> Left); }

29 Insertion in BST Three steps of insertion
If the root of the tree is NULL then insert the first node and root points to that node. If the inserted number is lesser than the root node then insert the node in the left sub-tree. If the inserted number is greater than the root node then insert the node in the right sub-tree.

30 Steps to Insert a New Node
Suppose a “DATA” is the information to be inserted in a BST Step 1: Compare DATA with root node information of the tree (i) If (DATA < ROOT → Info) Proceed to the left child of ROOT (ii) If (DATA > ROOT → Info) Proceed to the right child of ROOT Step 2: Repeat the Step 1 until we meet an empty sub tree, where we can insert the DATA in place of the empty sub tree by creating a new node. Step 3: Exit

31 For Example Consider the following BST
Suppose we want to insert a DATA=55 in to the tree, then following steps are performed:

32 Compare 55 with root node info (i. e
Compare 55 with root node info (i.e., 50) since 55 > 50 proceed to the right sub tree of 50 2. The root node of the right sub tree contains 75. Compare 55 with 75. Since 55 < 75 proceed to the left sub tree of 75 3. The root node of the left sub tree contains 60. Compare 55 with 60. Since 55 < 60 proceed to the left sub tree of 60 4. Since left sub tree is NULL, place 55 as the left child of 60 as shown in the figure

33

34 Insertion Function node* BinaryTree::Insert(node* temp, int num) { if ( temp == NULL ) temp = new node; temp->data= num; temp->LTree= NULL; temp->RTree=NULL; } else if(num < temp->data) temp->LTree = Insert(temp->LTree, num); else if( num >= temp->data) temp->RTree = Insert(temp->RTree, num); return temp;

35 Tree Insertion - Alternate
void BST:insert(int data) { BSTNode *p = root, *prev = NULL; while(p != NULL) //Find a place for inserting new node { prev = p; if(p  key < data) p = p right; else p = p left; } if(root == NULL) //tree is empty root = new BSTNode(data); else if (prevkey < data) prevright = new BSTNode(data); else prevleft = new BSTNode(data);

36 Exercise Write the following Functions
A function which counts the number of nodes present in the binary search tree. A function which counts the number of leaves present in the binary search tree. A function which counts the number of nodes of the binary search tree which has both children

37 Deletion in BST When we delete a node, we need to consider how we take care of the children of the deleted node. This has to be done such that the property of the search tree is maintained.

38 Deletion in BST Three cases: (1) The node is a leaf
Delete it immediately (2) The node has one child Adjust a pointer from the parent to bypass that node

39 Deletion in BST (3) The node has 2 children
Replace the key of that node with the minimum element at the right subtree Delete the minimum element Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2.

40 node. Delete(int num, node. T) { node
node* Delete(int num, node* T) { node *temp; if(T == NULL) cout<<“element not found”; else if(num< T->data) T ->Left = Delete(num, T->Left); //proceed to left subtree else if (num> T->data) T->Right = Delete(num, T->Right); //proceed to right subtree

41 else // Found element to be deleted if(T->Left && T->Right) { /
else // Found element to be deleted if(T->Left && T->Right) { /*Replace with smallest in Right Subtree */ temp = FindMin(T->Right); T->data = temp ->data; T-Right = Delete(T->data, T->Right); } else { temp = T; if(T->Left == NULL) T = T->Right; if(T->Right == NULL) T = T->Left; Delete temp; } return T;

42 Deletion Code - Alternate
void BTree::DeleteNode(node* temp, int num) { if (temp==NULL) cout<<"Number not Found"; else if((temp->data == num)) { node *parent, *min ; int number; // if number is found at a leaf node if((temp->LTree == NULL) && (temp->RTree == NULL)) { parent=GetParent(root, temp->data, root); //will return parent node if(parent->LTree == temp) parent->LTree = NULL; else if (parent->RTree == temp) parent->RTree = NULL; delete temp; }

43 if(temp->LTree != NULL){ if(parent->LTree == temp)
// if node to be deleted has one child else if(((temp->LTree == NULL) && (temp->RTree != NULL)) || ((temp->LTree != NULL) && (temp->RTree == NULL))) { parent = GetParent(root, temp->data, root); //will return parent node if(temp->LTree != NULL){ if(parent->LTree == temp) parent->LTree = temp->LTree; else if (parent->RTree == temp) parent->RTree = temp->LTree; } else if(temp->RTree != NULL){ parent->LTree = temp->RTree; parent->RTree = temp->RTree; delete temp;

44 //if node to be deleted has two children else if((temp->LTree
//if node to be deleted has two children else if((temp->LTree != NULL) && (temp->RTree != NULL)) { min = FindMin(temp->RTree); //will return the min. no. found in RTree number = min->data; DeleteNode(temp->RTree, min->data); //calling to itself recursively temp->data= number; } else if (num < temp->data) DeleteNode(temp->LTree, num); //calling to itself recursively else if (num > temp->data) DeleteNode(temp->RTree, num); //calling to itself recursively

45 Binary Tree Traversals
Depth First Tree Traversal PreOrder Tree Traversal InOrder Tree Traversal PostOrder Tree Traversal Breadth First Tree Traversal

46 Binary Trees Traversals
Breadth First Tree Traversal Implementation of this kind of traversal is straightforward when a queue is used. Consider a top down left-to-right, breadth first traversal. After a node is visited, its children, if any, are placed at the end (rear) of a queue, and the node at the beginning (front) of the queue is visited.

47 Breadth First Traversal Example
C D B E F G I H

48 Breadth First Traversal Example
C D B E F G I C Queue B Front Rear A

49 Breadth First Traversal Example
C D B E F G I Dqueue C B AC

50 Breadth First Traversal Example
C D B E F G I B Enqueu D D AC

51 Breadth First Traversal Example
C D B E F G I Dqueue B D ACB

52 Breadth First Traversal Example
C D B E F G I D Enqueue E, H E H ACB

53 Breadth First Traversal Example
C D B E F G I Dqueue D E H ACBD

54 Breadth First Traversal Example
C D B E F G I Dqueue E H ACBDE

55 Breadth First Traversal Example
C D B E F G I Enqueue F, G H F G ACBDE

56 Breadth First Traversal Example
C D B E F G I Dqueue H F G ACBDEH

57 Breadth First Traversal Example
C D B E F G I I Enqueue I F G ACBDEH

58 Breadth First Traversal Example
C D B E F G I I Dqueue F G ACBDEHF

59 Breadth First Traversal Example
C D B E F G I I Dqueue G ACBDEHFG

60 Breadth First Traversal Example
Dqueue I C B D E H F G I ACBDEHFGI

61 Binary Trees Traversals
void BT::breadthFirst() { Queue queue; BTNode *p = root; if(p != NULL) { queue.enqueue(p); while(!queue.empty()) { p = queue.dequeue(); cout<<p->key; if(p -> left != 0) queue.enqueue(p->left); if(p -> right != 0) queue.enqueue(p->right); }

62 Example Traverse the following tree in Breadth-First order. 4 2 6 3 1
5 6 1 15 14 7


Download ppt "Binary Search Tree."

Similar presentations


Ads by Google