 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
COL 106 Shweta Agrawal, Amit Kumar
Advertisements

Info 3.3. Chapter 3.3 Recursive Data Structures Part 2 : Binary Trees.
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.
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!
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
1 B trees Nodes have more than 2 children Each internal node has between k and 2k children and between k-1 and 2k-1 keys A leaf has between k-1 and 2k-1.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
© 2004 Goodrich, Tamassia Binary Search Trees   
Binary Search Trees1 Part-F1 Binary Search Trees   
Chapter 10 Heaps Anshuman Razdan Div of Computing Studies
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.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
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.
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.
Data Structures – Binary Tree
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
INTRODUCTION TO AVL TREES P. 839 – 854. INTRO  Review of Binary Trees: –Binary Trees are useful for quick retrieval of items stored in the tree –order.
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.
Balanced Search Trees Chapter Chapter Contents AVL Trees Single Rotations Double Rotations Implementation Details 2-3 Trees Searching Adding Entries.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
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.
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.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Starting at Binary 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.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
© 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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
AA 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.
CSC212 Data Structure - Section AB
BST Trees
Binary search tree. Removing a node
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Binary Search Tree In order Pre order Post order Search Insertion
Chapter 20: Binary Trees.
(edited by Nadia Al-Ghreimil)
Complete Binary Trees Chapter 9 introduces trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 21: Binary Trees.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
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.
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.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
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)
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 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
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.
Data Structures – Binary Tree
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   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 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