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.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal, Amit Kumar
Advertisements

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.
 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.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
They’re not just binary anymore!
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures Topic #9. Today’s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency.
©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Part B Part A:  Index Definition in SQL  Ordered Indices  Index Sequential.
1 Database indices Database Systems manage very large amounts of data. –Examples: student database for NWU Social Security database To facilitate queries,
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Marc Smith and Jim Ten Eyck
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.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSC 213 – Large Scale Programming. Today’s Goals  Review a new search tree algorithm is needed  What real-world problems occur with old tree?  Why.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
More Trees Multiway Trees and 2-4 Trees. Motivation of Multi-way Trees Main memory vs. disk ◦ Assumptions so far: ◦ We have assumed that we can store.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
 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.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Binary Search Tree vs. Balanced Search Tree. Why care about advanced implementations? Same entries, different insertion sequence: 10,20,30,40,50,60,70,
Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.
File Organization and Processing Week Tree Tree.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
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 Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Binary Search Trees Chapter 7 Objectives
AA Trees.
Trees Chapter 11 (continued)
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
Trees Chapter 11 (continued)
BST Trees
Binary search tree. Removing a node
Binary Search Tree Chapter 10.
Hashing Exercises.
(edited by Nadia Al-Ghreimil)
Complete Binary Trees Chapter 9 introduces trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
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.
Search Sorted Array: Binary Search Linked List: Linear Search
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
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.
(edited by Nadia Al-Ghreimil)
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.
Mark Redekopp David Kempe
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

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 structures derived from binary trees binary search treesNow we will look at a specific ADT based on the generic binary tree: binary search trees

3 BST characteristics Based on binary trees Defining quality has to do with the order in which data are stored Commonly used in database applications where rapid retrieval of data is desired

4 Binary Search Trees total order semanticsEntries in a BST must be objects to which total order semantics apply -- in other words, objects for which all the binary comparison operators are defined Storage rules -- for every node n: –every entry in n’s left subtree is less than or equal to the entry in n –every entry in n’s right subtree is greater than n’s entry

5 Binary Search Tree There is no special requirement for the tree to maintain a particular shape -- but balanced a balanced tree (in which there are approximately the same number of nodes in each subtree) facilitates data searching 12

6 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.

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

8 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

9 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

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

11 Item Dictionary::retrieve("Washington"); The Dictionary Data Type p When you want to retrieve an item, you specify the key and the retrieval procedure returns the item.

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

13 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

14 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.

15 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'

16 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

17 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

18 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

19 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.

20 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.

21 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.

22 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.

23 Arizona Arkansas ¶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 Adding a New Item with a Given Key

24 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

25 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

26 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

27 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

28 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

29 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

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

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

32 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

33 Arizona Arkansas Removing 'Florida' ¶Find the item. Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa Kazakhstan

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

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

36 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. The problem of breaking the tree happens because Florida has 2 children. ·If necessary, do some rearranging.

37 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... For the rearranging, take the smallest item in the right subtree...

38 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......copy that smallest item onto the item that we're removing... ·If necessary, do some rearranging.

39 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 and then remove the extra copy of the item we copied... ·If necessary, do some rearranging.

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

41 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? Why did I choose the smallest item in the right subtree?

42 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

43 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.

44 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

45 Trees III: Binary Search Trees - ends -