Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Similar documents
Data Structures in Java

CMSC 341. Binary Search Trees CMSC 341 BST

CIS 121. Binary Search Trees

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

CMSC 341 Lecture 10 Binary Search Trees

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

! 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

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

protected BinaryNode root; } 02/17/04 Lecture 11 1

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

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

CS350: Data Structures Binary Search Trees

CS 350 : Data Structures Binary Search Trees

Priority Queues. 04/10/03 Lecture 22 1

CS350: Data Structures Tree Traversal

Binary Search Trees. See Section 11.1 of the text.

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

COMS 3137 Class Notes

CS 151. Binary Search Trees. Wednesday, October 10, 12

Generic BST Interface

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

AVL Trees. See Section 19.4of the text, p

Binary Trees, Binary Search Trees

CS350: Data Structures AA Trees

Second Exam SAMPLE Computer Programming 338 Dr. St. John Lehman College City University of New York Tuesday, 5 April 2011

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

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

CE 221 Data Structures and Algorithms

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

void insert( Type const & ) void push_front( Type const & )

TREES Lecture 12 CS2110 Spring 2018

COMP : Trees. COMP20012 Trees 219

Programming II (CS300)

CS 315 Data Structures mid-term 2

Implementations II 1

Lecture 23: Binary Search Trees

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

Trees, Binary Trees, and Binary Search Trees

Binary Tree Applications

8. Binary Search Tree

In addition to the correct answer, you MUST show all your work in order to receive full credit.

ITEC2620 Introduction to Data Structures

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

Trees, Part 1: Unbalanced Trees

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

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

CSE 214 Computer Science II Heaps and Priority Queues

The Binary Search Tree ADT

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

TREES 11/1/18. Prelim Updates. Data Structures. Example Data Structures. Tree Overview. Tree. Singly linked list: Today: trees!

CSE 373 Spring Midterm. Friday April 21st

ITI Introduction to Computing II

We have the pointers reference the next node in an inorder traversal; called threads

Searching: Introduction

TREES Lecture 12 CS2110 Fall 2016

ITI Introduction to Computing II

Why Do We Need Trees?

Binary Trees: Practice Problems

CS 310: Tree Rotations and AVL Trees

CmpSci 187: Programming with Data Structures Spring 2015

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

COT 6405: Analysis of Algorithms. Giri Narasimhan. ECS 389; Phone: x3748

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

Dictionaries. Priority Queues

CS350: Data Structures Stacks

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Priority Queues (Heaps)

Binary Heaps. CSE 373 Data Structures Lecture 11

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

Name CPTR246 Spring '17 (100 total points) Exam 3

INF2220: algorithms and data structures Series 1

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Recursive Data Structures and Grammars

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

ECE 242 Data Structures and Algorithms. Heaps I. Lecture 22. Prof. Eric Polizzi

4/18/ Binary Search Trees (BSTs) 17.1 Adding an Element to a BST Removing an Element from a BST. 17.

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

CS102 Binary Search Trees

Data Structures and Algorithms

ECE 242. Data Structures

Implementations II 1

Data Structures and Algorithms for Engineers

Outline. Computer Science 331. Insertion: An Example. A Recursive Insertion Algorithm. Binary Search Trees Insertion and Deletion.

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

A Binary Search Tree Implementation

CSE 326: Data Structures Binary Search Trees

Chapter 20: Binary Trees

Principles of Computer Science

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Transcription:

Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu

Search Tree Structures

Binary Tree Operations u Tree Traversals u Search O(n) calls to visit() Why? Every recursive has one call to visit() and spends O(1) time Number of recursive calls = number of nodes + number of NULL pointers = n + (n+1) = O(n) O(N): Tree Traversal with O(1) time for visit() to compare u Insert and Delete Too expensive! Needs to be defined properly and can be O(1) time

Binary Search Trees u Helps efficient search u Binary Tree where each node stores a value u Value stored at node is larger than all values stored in nodes of left subtree u Value stored at node is smaller than all values stored in nodes of right subtree https://www.cs.cmu.edu/~adamchik/15-121/lectures/trees/pix/insertex.bmp

Implementations: BST Private static class BinaryNode<AnyType> { AnyType element; // data in node BinaryNode<AnyType> left; // left child BinaryNode<AnyType> right; // right child }

Operations: BST void insert( x ) --> Insert x void remove( x ) --> Remove x boolean contains( x ) --> Return true if x is present Comparable findmin( ) --> Return smallest item Comparable findmax( ) --> Return largest item boolean isempty( ) --> Return true if empty; else false void makeempty( ) --> Remove all items void printtree( ) --> Print tree in sorted order

Search: contains() public boolean contains( AnyType x ) { return contains( x, root ); } private boolean contains( AnyType x, BinaryNode<AnyType> t ) { if( t == null ) return false; // empty tree } int compareresult = x.compareto( t.element ); if( compareresult < 0 ) // x is smaller than t.element return contains( x, t.left ); else if( compareresult > 0 ) // x is larger than t.element return contains( x, t.right ); else // x = t.element return true; // Match Time Complexity = O(h)

findmin() and findmax() private BinaryNode<AnyType> findmin (BinaryNode<AnyType> t) { if( t == null ) return null; else if( t.left == null ) return t; return findmin( t.left ); } private BinaryNode<AnyType> findmax (BinaryNode<AnyType> t) { if( t == null ) return null; else if( t.left == null ) return t; return findmax( t.right); } Time Complexity = O(h)

Insert into BST u Idea: First search and then insert at point of failure private BinaryNode<AnyType> insert(anytype x, BinaryNode<AnyType> t) { if( t == null ) return new BinaryNode<>( x, null, null ); } int compareresult = x.compareto( t.element ); if( compareresult < 0 ) t.left = insert( x, t.left ); else if( compareresult > 0 ) t.right = insert( x, t.right ); else ; // Duplicate; do nothing return t; // Note left hand side of stmt Time Complexity = O(h)

Delete from BST u Idea: First search and then insert at point of failure private BinaryNode<AnyType> remove(anytype x, BinaryNode<AnyType> t) { if( t == null ) return t; // item not found; do nothing int compareresult = x.compareto( t.element ); if( compareresult < 0 ) t.left = remove( x, t.left ); // Note left hand side of stmt else if( compareresult > 0 ) t.right = remove( x, t.right ); else if( t.left!= null && t.right!= null ) { // Two children t.element = findmin( t.right ).element; t.right = remove( t.element, t.right ); } else t = ( t.left!= null )? t.left : t.right; } return t; Time Complexity = O(h)

Static vs Dynamic Structures

Static vs Dynamic Structures u Static data structures Build data structures once and few updates Lots of queries E.g., Static Lists such as u Dynamic data structures Data structure constantly changing Answers to queries depends on current state Lots of inserts and deletes E.g., Stacks and Queues

Linear Data Structures u Lists ArrayList LinkedList u Stacks & Queues u Sorted Lists List is sorted according to some key value in the data Inserts and deletes must maintain sorted order Search is an important operation Implementations ArrayList LinkedList

SortedList: Time Complexity ArrayList LinkedList add(x) O(N) O(N) remove(x) O(N) O(N) find(x) O(log N) O(N) ArrayList LinkedList add(x) a9er find(x) O(N) O(1) remove(x) a9er find(x) O(N) O(1) find(x) O(log N) O(N) BinaryTree BinarySearchTree insert(x) O(1) O(h) remove(x) O(1) O(h) contains(x) O(N) O(h) Tradeoffs!