CS16: Introduction to Data Structures & Algorithms

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Chapter 13. Red-Black Trees
COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Addition Facts
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
ABC Technology Project
CSE 373 Data Structures and Algorithms
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Introduction to Algorithms Red-Black Trees
Addition 1’s to 20.
25 seconds left…...
Week 1.
10 -1 Chapter 10 Amortized Analysis A sequence of operations: OP 1, OP 2, … OP m OP i : several pops (from the stack) and one push (into the stack)
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
We will resume in: 25 Minutes.
Chapter 12 Binary Search Trees
CSE Lecture 17 – Balanced trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Binary Search Tree Smt Genap
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.
Chapter 4: Trees Part II - AVL Tree
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Search Trees: BSTs and B-Trees David Kauchak cs302 Spring 2013.
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.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
BST Data Structure A BST node contains: A BST contains
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
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.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
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.
CSIT 402 Data Structures II
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL Trees.
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.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Non Linear Data Structure
AA Trees.
BST Trees
Binary search tree. Removing a node
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
COMP 103 Binary Search Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Monday, April 16, 2018 Announcements… For Today…
Search Sorted Array: Binary Search Linked List: Linear Search
CSC 143 Binary Search Trees.
Presentation transcript:

CS16: Introduction to Data Structures & Algorithms Thursday, February 20, 2014 Binary search trees CS16: Introduction to Data Structures & Algorithms

Outline Binary Search Trees Searching BSTs Adding to BSTs Thursday, February 20, 2014 Outline Binary Search Trees Searching BSTs Adding to BSTs Removing from BSTs BST Analysis Balancing BSTs

Thursday, February 20, 2014 Binary Search Tree Binary search trees (BSTs) are binary trees with a special property For each node All descendants in its left subtree have a lower value All descendants in its right subtree have a higher value An in-order traversal will output the nodes in increasing order, hence the name “in-order” F B H G I A D C E

Searching a BST How do we implement contains() using a BST? Thursday, February 20, 2014 Searching a BST How do we implement contains() using a BST? Suppose we’re looking for 11 in the tree below Starting at the root, each comparison tells us which subtree to look in 13 7 20 15 24 5 10 8 11 11 < 13 11 > 7 11 > 10 found it!

Searching a BST (2) What if an element isn’t in the tree? Thursday, February 20, 2014 Searching a BST (2) What if an element isn’t in the tree? Suppose we are looking for 14 in the same tree 13 7 20 15 24 5 10 8 11 14 > 13 14 < 20 we’ve hit a leaf without finding 14, so it’s not in the tree

Thursday, February 20, 2014 BST contains() function contains(node, toFind): // Input: node - root node of tree // toFind - data of the node you’re trying to find // Output: the node with data toFind or null if toFind is not // in BST if node.data == toFind: return node else if toFind < node.data and node.left != null: return contains(node.left, toFind) else if toFind > node.data and node.right != null: return contains(node.right, toFind) return null

Thursday, February 20, 2014 Inserting into a BST To add an item to a BST, perform the same search to find where it should go An item is always added as a new leaf node Example: add 17 13 7 20 15 24 5 10 8 11 17 > 13 17 < 20 17 > 15 17

BST insert() function insert(node, toInsert): Thursday, February 20, 2014 BST insert() function insert(node, toInsert): // Input: node - root node of tree // toInsert - data you are trying to insert if node.data == toInsert: // data already in tree return if toInsert < node.data: if node.left == null: node.addLeft(toInsert) else: insert(node.left, toInsert) if node.right == null: node.addRight(toInsert) insert(node.right, toInsert)

Removing from a BST Removing an item from a BST is tricky (sometimes). Thursday, February 20, 2014 Removing from a BST Removing an item from a BST is tricky (sometimes). We have three cases to consider: 1) Removing a leaf (easy, just remove it) 2) Removing an internal node with one child 3) Removing an internal node with two children 13 7 20 15 24 5 10 8 11 17

Removing from a BST – Case 2 Thursday, February 20, 2014 Removing from a BST – Case 2 Case 2: Removing an internal node with one child General strategy: “splice” out the node to remove by connecting the node’s parent to the node’s child. Example: remove(15) 13 7 20 15 24 5 10 8 11 17

Removing from a BST – Case 2 Thursday, February 20, 2014 Removing from a BST – Case 2 Case 2: Removing an internal node with one child General strategy: “splice” out the node to remove by connecting the node’s parent to the node’s child. Example: remove(15) We set the parent node’s left reference to the given node’s only child 13 7 20 15 24 5 10 8 11 17

Removing from a BST – Case 2 Thursday, February 20, 2014 Removing from a BST – Case 2 Case 2: Removing an internal node with one child General strategy: “splice” out the node to remove by connecting the node’s parent to the node’s child. Example: remove(15) We set the parent node’s left reference to the given node’s only child There are no more references to the given node, so it is deleted (garbage collected) 13 7 20 15 24 5 10 8 11 17

Removing from a BST – Case 2 Thursday, February 20, 2014 Removing from a BST – Case 2 Case 2: Removing an internal node with one child General strategy: “splice” out the node to remove by connecting the node’s parent to the node’s child. Example: remove(15) We set the parent node’s left reference to the given node’s only child There are no more references to the given node, so it is deleted (garbage collected) BST order is maintained 13 7 20 17 24 5 10 8 11

Removing from a BST – Case 3 Thursday, February 20, 2014 Removing from a BST – Case 3 Case 3: Removing an internal node with two children General strategy: Replace the data of the node to remove with the data of the node’s successor. Delete the successor. The successor is also called the in-order successor, because it comes next in the in-order tree traversal. 13 7 20 17 24 5 10 8 11 9 (first, to help with our example, we made a small addition to this tree)

Removing from a BST – Case 3 Thursday, February 20, 2014 Removing from a BST – Case 3 Case 3: Removing an internal node with two children Example: remove(7) First, let’s find the in-order successor to the given node. Since we know the given node has two children – which guarantees the node has a right subtree – we know that its successor will always be the left-most node in its right subtree. 13 7 20 17 24 5 10 8 11 9 13 7 20 17 24 5 10 8 11 in-order successor to 7

Removing from a BST – Case 3 Thursday, February 20, 2014 Removing from a BST – Case 3 Case 3: Removing an internal node with two children Example: remove(7) Code to find the in-order successor: successor(node): // Input: node – the node for // which to find the successor curr = node.right while (curr.left != null): curr = curr.left return curr 13 7 20 17 24 5 10 8 11 9 13 7 20 17 24 5 10 8 11 in-order successor to 7

Removing from a BST – Case 3 Thursday, February 20, 2014 Removing from a BST – Case 3 Case 3: Removing an internal node with two children Example: remove(7) Second, let’s replace the data of the node to remove with that of its successor. 13 8 20 17 24 5 10 11 9 13 7 20 17 24 5 10 8 11

Removing from a BST – Case 3 Thursday, February 20, 2014 Removing from a BST – Case 3 Case 3: Removing an internal node with two children Example: remove(7) Lastly, remove the successor. Notice that we can make one very important guarantee: the successor cannot have a left child, otherwise that child would have been the in-order successor to 7. Thus, the successor can have at most one (right) child. Therefore, we can delete the successor according to the strategies we defined for Cases 1 and 2. 13 8 20 17 24 5 10 11 9 13 7 20 17 24 5 10 8 11

Removing from a BST – Case 3 Thursday, February 20, 2014 Removing from a BST – Case 3 Case 3: Removing an internal node with two children Example: remove(7) In this case, we remove the successor according to Case 2: internal node with one child. 13 8 20 17 24 5 10 11 9 13 7 20 17 24 5 10 8 11

Removing from a BST – Case 3 Thursday, February 20, 2014 Removing from a BST – Case 3 Case 3: Removing an internal node with two children Example: remove(7) Successor is removed. BST order is maintained. 13 8 20 17 24 5 10 9 11 13 7 20 17 24 5 10 8 11

BST remove() function remove(node): Thursday, February 20, 2014 BST remove() function remove(node): // Input: node – the node we are trying to remove. We can find this node // by calling contains() if node has no children: // case 1 – node is a leaf node.parent.removeChild(node) else if node only has left child: // case 2a – only left child if node.parent.left == node: // if node is a left child node.parent.left = node.left else: node.parent.right = node.left else if node only has right child: // case 2b – only right child if node.parent.left == node: node.parent.left = node.right node.parent.right = node.right else: // case 3 – node has two children nextNode = successor(node) node.data = nextNode.data // replace node’s data with that of nextNode remove(nextNode) // nextNode guaranteed to have at most one child

Successor vs. Predecessor Thursday, February 20, 2014 Successor vs. Predecessor It should be noted that it is perfectly valid to use a node’s in-order predecessor in place of its successor in our BST remove() algorithm. It doesn’t matter if you use one over the other, but randomly picking between the two helps keep the tree more balanced In case 3, the predecessor would be the right-most node of the given node’s left subtree.

BST Analysis How fast are the BST functions? Thursday, February 20, 2014 BST Analysis How fast are the BST functions? Depends on the height of the tree! The worst case requires traversing all the way to the leaf with the greatest depth If the tree is perfectly balanced, then its height is about log2n, which would let the BST functions run in O(log n) time But in the extremely unbalanced case, a binary search tree just becomes a sorted linked list, and its functions run in O(n) time B A D C E

Thursday, February 20, 2014 Balancing a BST If a binary tree becomes unbalanced, we can fix it by performing a series of tree rotations A B C A B C A B C Observe that the in-order ordering of each of these trees remains which means that the BST order is preserved between rotations A B C