CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

COL 106 Shweta Agrawal and Amit Kumar
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Binary Search Trees1 Part-F1 Binary Search Trees   
CSE 326: Data Structures Binary Search Trees Ben Lerner Summer 2007.
Trees and Red-Black Trees Gordon College Prof. Brinton.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
© 2004 Goodrich, Tamassia (2,4) Trees
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples.
4-Searching1 Searching Dan Barrish-Flood. 4-Searching2 Dynamic Sets Manage a changing set S of elements. Every element x has a key, key[x]. Operations:
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
Binary Trees Chapter 6.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
CS 361 – Chapter 5 Priority Queue ADT Heap data structure –Properties –Internal representation –Insertion –Deletion.
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.
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.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
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.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
CS 367 Introduction to Data Structures Lecture 8.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Nov 2, 2001CSE 373, Autumn Hash Table example marking deleted items + choice of table size.
Binary Search Trees What is a binary search tree?
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Trees.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Trees, Binary Search Trees
Bit Vector Linked List (Sorted and Unsorted) Hash Table Search Trees
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Find in a linked list? first last 7  4  3  8 NULL
Binary Search Trees (13.1/12.1)
Binary Trees, Binary Search Trees
Mark Redekopp David Kempe
Chapter 12&13: Binary Search Trees (BSTs)
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree

Sorted Dictionary Still want to access elements by their key value. Also: maintain elements “in order”. Besides insert, delete, find, we add these functions: –predecessor (x) –successor (x) Generalization: in case x is itself not in the dictionary –closestBefore (x) –closestAfter (x)

Implementation A hash table would not be good –Since elements not sorted in any way, searching would be a waste of time. A simple implementation: sorted table/array –Insert & delete operations require shifting. √ –Search: binary search strategy runs in O(log n) time. Cut the search space in half until you find value, or conclude it doesn’t exist. Does this list contain 52? 5,8,12, 13, 15, 18, 20, 25, 30, 32, 36, 38, 44, 45, 58, 61, 62, 77, 80 –This is how a spell checker works. –Finding the pred/succ also O(log n).

Implementation #2 The classic way to implement a sorted dictionary ADT is the binary search tree. Seeks to make insert & delete more efficient than sorted table. Properties of BST –For all nodes: left child  you  right child –Or better yet: all in L subtree  you  all elements in R subtree –Not a heap: no restriction on height. Internal rep’n may be array, but: –Convenient to conceive of logically as a tree. –Unlike a heap, array would be space-inefficient.

BST functions To understand BST, we should consider how to: Search for a key value Find the max and min values Find the pred or succ Insert Delete

Search, min, max Search(k) –Just follow binary search strategy. –Starting at root, follow left or right child… until you find k or discover it can’t exist. –May be recursive or just a while loop. Next, how would you find …? –Min key value in the tree –Max key value in the tree

Successor Pred & succ are analogous/symmetric cases, so let’s just look at successor function. Try example. succ(x): if right(x) not empty return min(right(x)) else // succ is lowest ancestor of x whose // left child is also ancestor of x y = parent(x) while y && x == right(y) x = y y = parent(x) return y The while loop goes up the tree until x is the left child of its parent

Insert, delete Insert(k) is like search –The new key becomes a left/right child as appropriate. –Special case if tree initially empty. Delete(victim) – Careful… there are 3 cases, depending on how many children the victim has. –If victim has no children: it’s a leaf. Just tell parent to set its left or right child pointer to null. –If victim has 1 child: Reset its parent and child to point to each other now. –What if victim has 2 children?

Delete, continued Deleting a node that has 2 children: –Replace the victim with succ(victim) –Now, need to repair pointers where succ(victim) came from. Turns out that succ(victim) has at most 1 child. It can’t have a left child… if it did, then it’s not really the succ. Go back to how we calculated succ(x) to see why it’s true. So, treat the removal of succ(victim) like the 0 or 1 child delete case. Try example