Ordered Lists and Binary Trees



Similar documents
Data Structures and Algorithms

Binary Search Trees (BST)

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Binary Search Trees CMPSC 122

Chapter 14 The Binary Search Tree

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

TREE BASIC TERMINOLOGIES

Converting a Number from Decimal to Binary

From Last Time: Remove (Delete) Operation

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Algorithms Chapter 12 Binary Search Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

DATA STRUCTURES USING C

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Analysis of Algorithms I: Binary Search Trees

Data Structures. Level 6 C Module Descriptor

Data Structures and Algorithms Lists

Keys and records. Binary Search Trees. Data structures for storing data. Example. Motivation. Binary Search Trees

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

Binary Heap Algorithms

Data Structure [Question Bank]

Lecture Notes on Binary Search Trees

Binary Trees and Huffman Encoding Binary Search Trees

Symbol Tables. Introduction

Algorithms and Data Structures Written Exam Proposed SOLUTION

Data Structure with C

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Alex. Adam Agnes Allen Arthur

Binary Heaps. CSE 373 Data Structures

Lecture Notes on Binary Search Trees

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Data Structures Fibonacci Heaps, Amortized Analysis

Analysis of Algorithms I: Optimal Binary Search Trees

Binary Search Trees 3/20/14

Binary Search Trees. Ric Glassey

Optimal Binary Search Trees Meet Object Oriented Programming

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

10CS35: Data Structures Using C

The ADT Binary Search Tree

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Algorithms and Data Structures

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Section IV.1: Recursive Algorithms and Recursion Trees

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

PES Institute of Technology-BSC QUESTION BANK

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Big Data and Scripting. Part 4: Memory Hierarchies

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

Exam study sheet for CS2711. List of topics

Any two nodes which are connected by an edge in a graph are called adjacent node.

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Full and Complete Binary Trees

GRAPH THEORY LECTURE 4: TREES

How To Create A Tree From A Tree In Runtime (For A Tree)

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

CSE 326: Data Structures B-Trees and B+ Trees

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Heaps & Priority Queues in the C++ STL 2-3 Trees

Binary Trees. Wellesley College CS230 Lecture 17 Thursday, April 5 Handout #28. PS4 due 1:30pm Tuesday, April

Unordered Linked Lists

Data Structures and Algorithms

Sample Questions Csci 1112 A. Bellaachia

Java Software Structures

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Data Structures and Algorithms Written Examination

Data Structures and Data Manipulation

Operations: search;; min;; max;; predecessor;; successor. Time O(h) with h height of the tree (more on later).

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Persistent Binary Search Trees

Chapter 8: Binary Trees

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Analysis of a Search Algorithm

OPTIMAL BINARY SEARCH TREES

M-way Trees and B-Trees

DATABASE DESIGN - 1DL400

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

Parallelization: Binary Tree Traversal

Data Structures Using C++

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Rotation Operation for Binary Search Trees Idea:

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Why Use Binary Trees?

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Transcription:

Data Structures and Algorithms Ordered Lists and Binary Trees Chris Brooks Department of Computer Science University of San Francisco Department of Computer Science University of San Francisco p.1/62

6-0: Ordered Lists We will often want to have a list in which all the elements are ordered. As we add or remove elements, order is maintained. List of names in alphabetical order List of files from smallest to largest List of records from earliest to most recent Department of Computer Science University of San Francisco p.2/62

6-1: 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 How to implement this? Array or linked list? Department of Computer Science University of San Francisco p.3/62

Using an Ordered Array Running times: Check Insert Remove Print 6-2: Implementing Ordered List Department of Computer Science University of San Francisco p.4/62

Using an Ordered Array Running times: 6-3: Implementing Ordered List Check Insert Remove Print Department of Computer Science University of San Francisco p.5/62

Using an Unordered Array Running times: Check Insert Remove Print 6-4: Implementing Ordered List Department of Computer Science University of San Francisco p.6/62

Using an Unordered Array Running times: 6-5: Implementing Ordered List Check Insert Remove Print (Given a fast sorting algorithm) Keeping the array ordered makes insertion require time. Department of Computer Science University of San Francisco p.7/62

6-6: Implementing Ordered List Using an Ordered Linked List Running times: Check Insert Remove Print Department of Computer Science University of San Francisco p.8/62

6-7: Implementing Ordered List Using an Ordered Linked List Running times: Check Insert Remove Print With an unordered list, insertion requires time. Department of Computer Science University of San Francisco p.9/62

Linked Lists Insert fast / Find slow Arrays Find fast / Insert slow 6-8: The Best of Both Worlds 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... Department of Computer Science University of San Francisco p.10/62

6-9: The Best of Both Worlds 4 8 12 15 22 25 28 Department of Computer Science University of San Francisco p.11/62

6-10: The Best of Both Worlds 4 8 12 15 22 25 28 Move the initial pointer to the middle of the list: 4 8 12 15 22 25 28 We ve cut our search time in half! Have we changed the running time? Department of Computer Science University of San Francisco p.12/62

6-11: The Best of Both Worlds 4 8 12 15 22 25 28 Move the initial pointer to the middle of the list: 4 8 12 15 22 25 28 We ve cut our search time in half! Have we changed the running time? Repeat the process! Department of Computer Science University of San Francisco p.13/62

6-12: 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 Department of Computer Science University of San Francisco p.14/62

Grab the first element of the list: 6-13: The Best of Both Worlds 4 8 12 15 22 25 28 Give it a good shake - 15 8 25 4 12 22 28 Department of Computer Science University of San Francisco p.15/62

6-14: Binary Trees Binary Trees are Recursive Data Structures Base Case: Empty Tree Recursive Case: Node, consisting of: Left Child (Tree) Right Child (Tree) Data Department of Computer Science University of San Francisco p.16/62

6-15: 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 Department of Computer Science University of San Francisco p.17/62

6-16: Tree Terminology Parent / Child Leaf node Root node Edge (between nodes) Path Ancestor / Descendant Depth of a node Length of path from root to Height of a tree (Depth of deepest node) + 1 Department of Computer Science University of San Francisco p.18/62

6-17: Full Binary Tree Each node has 0 or 2 children Full Binary Trees Not Full Binary Trees Department of Computer Science University of San Francisco p.19/62

6-18: Complete Binary Tree Can be build by starting at the root, and filling the tree by levels from left to right Complete Binary Trees Not Complete Binary Trees Department of Computer Science University of San Francisco p.20/62

6-19: Binary Search Trees Binary Trees For each node n, (value stored at node n) in left subtree) For each node n, (value stored at node n) in right subtree) (value stored (value stored Department of Computer Science University of San Francisco p.21/62

6-20: Example Binary Search Trees A D B C D B D A D B F A c E G Department of Computer Science University of San Francisco p.22/62

6-21: Implementing BSTs Department of Computer Science University of San Francisco p.23/62

6-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... Department of Computer Science University of San Francisco p.24/62

6-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 Department of Computer Science University of San Francisco p.25/62

6-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? Department of Computer Science University of San Francisco p.26/62

6-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 Department of Computer Science University of San Francisco p.27/62

6-26: Finding an Element in a BST Next, the Recursive Case how do we make the problem smaller? Department of Computer Science University of San Francisco p.28/62

6-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? Department of Computer Science University of San Francisco p.29/62

6-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. Department of Computer Science University of San Francisco p.30/62

6-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? Department of Computer Science University of San Francisco p.31/62

6-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) Department of Computer Science University of San Francisco p.32/62

6-31: Finding an Element in a BST To find an element in a Binary Search Tree : If is empty, then If the root of is not in contains, then is in If the element stored in the root of Look for in the left subtree of Otherwise Look for in the right subtree of : Department of Computer Science University of San Francisco p.33/62

6-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); } Department of Computer Science University of San Francisco p.34/62

6-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 Department of Computer Science University of San Francisco p.35/62

6-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! Department of Computer Science University of San Francisco p.36/62

6-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? Department of Computer Science University of San Francisco p.37/62

6-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... Department of Computer Science University of San Francisco p.38/62

void print(node tree) { if (tree!= null) { print(tree.left()); System.out.prinln(tree.element()); print(tree.right()); } } 6-37: Printing out a BST Department of Computer Science University of San Francisco p.39/62

6-38: Printing out a BST Examples Department of Computer Science University of San Francisco p.40/62

6-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 Department of Computer Science University of San Francisco p.41/62

6-40: PREORDER Examples Department of Computer Science University of San Francisco p.42/62

6-41: POSTORDER Examples Department of Computer Science University of San Francisco p.43/62

6-42: INORDER Examples Department of Computer Science University of San Francisco p.44/62

To find the minimal element in a BST: 6-43: BST Minimal Element 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? Department of Computer Science University of San Francisco p.45/62

To find the minimal element in a BST: Base Case: 6-44: BST Minimal Element When is it easy to find the smallest element in a BST? Department of Computer Science University of San Francisco p.46/62

To find the minimal element in a BST: Base Case: 6-45: BST Minimal Element 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. Department of Computer Science University of San Francisco p.47/62

To find the minimal element in a BST: Recursive Case: How can we make the problem smaller? 6-46: BST Minimal Element Department of Computer Science University of San Francisco p.48/62

To find the minimal element in a BST: Recursive Case: 6-47: BST Minimal Element 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? Department of Computer Science University of San Francisco p.49/62

To find the minimal element in a BST: Recursive Case: 6-48: BST Minimal Element 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 Department of Computer Science University of San Francisco p.50/62

Comparable minimum(node tree) { if (tree == null) return null; if (tree.left() == null) return tree.element(); else return minimum(tree.left()); } 6-49: BST Minimal Element Department of Computer Science University of San Francisco p.51/62

6-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(); } Department of Computer Science University of San Francisco p.52/62

6-51: Inserting into BST What is the base case an easy tree to insert an element into? Department of Computer Science University of San Francisco p.53/62

6-52: Inserting into BST What is the base case an easy tree to insert an element into? An empty tree Create a new tree, containing the element Department of Computer Science University of San Francisco p.54/62

6-53: Inserting into BST Recursive Case: How do we make the problem smaller? Department of Computer Science University of San Francisco p.55/62

6-54: Inserting into BST 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? Department of Computer Science University of San Francisco p.56/62

6-55: Inserting into BST 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 value stored at the root, and insert the element into the right subtree if value stored at the root Department of Computer Science University of San Francisco p.57/62

6-56: Inserting into BST Base case is empty: Create a new tree, containing the element Recursive Case: If is less than the element at the root of, insert into left subtree If is greater than the element at the root of, insert into the right subtree Department of Computer Science University of San Francisco p.58/62

6-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); Department of Computer Science University of San Francisco p.59/62

6-58: Inserting into BST 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; } } Department of Computer Science University of San Francisco p.60/62

6-59: 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 Department of Computer Science University of San Francisco p.61/62

6-60: Comparable vs..key() method We have been storing Comparable elements in BSTs Book uses 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 Department of Computer Science University of San Francisco p.62/62