Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Similar documents
Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Lecture 6: Analysis of Algorithms (CS )

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Associate Professor Dr. Raed Ibraheem Hamed

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Search Trees. Undirected graph Directed graph Tree Binary search tree

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Lecture: Analysis of Algorithms (CS )

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Search Trees: BSTs and B-Trees

Binary Trees. Recursive definition. Is this a binary tree?

Trees. (Trees) Data Structures and Programming Spring / 28

Binary Trees, Binary Search Trees

SFU CMPT Lecture: Week 9

Binary Trees

Binary Search Trees. See Section 11.1 of the text.

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

A set of nodes (or vertices) with a single starting point

CS350: Data Structures Red-Black Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

COMP Analysis of Algorithms & Data Structures

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CS102 Binary Search Trees

Garbage Collection: recycling unused memory

a graph is a data structure made up of nodes in graph theory the links are normally called edges

COMP Analysis of Algorithms & Data Structures

Binary search trees (BST) Binary search trees (BST)

Binary Search Trees. Analysis of Algorithms

Algorithms. Deleting from Red-Black Trees B-Trees

Binary Search Trees. What is a Binary Search Tree?

TREES. Trees - Introduction

Augmenting Data Structures

Binary Trees and Binary Search Trees

Data Structures and Algorithms for Engineers

Binary Search Trees Treesort

Data Structures Lesson 7

INF2220: algorithms and data structures Series 1

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

CS301 - Data Structures Glossary By

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Algorithms. AVL Tree

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it

) $ f ( n) " %( g( n)

Programming II (CS300)

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

We assume uniform hashing (UH):

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

Data Structures and Algorithms

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Data Structure. IBPS SO (IT- Officer) Exam 2017

Advanced Set Representation Methods

Lecture Notes for Advanced Algorithms

Binary Search Tree (3A) Young Won Lim 6/6/18

Successor/Predecessor Rules in Binary Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CSCI Trees. Mark Redekopp David Kempe

Chapter 20: Binary Trees

ץע A. B C D E F G H E, B ( -.) - F I J K ) ( A :. : ע.)..., H, G E (. י : י.)... C,A,, F B ( 2

Priority Queues, Binary Heaps, and Heapsort

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

COMP Analysis of Algorithms & Data Structures

Binary Search Tree (3A) Young Won Lim 6/4/18

If you took your exam home last time, I will still regrade it if you want.

Binary Search Tree (3A) Young Won Lim 6/2/18

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Programming II (CS300)

Trees. Truong Tuan Anh CSE-HCMUT

CSE 530A. B+ Trees. Washington University Fall 2013

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

COMP171. AVL-Trees (Part 1)

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Lecture 23: Binary Search Trees

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

CISC 235: Topic 4. Balanced Binary Search Trees

Friday Four Square! 4:15PM, Outside Gates

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Trees. Tree Structure Binary Tree Tree Traversals

Data Structures in Java

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Binary search trees :

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

DATA STRUCTURES AND ALGORITHMS

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Module 4: Dictionaries and Balanced Search Trees

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

Lecture 7. Binary Search Trees / AVL Trees

Transcription:

Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs

ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in C++ (Third Edition), 1998.

ISE 172 Lecture 16 2 Binary Search Trees To use the BST data structure, the keys must have an order. As with heaps, a binary search tree is a binary tree with additional structure to support more operations (at a cost). Listing the items in sorted order Finding the k th items in sorted order In a binary tree, the key value of any node is greater than or equal to the key value of all nodes in its left subtree; less than or equal to the key value of all nodes in its right subtree. For now, we will assume that all keys are unique. With this simple structure, we can implement all functions efficiently.

ISE 172 Lecture 16 3 Searching Search in a BST can be implemented recursively in a fashion similar to binary search, starting with the root as the current node. If the pointer to the current node is None, then return None. Otherwise, compare the search key to the current node s key, if it exists. If the keys are equal, then return a pointer to the current node. If the search key is smaller, recursively search in the left subtree. If the search key is larger, recursively search in the right subtree. What is the running time of this operation?

ISE 172 Lecture 16 4 Inserting a Node The procedure for inserting a node is similar to that for searching. As before, we will assume there is no item with an identical key already in the tree. We simply perform an unsuccessful search and insert the node in place of the final None pointer at the end of the search path. This places it where we would expect to find it the next time we look. The running time is the same as searching. Constructing a BST from a given list of elements can be done by iteratively inserting each element.

ISE 172 Lecture 16 5 Finding the Minimum and Maximum Finding the minimum and maximum is a simple procedure. The minimum is the leftmost node in the tree. The maximum is the rightmost node in the tree.

ISE 172 Lecture 16 6 Sorting We can easily read off the items from a BST in sorted order. This involves walking the tree in a specified way. Walking the tree is done recursively by first walking the left subtree and then the right subtree. Recall that this leads to three different orders in which we can display the key values in the tree. To display the values in preorder, print the value of the current node before recursively walking the two subtrees. To display the values in inorder, print the value of the current node after walking the left subtree, but before walking the right subtree. To display the values in postorder, print the value of the current node after walking both subtrees. Which display order will result in the printing of a sorted list?

ISE 172 Lecture 16 7 Finding the Predecessor and Successor To find the successor of a node x, think of an inorder tree walk. After visiting a given node, what is the next value to get printed out? We need to examine two cases. If x has a right child, then the successor is the node with the minimum key in the right subtree (easy to find). Otherwise, the successor is the lowest ancestor of x whose left child is also an ancestor of x (why?). To find such a node, we follow the path to the root until we reach a node that is the left child of its parent. Note that if a node has two children, its successor cannot have a left child (why not?). Finding the predecessor works the same way.

ISE 172 Lecture 16 8 Deleting a Node Deleting a node z from a BST is more complicated than other operations because of the rigid structure that must be maintained. There are a number of algorithms for doing this. The most straightforward implementation considers three cases. If z has no children, then simply set the pointer to z in the parent to be None. If z has one child, then replace z with its child. If z has two children, then delete either the predecessor or the successor and then replace z with it. Why does this work?

ISE 172 Lecture 16 9 Performance of BSTs Efficiency of the basic operations depends on the depth of the tree. Consider the search operation: what is the best case? The best case is to make the same comparisons as in binary search. However, this can only happen if the root of each subtree is the median element of that subtree, i.e., the tree is balanced. Fortunately, if keys are added at random, this should be the case on average. Like quicksort, the average performance is very good, but worst case behavior is easy to find (where?). In fact, quicksort and BSTs exhibit worst case behavior on the same inputs! As with quicksort, one can show that for a random sequence of keys, the average depth of the tree is 2 ln n 1.39 lg n. Again, the average depth is only 40% higher than the best possible. Building a binary search tree has the same running time as quicksort!

ISE 172 Lecture 16 10 Handling Duplicate Keys What happens when the tree may contain duplicate keys? To make things easier, we can always insert items with duplicate keys in the right subtree. To find all items with the same key, search for the first item and then recursively search for the same item in the right subtree. Alternatively, we could maintain a linked list of items with the same key at each node in the tree.

ISE 172 Lecture 16 11 Selection Recall that the selection problem is that of finding the k th element in an ordered list. Selection can be done using an algorithm similar to the quicksort algorithm (notice the connection again). However, we need an additional data member count in the node class that tracks the size of the subtree rooted at each node. With this additional data member, we can recursively search for the k th element. Starting at the root, if the size of the left subtree is k 1, return a pointer to the root. If the size of the left subtree is more than k 1, recursively search for the k th element of the left subtree. Otherwise, recursively search for the (k t 1) th element of the right subtree, where t is the size of the left subtree. Note that maintaining the count data member can be expensive.

ISE 172 Lecture 16 12 Rotation and Balancing To guard against poor performance, we would like to have a scheme for keeping the tree balanced. There are many schemes for automatically maintaining balance. We describe here a method of manually rebalancing the tree. The basic operation that we ll need is that of rotation. Rotating the tree means changing the root from the current root to one of its children, while maintaining the BST structure. To change the right child of the current root into the new root. Make the current root the left child of the new root. Make the left child of the new root the right child of the old root. Note that we can make any node the root of the BST through a sequence of rotations.

ISE 172 Lecture 16 13 Partitioning and Rebalancing To partition the list around the k th item, select the k th item and rotate it to the root. This can be implemented easily in a recursive fashion. The left and right subtrees form the desired partition. To (re)balance a BST Partition around the middle node. Recursively balance the left and right subtrees. This operation can be called periodically What is the running time of this operation?

ISE 172 Lecture 16 14 Another Implementation of Delete Using the partition operation, we can implement delete in a slightly different way. Partition the right subtree of the node to be deleted around its smallest element x. Make the root of the left subtree the left child of x.

ISE 172 Lecture 16 15 Root Insertion and Joining Often it is useful to be able to insert a node as the root of the BST. This can be done easily by inserting it as usual and then rotating it to the root, i.e., partition around it. With root insertion, we can define a recursive method to join two BSTs. Insert the root of the first tree as the root of the second. Recursively join the pairs of left and right subtrees.

ISE 172 Lecture 16 16 Randomized BSTs Recall that we used randomization to guard against the worst case behavior of quicksort. We can do the same here. The procedure for randomly inserting into a BST of size n is as follows. With probability 1/(n + 1), perform root insertion. Otherwise, recursively insert into the right or left subtree, as appropriate, using the same method. One can prove mathematically that this is the same as randomly ordering the elements first and then inserting them as usual. Hence, this should guard against common worst-case inputs.