1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.

Slides:



Advertisements
Similar presentations
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Advertisements

Chapter 8 Binary Search Tree 1 Fall Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.
1 Nell Dale Chapter 9 Trees Plus Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Binary Search Trees CS 308 – Data Structures What is a binary tree? Property1: each node can have up to two successor nodes (children) –The predecessor.
1 Chapter 10 Trees. Tree Definition 1. A tree is a connected undirected graph with no simple circuits. Theorem 1. An undirected graph is a tree if and.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
EC-211 DATA STRUCTURES LECTURE Tree Data Structure Introduction –The Data Organizations Presented Earlier are Linear in That Items are One After.
1 Chapter 8 Binary Search Trees. 2 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
1 CS308 Data Structures An application of binary trees: Binary Expression Trees.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 19: Binary Trees.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 8 Binary Search Trees. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop.
Binary Search Trees. What is a binary tree? Property 1: each node can have up to two successor nodes.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Chapter 8 Binary Search Trees. 2 Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
Chapter 8 Binary Search Trees 1. Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Jake’s Pizza Shop 2.
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.
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.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Trees.
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.
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.
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.
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)
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Chapter 11 Binary Trees Dr. Youssef Harrath
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Binary Search Trees CS Goals Define and use the following terminology: binary tree root descendant subtree binary search tree parent level ancestor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
Binary Trees and Binary Search Trees
C++ Plus Data Structures
Section 8.1 Trees.
Binary Search Trees.
Chapter 8 Binary Search 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.
C++ Plus Data Structures
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
CMSC 341 Binary Search Trees.
CMSC 341 Binary Search Trees.
Chapter 8 Binary Search Tree
Chapter 20: Binary Trees.
Yan Shi CS/SE 2630 Lecture Notes
Tree traversals BST properties Search Insertion
C++ Plus Data Structures
Presentation transcript:

1 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures

2 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len

3 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Tree Has a Root Node ROOT NODE

4 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Leaf nodes have no children LEAF NODES

5 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Tree Has Levels LEVEL 0

6 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Level One LEVEL 1

7 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Level Two LEVEL 2

8 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len A Subtree LEFT SUBTREE OF ROOT NODE

9 Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len Another Subtree RIGHT SUBTREE OF ROOT NODE

10 A binary tree is a structure in which: Each node can have at most two children, and in which a unique path exists from the root to every other node. The two children of a node are called the left child and the right child, if they exist. Binary Tree

11 A Binary Tree Q V T K S A E L

12 How many leaf nodes? Q V T K S A E L

13 How many descendants of Q? Q V T K S A E L

14 How many ancestors of K? Q V T K S A E L

15 Implementing a Binary Tree with Pointers and Dynamic Data Q V T K S A E L

16 Each node contains two pointers template struct TreeNode { ItemType info; // Data member TreeNode * left; // Pointer to left child TreeNode * right; // Pointer to right child };. left. info. right NULL ‘A’ 6000

// BINARY SEARCH TREE SPECIFICATION template class TreeType { public: TreeType ( ); // constructor ~TreeType ( ); // destructor bool IsEmpty ( ) const; bool IsFull ( ) const; int NumberOfNodes ( ) const; void InsertItem ( ItemType item ); void DeleteItem (ItemType item ); void RetrieveItem ( ItemType& item, bool& found ); void PrintTree (ofstream& outFile) const;... private: TreeNode * root; }; 17

18 TreeType CharBST; ‘J’ ‘E’ ‘A’ ‘S’ ‘H’ TreeType ~TreeType IsEmpty InsertItem Private data: root RetrieveItem PrintTree.

19 A special kind of binary tree in which: 1. Each node contains a distinct data value, 2. The key values in the tree can be compared using “greater than” and “less than”, and 3. The key value of each node in the tree is less than every key value in its right subtree, and greater than every key value in its left subtree. A Binary Search Tree (BST) is...

20 Depends on its key values and their order of insertion. Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order. The first value to be inserted is put into the root node. Shape of a binary search tree... ‘J’

21 Thereafter, each value to be inserted begins by comparing itself to the value in the root node, moving left it is less, or moving right if it is greater. This continues at each level until it can be inserted as a new leaf. Inserting ‘E’ into the BST ‘J’ ‘E’

22 Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘F’ into the BST ‘J’ ‘E’ ‘F’

23 Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘T’ into the BST ‘J’ ‘E’ ‘F’ ‘T’

24 Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘A’ into the BST ‘J’ ‘E’ ‘F’ ‘T’ ‘A’

25 is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order? What binary search tree... ‘A’

26 obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order. Binary search tree... ‘A’ ‘E’ ‘F’ ‘J’ ‘T’

27 Another binary search tree Add nodes containing these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’ ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘P’

28 Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’

// BINARY SEARCH TREE SPECIFICATION template class TreeType { public: TreeType ( ) ; // constructor ~TreeType ( ) ; // destructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int NumberOfNodes ( ) const ; void InsertItem ( ItemType item ) ; void DeleteItem (ItemType item ) ; void RetrieveItem ( ItemType& item, bool& found ) ; void PrintTree (ofstream& outFile) const ;... private: TreeNode * root ; }; 29

// SPECIFICATION (continued) // // RECURSIVE PARTNERS OF MEMBER FUNCTIONS template void PrintHelper ( TreeNode * ptr, ofstream& outFile ) ; template void InsertHelper ( TreeNode * & ptr, ItemType item ) ; template void RetrieveHelper ( TreeNode * ptr, ItemType& item, bool& found ) ; template void DestroyHelper ( TreeNode * ptr ) ; 30

// BINARY SEARCH TREE IMPLEMENTATION // OF MEMBER FUNCTIONS AND THEIR HELPER FUNCTIONS // template TreeType :: TreeType ( ) // constructor { root = NULL ; } // template bool TreeType :: IsEmpty( ) const { return ( root == NULL ) ; } 31

template void TreeType :: RetrieveItem ( ItemType& item, bool& found ) { RetrieveHelper ( root, item, found ) ; } template void RetrieveHelper ( TreeNode * ptr, ItemType& item, bool& found) { if ( ptr == NULL ) found = false ; else if ( item info )// GO LEFT RetrieveHelper( ptr->left, item, found ) ; else if ( item > ptr->info ) // GO RIGHT RetrieveHelper( ptr->right, item, found ) ; else { item = ptr->info ; found = true ; } 32

template void TreeType :: InsertItem ( ItemType item ) { InsertHelper ( root, item ) ; } template void InsertHelper ( TreeNode * & ptr, ItemType item ) { if ( ptr == NULL ) { // INSERT item HERE AS LEAF ptr = new TreeNode ; ptr->right = NULL ; ptr->left = NULL ; ptr->info = item ; } else if ( item info )// GO LEFT InsertHelper( ptr->left, item ) ; else if ( item > ptr->info ) // GO RIGHT InsertHelper( ptr->right, item ) ; } 33

34 Inorder Traversal: A E H J M T Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree last Print second

// INORDER TRAVERSAL template void TreeType :: PrintTree ( ofstream& outFile ) const { PrintHelper ( root, outFile ) ; } template void PrintHelper ( TreeNode * ptr, ofstream& outFile ) { if ( ptr != NULL ) { PrintHelper( ptr->left, outFile ) ;// Print left subtree outFile info ; PrintHelper( ptr->right, outFile ) ;// Print right subtree } 35

36 Preorder Traversal: J E A H T M Y ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree secondPrint right subtree last Print first

37 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ tree Print left subtree firstPrint right subtree second Print last Postorder Traversal: A H E M Y T J

template TreeType :: ~TreeType ( )// DESTRUCTOR { DestroyHelper ( root ) ; } template void DestroyHelper ( TreeNode * ptr ) // Post: All nodes of the tree pointed to by ptr are deallocated. { if ( ptr != NULL ) { DestroyHelper ( ptr->left ) ; DestroyHelper ( ptr->right ) ; delete ptr ; } 38