Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Department of Computer Science University of Maryland, College Park
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,
Main Index Contents 11 Main Index Contents Tree StructuresTree Structures (3 slides) Tree Structures Tree Node Level and Path Len. Tree Node Level and.
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.
C++ is Fun – Part 14 at Turbine/Warner Bros.! Russell Hanson.
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.
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.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
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.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
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.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
CSCS-200 Data Structure and Algorithms Lecture
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.
Binary Search Trees (BST)
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.
Data Structures Using C++ 2E Chapter 11 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.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
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.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Definition and Application of Binary Trees
Data Structure and Algorithms
Recursive Objects (Part 4)
Problems with Linked List (as we’ve seen so far…)
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
CMSC 341 Introduction to Trees.
Binary Search Trees.
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Trees 3: The Binary Search Tree
Chapter 21: Binary Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Operations on Binary Tree
Prepared by: S. Kondakci
Chapter 10 1 – Binary Trees Tree Structures (3 slides)
2018, Fall Pusan National University Ki-Joune Li
Pointers & Dynamic Data Structures
Chapter 20: Binary Trees.
Yan Shi CS/SE 2630 Lecture Notes
Data Structures Using C++ 2E
Presentation transcript:

Binary Search Trees

2 Overview Recursive linked list ops Binary Trees

3 Recursion w/ LL Recursion can be used to traverse a linked list Any recursive solution has an iterative counterpart –recursive is sometime more elegant though 2 functions : –countNodes(ListNode*); –showReverse(ListNode*);

4 Counting Nodes in a List int NumberList::numNodes() { return countNodes(head); } int NumberList::countNodes(ListNode* nodePtr) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } Must provide public member function. Recursive function must start at head, but head can’t be passed in from outside the class.

5 Counting Nodes in a List int NumberList::countNodes(ListNode* nodePtr) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } 534 Head Null

6 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null stack

7 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } stack

8 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + countNodes(NULL); else return 0; } stack

9 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + countNodes(NULL); else return 0; } int NumberList::countNodes(NULL) { if(nodePtr != NULL) return 1 + countNodes(nodePtr->next); else return 0; } stack

10 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + countNodes(3016); else return 0; } int NumberList::countNodes(3016) { if(nodePtr != NULL) return 1 + 0; else return 0; } stack

11 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + countNodes(3008); else return 0; } 534 Head Null int NumberList::countNodes(3008) { if(nodePtr != NULL) return 1 + 1; else return 0; } stack

12 Counting Nodes in a List int NumberList::countNodes(3000) { if(nodePtr != NULL) return 1 + 2; else return 0; } 534 Head Null stack finally returns 3 to numNodes

13 Displaying Nodes in Reverse Order int NumberList::displayBackwards() { return showReverse(head); } int NumberList::showReverse(ListNode* nodePtr) { if(nodePtr != NULL) { showReverse(nodePtr->next); cout value << “ “; } Must provide public member function. Recursive function must start at head, but head can’t be passed in from outside the class.

14 Counting Nodes in a List void NumberList::showReverse(ListNode* nodePtr) { if(nodePtr != NULL) { showReverse(nodePtr->next); cout value << “ “; } 534 Head Null

15 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null

16 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; }

17 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }

18 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; } void NumberList::showReverse(NULL) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }

19 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; } void NumberList::showReverse(3016) { if(nodePtr != NULL) { showReverse(NULL); cout value << “ “; }

20 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null void NumberList::showReverse(3008) { if(nodePtr != NULL) { showReverse(3016); cout value << “ “; }

21 Counting Nodes in a List void NumberList::showReverse(3000) { if(nodePtr != NULL) { showReverse(3008); cout value << “ “; } 534 Head Null

22 Binary Trees

23 Binary Tree –non linear linked structure –each node may point to 2 other nodes –every node has exactly 1 predecessor tree pointer

24 Binary Tree tree pointer p f a t z null

25 Binary Tree Terminology tree pointer p f a t z null Left Subtree Leaf Node Root Node

Binary Search Tree class interface #ifndef INTBINARYTREE_H #define INTBINARYTREE_H class IntBinaryTree { private: struct TreeNode {int value; TreeNode *left; TreeNode *right; }; TreeNode *root; void insert(TreeNode *&, TreeNode *&); void destroySubTree(TreeNode *); void deleteNode(int, TreeNode *&); void makeDeletion(TreeNode *&); void displayInOrder(TreeNode *); void displayPreOrder(TreeNode *); void displayPostOrder(TreeNode *);

Binary Search Tree class interface public: IntBinaryTree()// Constructor { root = NULL; } ~IntBinaryTree()// Destructor { destroySubTree(root); } void insertNode(int); bool searchNode(int); void remove(int); void showNodesInOrder() {displayInOrder(root); } void showNodesPreOrder() {displayPreOrder(root); } void showNodesPostOrder() {displayPostOrder(root); } }; #endif

Inorder Traversal of a Binary Search Tree void IntBinaryTree::displayInOrder(TreeNode *nodePtr) { if (nodePtr) { displayInOrder(nodePtr->left); cout value << endl; displayInOrder(nodePtr->right); }

Search for a specific value within a Binary Search Tree bool IntBinaryTree::searchNode(int num) {TreeNode *nodePtr = root; while (nodePtr) { if (nodePtr->value == num) return true; else if (num value) nodePtr = nodePtr->left; else nodePtr = nodePtr->right; } return false; }

Insert implementation //********************************************************** // insertNode creates a new node to hold num as its value, // and passes it to the insert function. //********************************************************** void IntBinaryTree::insertNode(int num) { TreeNode *newNode;// Pointer to a new node. // Create a new node and store num in it. newNode = new TreeNode; newNode->value = num; newNode->left = newNode->right = NULL; // Insert the node. insert(root, newNode); }

Insert implementation void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) { if (nodePtr == NULL) nodePtr = newNode; // Insert the node. else if (newNode->value value) insert(nodePtr->left, newNode); // Search the left branch else insert(nodePtr->right, newNode); // Search the right branch }

Delete implementation void IntBinaryTree::remove(int num) {deleteNode(num, root); } //******************************************** // deleteNode deletes the node whose value // member is the same as num. void IntBinaryTree::deleteNode(int num, TreeNode *&nodePtr) { if (num value) deleteNode(num, nodePtr->left); else if (num > nodePtr->value) deleteNode(num, nodePtr->right); else makeDeletion(nodePtr); }

Delete implementation void IntBinaryTree::makeDeletion(TreeNode *&nodePtr) { TreeNode *tempNodePtr; // Temporary pointer, used in reattaching the // left subtree. if (nodePtr == NULL) cout << "Cannot delete empty node.\n"; else if (nodePtr->right == NULL) { tempNodePtr = nodePtr; nodePtr = nodePtr->left; // Reattach the left child delete tempNodePtr; } else

Delete implementation if (nodePtr->left == NULL) { tempNodePtr = nodePtr; nodePtr = nodePtr->right;// Reattach the right child delete tempNodePtr; }

Delete implementation else // If the node has two children. { // Move one node the right. tempNodePtr = nodePtr->right; // Go to the end left node. while (tempNodePtr->left) tempNodePtr = tempNodePtr->left; // Reattach the left subtree. tempNodePtr->left = nodePtr->left; tempNodePtr = nodePtr; // Reattach the right subtree. nodePtr = nodePtr->right; delete tempNodePtr; }