Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Comp 245 Data Structures Trees. Introduction to the Tree ADT A tree is a non-linear structure. A treenode can point to 0 to N other nodes. There is one.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
CS 171: Introduction to Computer Science II
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
Starting Out with C++, 3 rd Edition 1 Chapter 20 Binary Trees.
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.
Data Structures Using C++1 Chapter 11 Binary Trees.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 8 – Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
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.
Trees. Trees Traversal Inorder  (Left) Root (Right) Preorder  Root (Left) (Right) Postorder  (Left) (Right) Root Root LeftRight.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R2. Binary Search Trees.
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.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
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.
1 CSE 2341 Object Oriented Programming with C++ Note Set #21.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Data Structures and Algorithms Introduction to Binary and Binary Search Trees Prepared by: S. Kondakci.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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 By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.
(c) University of Washington20-1 CSC 143 Java Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Trees.
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Prepared by: S. Kondakci
Trees Chapter 10.
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Trees.
Data Structures Using C++ 2E
Presentation transcript:

Binary Search Tree

Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only one predecessor (parent), except the root node, which has none. ΩΩΩΩΩΩ ΩΩΩΩΩΩ Tree Data Structure Ω

Terminology Node  Contains data and pointers to other nodes (called child nodes) Parent Node  Each node has at most one node.  Root node has no parent Leaf  Node with no children ΩΩΩΩΩΩΩ ΩΩΩΩΩΩ ΩΩ Root Node Leaf Node

Terminology Height of Tree  Longest path to a leaf Subtree  A node and all its children Edge (or link)  A path from a node to its child Null pointer  Points to no node (in C++, 0) ΩΩΩΩΩΩΩ ΩΩΩΩΩΩ ΩΩ Root Node (Ω)(Ω)

Binary Tree  is a nonlinear linked structure in which each node may point to two other nodes, and every node but the root node has a single predecessor. Root Node Left SubtreeRight Subtree

Binary Search Tree  Binary tree in which a data item is inserted in such a way that  all items less than the item in a node are in the left subtree  All items greater than the item in a node are in the right subtree Root Node Left SubtreeRight Subtree

Traversals Any process for visiting the nodes in some order is called a traversal.  E.g., printing, searching.

Traversals Preorder traversal:  Visit the root, then visit its left and right subtrees (children) Postorder traversal:  Visit the left and right subtrees (children), then visit the root Inorder traversal:  Visit the left subtree, then the root, then the right subtree.

Preorder Traversal 1 23

Postorder Traversal 1 2 3

Inorder Traversal 1 2 3

Representing Node struct TreeNode { elemType value; // value in node TreeNode *left; // pointer to left child TreeNode *right; // pointer to right child }; TreeNode *root; // pointer to root node Note the recursive form of the declaration. 30

BST Operations (Public) typedef int elemType; class BinTree { public: BinTree(); void insertNode(elemType item); bool search(elemType item); void remove(elemType item); void displayInOrder(); void displayPreOrder(); void displayPostOrder(); private:... Private declarations };

Private Methods class BinTree { public:... Public methods private: struct TreeNode { elemType value; // value in node TreeNode *left; // pointer to left child node TreeNode *right; // pointer to right child node }; TreeNode *root; // pointer to the root node void insert(TreeNode *&nodePtr, TreeNode *&newNode); void removeNode(elemType item, TreeNode *&nodePtr); void makeRemoval(TreeNode *&nodePtr); void displayInOrder(TreeNode *nodePtr); void displayPreOrder(TreeNode *nodePtr); void displayPostOrder(TreeNode *nodePtr); };

BST Constructor #define NULL 0 BinTree::BinTree() { root = NULL; // NULL pointer }

BST InsertNode Operation To DisplayInOrder

BST InsertNode Operation void BinTree::insertNode(elemType item) { TreeNode *newNode; // pointer to new node // Create new node and store item newNode = new TreeNode; newNode->value = item; newNode->left = newNode->right = NULL; // Insert the node insert(root, newNode); }

BST Insert Method (Private) void BinTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) { if (nodePtr == NULL) // Insert the node nodePtr = newNode; else if (newNode->value value) // Attach on left subtree insert(nodePtr->left, newNode); else // Attach on right subtree insert(nodePtr->right, newNode); } TreeNode *&NodePtr — nodePtr is reference to actual argument

Order of Insertion Affects Tree Height 37, 24, 32, 42, 40, 42, 7, 120, 2 120, 42, 7, 42, 32, 2, 37, 40, 24

Which Structure Allows More Efficient Search? Key Value:

BST DisplayInOrder Operation void BinTree::displayInOrder() { displayInOrder(root); }

BST DisplayInOrder Method (Private) void BinTree::displayInOrder( TreeNode *nodePtr) { if (nodePtr) { displayInOrder(nodePtr->left); cout value << endl; displayInOrder(nodePtr->right); } To BST diagram

BST Remove Operation When removing a node from a tree, there are 4 cases to consider 1. The node is a leaf 2. The node has no right child 3. The node has no left child 4. The node has both children

BST Remove Operation (Node is a leaf) root Ω Ω Ω Ω Ω Ω This node to be deleted Ω Ω Ω Ω 7 Ω Ω BeforeAfter

BST Remove Operation (Node has no right child) root Ω Ω Ω Ω Ω Ω This node to be deleted Ω Ω Ω Ω Ω Ω Before After

BST Remove Operation (Node has no left child) root Ω Ω Ω Ω Ω Ω This node to be deleted Ω ΩΩ Ω Ω Ω After Before

BST Remove Operation (Node has both children) This node to be deleted root Ω Ω Ω Ω Ω 40 Ω Ω Ω ΩΩ Ω Ω 40 Ω Ω Before Ω Ω Ω 40 Ω Ω After

BST remove Operation void BinTree::remove(elemType item) { removeNode(item, root); }

BST removeNode Method (Private) void BinTree::removeNode(elemType item, TreeNode *&nodePtr) { if (item == nodePtr->value) makeRemoval(nodePtr); else if (item value) removeNode(item, nodePtr->left); else // item > nodePtr->value removeNode(item, nodePtr->right); }

BST makeRemoval Method (Private) void BinTree::makeRemoval(TreeNode *&nodePtr) { TreeNode *tempPtr; if (nodePtr == NULL) cout << "Cannot remove empty node.\n"; else if (nodePtr->right == NULL) { // no right child tempPtr = nodePtr; nodePtr = nodePtr->left; // reattach left child delete tempPtr; } else if (nodePtr->left == NULL) { // no left child tempPtr = nodePtr; nodePtr = nodePtr->right; // reattach right child delete tempPtr; }... This is where *&nodePtr becomes important.

BST makeRemoval Method (cont.) else { // node has right and left children // check the right subtree tempPtr = nodePtr->right; // got to end of left subtree while (tempPtr->left) tempPtr = tempPtr->left; // reattach left subtree tempPtr->left = nodePtr->left; tempPtr = nodePtr; // reattach right subtree nodePtr = nodePtr->right; delete tempPtr; }