Data Structures and Algorithms

Similar documents
CS S-06 Binary Search Trees 1

Binary Trees, Binary Search Trees

Programming II (CS300)

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

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

Binary Trees

Chapter 20: Binary Trees

Data Structures and Algorithms

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

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

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

Trees, Binary Trees, and Binary Search Trees

INF2220: algorithms and data structures Series 1

CS 171: Introduction to Computer Science II. Binary Search Trees

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Binary Trees and Binary Search Trees

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

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

TREES. Trees - Introduction

Algorithms. Deleting from Red-Black Trees B-Trees

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Trees. CSE 373 Data Structures

Binary Trees. Height 1

Data Structures and Algorithms for Engineers

Tree Structures. A hierarchical data structure whose point of entry is the root node

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Data Structures in Java

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

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

March 20/2003 Jayakanth Srinivasan,

Binary Search Trees. See Section 11.1 of the text.

Programming II (CS300)

Trees. Truong Tuan Anh CSE-HCMUT

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

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Spring 2018 Mentoring 8: March 14, Binary Trees

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Building Java Programs

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

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

Programming II (CS300)

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

BBM 201 Data structures

Associate Professor Dr. Raed Ibraheem Hamed

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Algorithms and Data Structures

CS24 Week 8 Lecture 1

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

IX. Binary Trees (Chapter 10)

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Data Structures and Algorithms

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

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

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

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

Solution to CSE 250 Final Exam

Lecture 6: Analysis of Algorithms (CS )

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

Data Structures and Algorithms

Binary Trees. Examples:

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

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

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

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

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

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Principles of Computer Science

The Binary Search Tree ADT

Friday Four Square! 4:15PM, Outside Gates

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

Data Structures and Algorithms

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

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

Cpt S 122 Data Structures. Data Structures Trees

COSC 2011 Section N. Trees: Terminology and Basic Properties

Binary Trees Fall 2018 Margaret Reid-Miller

CE 221 Data Structures and Algorithms

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

CS302 - Data Structures using C++

Chapter 5. Binary Trees

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

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Search Trees: BSTs and B-Trees

CS200: Trees. Rosen Ch & 11.3 Prichard Ch. 11. CS200 - Trees

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

examines every node of a list until a matching node is found, or until all nodes have been examined and no match is found.

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

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

Topic 14. The BinaryTree ADT

CMSC 341 Lecture 10 Binary Search Trees

Transcription:

Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco

06-0: Ordered List ADT Operations: Insert an element in the list Check if an element is in the list Remove an element from the list Print out the contents of the list, in order

06-1: Implementing Ordered List Using an Ordered Array Running times: Check Insert Remove Print

06-2: Implementing Ordered List Using an Ordered Array Running times: Check Θ(lg n) Insert Θ(n) Remove Θ(n) Print Θ(n)

06-3: Implementing Ordered List Using an Unordered Array Running times: Check Insert Remove Print

06-4: Implementing Ordered List Using an Unordered Array Running times: Check Θ(n) Insert Θ(1) Remove Θ(n) Need to find element first! Print Θ(n lg n) (Given a fast sorting algorithm)

06-5: Implementing Ordered List Using an Ordered Linked List Running times: Check Insert Remove Print

06-6: Implementing Ordered List Using an Ordered Linked List Running times: Check Insert Remove Print Θ(n) Θ(n) Θ(n) Θ(n)

06-7: The Best of Both Worlds Linked Lists Insert fast / Find slow Arrays Find fast / Insert slow The only way to examine nth element in a linked list is to traverse (n-1) other elements 4 8 12 15 22 25 28 If we could leap to the middle of the list...

06-8: The Best of Both Worlds 4 8 12 15 22 25 28

06-9: The Best of Both Worlds 4 8 12 15 22 25 28 ve the initial pointer to the middle of the list: 4 8 12 15 22 25 28 e ve cut our search time in half! Have we changed Θ() running time?

06-10: The Best of Both Worlds 4 8 12 15 22 25 28 ve the initial pointer to the middle of the list: 4 8 12 15 22 25 28 e ve cut our search time in half! Have we changed Θ() running time? peat the process!

06-11: The Best of Both Worlds 4 8 12 15 22 25 28 4 8 12 15 22 25 28 4 8 12 15 22 25 28

06-12: The Best of Both Worlds ab the first element of the list: 4 8 12 15 22 25 28 ve it a good shake - 15 8 25 4 12 22 28

06-13: Binary Trees Binary Trees are Recursive Data Structures Base Case: Empty Tree Recursive Case: Node, consiting of: Left Child (Tree) Right Child (Tree) Data

06-14: Binary Tree Examples The following are all Binary Trees (Though not Binary Search Trees) A A B C B D E C F D A B C D E F G

06-15: Tree Terminology Parent / Child Leaf node Root node Edge (between nodes) Path Ancestor / Descendant Depth of a node n Length of path from root to n Height of a tree (Depth of deepest node) + 1

06-16: Full Binary Tree Each node has 0 or 2 children Full Binary Trees Not Full Binary Trees

06-17: Complete Binary Tree Can be built by starting at the root, and filling the tree by levels from left to right Complete Binary Trees Not Complete Binary Trees

06-18: Binary Search Trees Binary Trees For each node n, (value stored at node n) (value stored in left subtree) For each node n, (value stored at node n) < (value stored in right subtree)

06-19: Example Binary Search Trees A D B C C B D A D B F A c E G

06-20: Implementing BSTs Each Node in a BST is implemented as a class: public class Node { public Comparable data; public Node left; public Node right; }

06-21: Implementing BSTs public class Node { public Node(Comparable data, Node left, Node right) { this.data = data; this.left = left; this.right = right; } public Node left() { return left; } public Node setleft(node newleft) { left = newleft }... (etc) } private Comparable data; private Node left; private Node right;

06-22: Finding an Element in a BST Binary Search Trees are recursive data structures, so most operations on them will be recursive as well Recall how to write a recursive algorithm...

06-23: Writing a Recursive Algorithm Determine a small version of the problem, which can be solved immediately. This is the base case Determine how to make the problem smaller Once the problem has been made smaller, we can assume that the function that we are writing will work correctly on the smaller problem (Recursive Leap of Faith) Determine how to use the solution to the smaller problem to solve the larger problem

06-24: Finding an Element in a BST First, the Base Case when is it easy to determine if an element is stored in a Binary Search Tree?

06-25: Finding an Element in a BST First, the Base Case when is it easy to determine if an element is stored in a Binary Search Tree? If the tree is empty, then the element can t be there If the element is stored at the root, then the element is there

06-26: Finding an Element in a BST Next, the Recursive Case how do we make the problem smaller?

06-27: Finding an Element in a BST Next, the Recursive Case how do we make the problem smaller? Both the left and right subtrees are smaller versions of the problem. Which one do we use?

06-28: Finding an Element in a BST Next, the Recursive Case how do we make the problem smaller? Both the left and right subtrees are smaller versions of the problem. Which one do we use? If the element we are trying to find is < the element stored at the root, use the left subtree. Otherwise, use the right subtree.

06-29: Finding an Element in a BST Next, the Recursive Case how do we make the problem smaller? Both the left and right subtrees are smaller versions of the problem. Which one do we use? If the element we are trying to find is < the element stored at the root, use the left subtree. Otherwise, use the right subtree. How do we use the solution to the subproblem to solve the original problem?

06-30: Finding an Element in a BST Next, the Recursive Case how do we make the problem smaller? Both the left and right subtrees are smaller versions of the problem. Which one do we use? If the element we are trying to find is < the element stored at the root, use the left subtree. Otherwise, use the right subtree. How do we use the solution to the subproblem to solve the original problem? The solution to the subproblem is the solution to the original problem (this is not always the case in recursive algorithms)

06-31: Finding an Element in a BST To find an element e in a Binary Search Tree T : If T is empty, then e is not in T If the root of T contains e, then e is in T If e < the element stored in the root of T : Look for e in the left subtree of T Otherwise Look for e in the right subtree of T

06-32: Finding an Element in a BST boolean find(node tree, Comparable elem) { if (tree == null) return false; if (elem.compareto(tree.element()) == 0) return true; if (elem.compareto(tree) < 0) return find(tree.left(), elem); else return find(tree.right(), elem); }

06-33: Printing out a BST To print out all element in a BST: Print all elements in the left subtree, in order Print out the element at the root of the tree Print all elements in the right subtree, in order

06-34: Printing out a BST To print out all element in a BST: Print all elements in the left subtree, in order Print out the element at the root of the tree Print all elements in the right subtree, in order Each subproblem is a smaller version of the original problem we can assume that a recursive call will work!

06-35: Printing out a BST What is the base case for printing out a Binary Search Tree what is an easy tree to print out?

06-36: Printing out a BST What is the base case for printing out a Binary Search Tree what is an easy tree to print out? An empty tree is extremely easy to print out do nothing! Code for printing a BST...

06-37: Printing out a BST void print(node tree) { if (tree!= null) { print(tree.left()); System.out.println(tree.element()); print(tree.right()); } }

06-38: Printing out a BST Examples

06-39: Tree Traversals PREORDER Traversal Do operation on root of the tree Traverse left subtree Traverse right subtree INORDER Traversal Traverse left subtree Do operation on root of the tree Traverse right subtree POSTORDER Traversal Traverse left subtree Traverse right subtree Do operation on root of the tree

06-40: PREORDER Examples

06-41: POSTORDER Examples

06-42: INORDER Examples

06-43: BST Minimal Element To find the minimal element in a BST: Base Case: When is it easy to find the smallest element in a BST? Recursive Case: How can we make the problem smaller? How can we use the solution to the smaller problem to solve the original problem?

06-44: BST Minimal Element To find the minimal element in a BST: Base Case: When is it easy to find the smallest element in a BST?

06-45: BST Minimal Element To find the minimal element in a BST: Base Case: When is it easy to find the smallest element in a BST? When the left subtree is empty, then the element stored at the root is the smallest element in the tree.

06-46: BST Minimal Element To find the minimal element in a BST: Recursive Case: How can we make the problem smaller?

06-47: BST Minimal Element To find the minimal element in a BST: Recursive Case: How can we make the problem smaller? Both the left and right subtrees are smaller versions of the same problem How can we use the solution to a smaller problem to solve the original problem?

06-48: BST Minimal Element To find the minimal element in a BST: Recursive Case: How can we make the problem smaller? Both the left and right subtrees are smaller versions of the same problem How can we use the solution to a smaller problem to solve the original problem? The smallest element in the left subtree is the smallest element in the tree

06-49: BST Minimal Element Comparable minimum(node tree) { if (tree == null) return null; if (tree.left() == null) return tree.element(); else return minimum(tree.left()); }

06-50: BST Minimal Element Iterative Version Comparable minimum(node tree) { if (tree == null) return null; while (tree.left()!= null) tree = tree.left(); return tree.element(); }

06-51: Inserting e into BST T What is the base case an easy tree to insert an element into?

06-52: Inserting e into BST T What is the base case an easy tree to insert an element into? An empty tree Create a new tree, containing the element e

06-53: Inserting e into BST T Recursive Case: How do we make the problem smaller?

06-54: Inserting e into BST T Recursive Case: How do we make the problem smaller? The left and right subtrees are smaller versions of the same problem. How do we use these smaller versions of the problem?

06-55: Inserting e into BST T Recursive Case: How do we make the problem smaller? The left and right subtrees are smaller versions of the same problem Insert the element into the left subtree if e value stored at the root, and insert the element into the right subtree if e > value stored at the root

06-56: Inserting e into BST T Base case T is empty: Create a new tree, containing the element e Recursive Case: If e is less than the element at the root of T, insert e into left subtree If e is greater than the element at the root of T, insert e into the right subtree

06-57: Tree Manipulation in Java Tree manipulation functions return trees Insert method takes as input the old tree and the element to insert, and returns the new tree, with the element inserted Old value (pre-insertion) of tree will be destroyed To insert an element e into a tree T: T = insert(t, e);

06-58: Inserting e into BST T Node insert(node tree, Comparable elem) { if (tree == null) { return new Node(elem); if (elem.compareto(tree.element() <= 0)) { tree.setleft(insert(tree.left(), elem)); return tree; } else { tree.setright(insert(tree.right(), elem)); return tree; } }

06-59: Deleting From a BST Removing a leaf:

06-60: Deleting From a BST Removing a leaf: Remove element immediately

06-61: Deleting From a BST Removing a leaf: Remove element immediately Removing a node with one child:

06-62: Deleting From a BST Removing a leaf: Remove element immediately Removing a node with one child: Just like removing from a linked list Make parent point to child

06-63: Deleting From a BST Removing a leaf: Remove element immediately Removing a node with one child: Just like removing from a linked list Make parent point to child Removing a node with two children:

06-64: Deleting From a BST Removing a leaf: Remove element immediately Removing a node with one child: Just like removing from a linked list Make parent point to child Removing a node with two children: Replace node with largest element in left subtree, or the smallest element in the right subtree

06-65: Comparable vs..key() method We have been storing Comparable elements in BSTs Alternately, could use a key() method elements stored in BSTs must implement a key() method, which returns an integer. We can combine the two methods Each element stored in the tree has a key() method key() method returns Comparable class

06-66: BST Implementation Details Use BSTs to implement Ordered List ADT Operations Insert Find Remove Print in Order The specification (interface) should not specify an implementation Allow several different implementations of the same interface

06-67: BST Implementation Details BST functions require the root of the tree be sent in as a parameter Ordered list functions should not contain implementation details! What should we do?

06-68: BST Implementation Details BST functions require the root of the tree be sent in as a parameter Ordered list functions should not contain implementation details! What should we do? Private variable, holds root of the tree Private recursive methods, require root as an argument Public methods call private methods, passing in private root