Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.

Slides:



Advertisements
Similar presentations
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Advertisements

Trees Types and Operations
Skip List & Hashing CSE, POSTECH.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Search Trees Definition Of Binary Search Tree A binary tree. Each node has a (key, value) pair. For every node x, all keys in the left subtree.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Dictionaries Collection of items. Each item is a pair.  (key, element)  Pairs have different keys.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Binary Search Trees Dictionary Operations:  IsEmpty()  Search(key)  Insert(key, value)  Delete(key)
BST Data Structure A BST node contains: A BST contains
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
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.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Tree.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CS261 – Recitation 5 Fall Outline Assignment 3: Memory and Timing Tests Binary Search Algorithm Binary Search Tree Add/Remove examples 1.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Tree 황승원 Fall 2011 CSE, POSTECH 2 2 Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Binary Search Trees Dictionary Operations:  get(key)  put(key, value)  remove(key) Additional operations:  ascend()  get(index) (indexed binary search.
Binary Search Trees Dictionary Operations:  search(key)  insert(key, value)  delete(key) Additional operations:  ascend()  IndexSearch(index) (indexed.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Dictionaries Collection of items. Each item is a pair. (key, element)
Binary Search Trees Chapter 7 Objectives
Heap Chapter 9 Objectives Define and implement heap structures
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Binary Search Trees Dictionary Operations: Additional operations:
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Binary Search Trees < > =
Binary Search Trees.
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Binary Search Trees CSE, POSTECH

Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and hash tables – Particularly ideal for accessing data sequentially or by rank In this chapter, we will learn – Binary search trees – Indexed binary search trees

Binary Search Tree Definition A binary tree that may be empty. A nonempty binary search tree satisfies the following properties: 1. Each node has a key (or value), and no two nodes have the same key (i.e., all keys are distinct). 2. For every node x, all keys in the left subtree of x are smaller than that in x. 3. For every node x, all keys in the right subtree of x are larger than that in x. 4. The left and right subtrees of the root are also binary search trees

Examples of Binary Trees Which of the above trees are binary search trees?  (b) and (c) Why isn’t (a) a binary search tree?  It violates the property #3 Figure 14.1 Binary Trees

Indexed Binary Search Trees Definition Binary search tree. Each node has an additional field ‘LeftSize’. Support search and delete operations by rank as well as all the binary search tree operations. LeftSize – the number of elements in its left subtree – the rank of an element with respect to the elements in its subtree (e.g., the fourth element in sorted order)

Indexed Binary Search Tree Example What is the Leftsize for each node? LeftSize values are in red

LeftSize and Rank Rank of an element is its position in inorder (inorder = ascending key order). [2,6,7,8,10,15,18,20,25,30,35,40] rank(2)=0 rank(15)=5 rank(20)=7 LeftSize(x) = rank(x) with respect to elements in the subtree rooted at x See Figure 14.2 for more examples

Exercise Do Exercise 14.1

The bsTree ADT See ADT 14.1 for bsTree ADT See ADT 14.2 for IndexedBSTree ADT See Programs 14.1 and 14.2 for C++ abstract class definitions for bsTree and IndexedBSTree

The Class binarySearchTree Since the number of elements in a binary search tree as well as its shape changes as operations are performed, a binary search tree is usually represented using the linked representation of Section We can define binarySearchTree as a derived class of linkedBinaryTree (Section 11.8)

The Operation Ascend() How can we output all elements in ascending order of keys? Do an inorder traversal (left, root, right). What would be the output? 2, 6, 8, 10, 15, 20, 25, 30,

The Operation Search(key, e) Search begins at the root If the root is NULL, the search tree is empty and the search fails. If key is less than the root, then left subtree is searched If key is greater than the root, then right subtree is searched If key equals the root, then the search terminates successfully The time complexity for search is O(height) See Program 11.4 for the search operation code

The Operation Insert(key, e) To insert a new element e into a binary search tree, we must first verify that its key does not already exist by performing a search in the tree If the search is successful, we do not insert If the search is unsuccessful, then the element is inserted at the point the search terminated – Why insert it at that point? The time complexity for insert is O(height) See Figure 14.3 for examples See Program 14.5 for the insert operation code

Insert Example We wish to insert an element with the key 35. Where should it be inserted?

Insert Example Insert an element with the key

Insert Example Insert an element with the key

The Operation Delete(key, e) For deletion, there are three cases for the element to be deleted: 1. Element is in a leaf. 2. Element is in a degree 1 node (i.e., has exactly one nonempty subtree). 3. Element is in a degree 2 node (i.e., has exactly two nonempty subtrees).

Case 1: Delete from a Leaf For case 1, we can simply discard the leaf node. Example, delete a leaf element. key=

Case 1: Delete from a Leaf Delete a leaf element. key=

Case 2: Delete from a Degree 1 Node Which nodes have a degree 1? Example: Delete key=

Case 2: Delete from a Degree 1 Node Delete from a degree 1 node. key=15

Case 3: Delete from a Degree 2 Node Which nodes have a degree 2? Example: Delete key=10

Replace with the largest key in the left subtree (or the smallest in the right subtree) Which node is the largest key in the left subtree? Case 3: Delete from a Degree 2 Node

The largest key must be in a leaf or degree 1 node. Case 3: Delete from a Degree 2 Node

Note that the node with largest key in the left subtree (as well as that with smallest in the right subtree) is guaranteed to be in a node with either zero or one nonempty subtree How can we find the node with largest key in the left subtree of a node?  by moving to the root of that subtree and then following a sequence of right-child pointers until we reach a node whose right-child pointer is NULL How can we find the node with smallest key in the right subtree of a node?  by moving to the root of that subtree and then following a sequence of left-child pointers until we reach a node whose left-child pointer is NULL

Another Delete from a Degree 2 Node Delete from a degree 2 node. key=20 Replace with the largest in the left subtree

Another Delete from a Degree 2 Node The time complexity of delete is O(height). See more delete examples in Figure 14.4 See Program 14.6 for the delete operation code The result is

Binary Search Trees with Duplicates We can remove the requirement that all elements in a binary search tree need distinct keys How? – Replace “smaller” in property 2 by “smaller or equal to” – Replace “larger” in property 3 by “larger or equal to” Then binary search trees can have duplicate keys

The Class dBSTree The binary search tree with duplicates (dBSTree) We can implement this by changing the while loop of binarySearchTree::Insert (Program 14.5) See Program 14.7 for the new while loop for dBSTree

Indexed Binary Search Tree – Search & Delete IndexSearch(rank) returns the rank th element IndexDelete(rank) deletes the rank th element If rank = x.LeftSize, desired element is x.element. If rank < x.LeftSize, desired element is rank th element in left subtree of x. If rank > x.LeftSize, desired element is (rank-(x.LeftSize+1)) th element in right subtree of x. Read Section 14.5

Indexed Binary Search Example What is the 4 th element in this IndexedBST? What is the 10 th element in this IndexedBST?

READING Do Exercise 14.7 Read Sections