Binary Search Tree.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
BST Data Structure A BST node contains: A BST contains
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
Binary Search Trees Chapter 6.
Binary Search Trees Chapter 7 Objectives
Data Structures Using C++1 Chapter 11 Binary Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Binary trees Binary search trees Expression trees Heaps Data Structures and Algorithms in Java, Third EditionCh06 – 1.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
CSE 3358 NOTE SET 10 Data Structures and Algorithms.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Binary Search Trees Chapter 7 Objectives
CSCE 3110 Data Structures & Algorithm Analysis
BCA-II Data Structure Using C
CSCE 3110 Data Structures & Algorithm Analysis
Binary Trees and Binary Search Trees
Trees ---- Soujanya.
UNIT III TREES.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Tree Traversals – reminder
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Tree Traversals – reminder
Chapter 21: Binary Trees.
CS212: Data Structures and Algorithms
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Trees.
Trees CMSC 202, Version 5/02.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Trees.
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Binary Search Tree

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.

Examples A binary search tree Not a binary search tree

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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*); };

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

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

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

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.

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); }

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!

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

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.

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

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

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

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;

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);

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

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.

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

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.

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

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;

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; }

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;

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

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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); }

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