 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 15 Trees and Tree Traversals Instructor: Prof. Zhigang Zhu Department of Computer Science City College of New.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
1 Trees III: Binary Search Trees. 2 A forest full of trees The generic toolkit of functions we have seen thus far can be applied to many types of data.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Trees Main and Savitch Chapter 10. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CSC212 Data Structure - Section AB
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Complete Binary Trees Chapter 9 introduces trees.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Using a Stack Chapter 6 introduces the stack data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
CSC212 Data Structure - Section KL
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Complete Binary Trees Chapter 9 introduces trees.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation illustrates how another data type called a dictionary is implemented with binary search trees. Binary Search Trees

The Dictionary Data Type   A dictionary is a collection of items, similar to a bag.   But unlike a bag, each item has a string attached to it, called the item's key.

The Dictionary Data Type   A dictionary is a collection of items, similar to a bag.   But unlike a bag, each item has a string attached to it, called the item's key. Example: The items I am storing are records containing data about a state.

The Dictionary Data Type   A dictionary is a collection of items, similar to a bag.   But unlike a bag, each item has a string attached to it, called the item's key. Example: The key for each record is the name of the state. Washington

The Dictionary Data Type   The insertion procedure for a dictionary has two parameters. void dictionary::insert(The key for the new item, The new item); Washington

The Dictionary Data Type   When you want to retrieve an item, you specify the key... Item dictionary::retrieve("Washington");

The Dictionary Data Type  When you want to retrieve an item, you specify the key and the retrieval procedure returns the item.

The Dictionary Data Type   We'll look at how a binary tree can be used as the internal storage mechanism for the dictionary.

Arizona Arkansas Colorado A Binary Search Tree of States The data in the dictionary will be stored in a binary tree, with each node containing an item and a key. Washington Oklahoma Florida Mass. New Hampshire West Virginia

Colorado Arizona Arkansas A Binary Search Tree of States Washington Oklahoma Colorado Florida Mass. New Hampshire West Virginia Storage rules:   Every key to the left of a node is alphabetically before the key of the node.

Arizona Colorado Arkansas A Binary Search Tree of States Storage rules:   Every key to the left of a node is alphabetically before the key of the node. Washington Oklahoma Florida Mass. New Hampshire West Virginia Example: “Massachusetts” and “ New Hampshire” are alphabetically before “Oklahoma”

Arizona Arkansas A Binary Search Tree of States Storage rules:   Every key to the left of a node is alphabetically before the key of the node.   Every key to the right of a node is alphabetically after the key of the node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas A Binary Search Tree of States Storage rules:   Every key to the left of a node is alphabetically before the key of the node. .  Every key to the right of a node is alphabetically after the key of the node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas Retrieving Data Start at the root.   If the current node has the key, then stop and retrieve the data.   If the current node's key is too large, move left and repeat 1-3.   If the current node's key is too small, move right and repeat 1-3. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas Retrieve " New Hampshire" Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root.   If the current node has the key, then stop and retrieve the data.   If the current node's key is too large, move left and repeat 1-3.   If the current node's key is too small, move right and repeat 1-3.

Arizona Arkansas Retrieve "New Hampshire" Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root.   If the current node has the key, then stop and retrieve the data.   If the current node's key is too large, move left and repeat 1-3.   If the current node's key is too small, move right and repeat 1-3.

Arizona Arkansas Retrieve "New Hampshire" Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root.   If the current node has the key, then stop and retrieve the data.   If the current node's key is too large, move left and repeat 1-3.   If the current node's key is too small, move right and repeat 1-3.

Arizona Arkansas Retrieve "New Hampshire" Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Start at the root.   If the current node has the key, then stop and retrieve the data.   If the current node's key is too large, move left and repeat 1-3.   If the current node's key is too small, move right and repeat 1-3.

Arizona Arkansas Adding a New Item with a Given Key   Pretend that you are trying to find the key, but stop when there is no node to move to.   Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire

Arizona Arkansas Adding   Pretend that you are trying to find the key, but stop when there is no node to move to.   Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding   Pretend that you are trying to find the key, but stop when there is no node to move to.   Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding   Pretend that you are trying to find the key, but stop when there is no node to move to.   Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding   Pretend that you are trying to find the key, but stop when there is no node to move to.   Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding   Pretend that you are trying to find the key, but stop when there is no node to move to.   Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding   Pretend that you are trying to find the key, but stop when there is no node to move to.   Add the new node at the spot where you would have moved to if there had been a node. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Arizona Arkansas Adding Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Where would you add this state? Kazakhstan

Arizona Arkansas Adding Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan is the new right child of Iowa? Kazakhstan

Arizona Arkansas Removing an Item with a Given Key   Find the item.   If necessary, swap the item with one that is easier to remove.   Remove the item. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan

Arizona Arkansas Removing "Florida"   Find the item. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan Florida cannot be removed at the moment...

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... because removing Florida would break the tree into two pieces.

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan The problem of breaking the tree happens because Florida has 2 children.   If necessary, do some rearranging.

Arizona Arkansas Removing "Florida"   If necessary, do some rearranging. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan For the rearranging, take the smallest item in the right subtree...

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan Iowa...copy that smallest item onto the item that we're removing...   If necessary, do some rearranging.

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... and then remove the extra copy of the item we copied...   If necessary, do some rearranging.

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan... and reconnect the tree   If necessary, do some rearranging.

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Kazakhstan Why did I choose the smallest item in the right subtree?

Arizona Arkansas Removing "Florida" Washington Oklahoma Colorado West Virginia Mass. New Hampshire Iowa Kazakhstan Because every key must be smaller than the keys in its right subtree

Removing an Item with a Given Key   Find the item.   If the item has a right child, rearrange the tree:  Find smallest item in the right subtree  Copy that smallest item onto the one that you want to remove  Remove the extra copy of the smallest item (making sure that you keep the tree connected) else just remove the item.

 Binary search trees are a good implementation of data types such as sets, bags, and dictionaries.  Searching for an item is generally quick since you move from the root to the item, without looking at many other items.  Adding and deleting items is also quick.  But as you'll see later, it is possible for the quickness to fail in some cases -- can you see why? Summary

T HE E ND Presentation copyright 2010 Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.