Friday Four Square! 4:15PM, Outside Gates

Similar documents
Binary Trees, Binary Search Trees

TREES. Trees - Introduction

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

Programming II (CS300)

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

Binary Search Trees Part Two

INF2220: algorithms and data structures Series 1

Binary Search Trees. See Section 11.1 of the text.

Binary Search Trees Treesort

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

Binary Trees

Chapter 20: Binary Trees

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

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

Data Structures and Algorithms for Engineers

Maps; Binary Search Trees

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

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

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

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Trees. CSE 373 Data Structures

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

CS24 Week 8 Lecture 1

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

Self-Balancing Search Trees. Chapter 11

Programming Languages and Techniques (CIS120)

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

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

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

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

INF2220: algorithms and data structures Series 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

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

x-fast and y-fast Tries

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

Algorithms. AVL Tree

Trees. Truong Tuan Anh CSE-HCMUT

Lecture 26. Introduction to Trees. Trees

CSE 230 Intermediate Programming in C and C++ Binary Tree

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

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

Garbage Collection: recycling unused memory

x-fast and y-fast Tries

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Trees. Trees. CSE 2011 Winter 2007

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

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

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

March 20/2003 Jayakanth Srinivasan,

Associate Professor Dr. Raed Ibraheem Hamed

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

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

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

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

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

9. Heap : Priority Queue

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

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

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Balanced Trees Part Two

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

8. Binary Search Tree

Algorithms. Deleting from Red-Black Trees B-Trees

CSC148-Section:L0301

Trees, Binary Trees, and Binary Search Trees

Summer Final Exam Review Session August 5, 2009

TREES Lecture 12 CS2110 Spring 2019

CSCI2100B Data Structures Trees

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

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

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

CS350: Data Structures AVL Trees

Binary Trees and Binary Search Trees

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Balanced Trees Part One

Data Structures and Algorithms

SFU CMPT Lecture: Week 9

Cpt S 122 Data Structures. Data Structures Trees

Data Structures - Binary Trees 1

Advanced Tree Data Structures

Data Structures Question Bank Multiple Choice

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

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

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

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

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

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

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

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

COMP Analysis of Algorithms & Data Structures

CSCI Trees. Mark Redekopp David Kempe

Binary search trees (chapters )

TREES Lecture 12 CS2110 Spring 2018

IX. Binary Trees (Chapter 10)

Course Review. Cpt S 223 Fall 2009

ECE250: Algorithms and Data Structures Midterm Review

Search Trees. Undirected graph Directed graph Tree Binary search tree

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

Transcription:

Binary Search Trees

Friday Four Square! 4:15PM, Outside Gates

Implementing Set On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively. Let's now turn our attention to the Set. Major operations: Insert Remove Contains

An Inefficient Implementation We could implement the Set as a list of all the values it contains. To add an element: Check if the element already exists. If not, append it. To remove an element: Find and remove it from the list. To see if an element exists: Search the list for the element.

Using Hashing If we have a hash function for the elements being stored, we can implement a Set using a hash table. What is the expected time to insert a value? Answer: O(1). What is the expected time to remove a value? Answer: O(1). What is the expected time to check if a value exists? Answer: O(1). However, writing a good hash function for a set of elements can be very tricky!

Using Tries If our keys are strings, we can store the set using a trie. Looking up or inserting a string with L letters takes time O(L). Doesn't work for arbitrary values.

An Entirely Different Approach

-1-2 2 3 0 4

-1-2 2 3 0 4

2-2 4-1 3 0

2-2 4-1 3 0

2-1 0-2 3 4

2-1 0-2 3 4

2-1 0-2 3 4

2-1 0-2 3 4

2-1 0-2 3 4

2-1 -2 0 4 3

2-1 -2 0 4 3

2-1 -2 0 4 3

2-1 -2 0 4 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

2-1 4-2 0 3

Binary Search Trees The data structure we have just seen is called a binary search tree (or BST). Uses comparisons between elements to store elements efficiently. No need for a complex hash function, or the ability to work one symbol at a time.

The Intuition

The Intuition 2-1 4-2 0 3

The Intuition 2-1 4-2 0 3 0

The Intuition 2-1 4-2 0 3 0

The Intuition 2-1 4-2 0 3 0

The Intuition 2-1 4-2 0 3 0 Values less than two Values greater than two

The Intuition 2-1 4-2 0 3 0 Values less than two Values greater than two

The Intuition 2-1 4-2 0 3 0

The Intuition 2-1 4-2 0 3 0 Values less than -1 Values greater than -1

The Intuition 2-1 4-2 0 3 0 Values less than -1 Values greater than -1

The Intuition 2-1 4-2 0 3 0

Tree Terminology As with a trie, a BST is a collection of nodes. The top node is called the root node. Nodes with no children are called leaves. 2-1 4-2 0 3

A Recursive View of BSTs 2-1 4-2 0 3

A Recursive View of BSTs 2-1 4-2 0 3

A Recursive View of BSTs

Implementing Lookups

Inserting into a BST 2-1 4-2 0 3

Inserting into a BST 2-1 4-2 0 3 1

Inserting into a BST 2-1 4-2 0 3 1

Inserting into a BST 2-1 4-2 0 3 1

Inserting into a BST 2-1 4-2 0 3 1

Inserting into a BST 2-1 4-2 0 3 1

Let's Code it Up!

Insertion Order Matters Suppose we create a BST of numbers in this order: 4, 2, 1, 3,, 5, 7 4 2 1 3 5 7

Insertion Order Matters Suppose we create a BST of numbers in this order: 1, 2, 3, 4, 5,, 7 1 2 3 4 5 7

Tree Terminology The height of a tree is the number of nodes in the longest path from the root to a leaf. 4 2 1 3 5 7

Tree Terminology The height of a tree is the number of nodes in the longest path from the root to a leaf. 1 2 3 4 5 7

Efficiency of Insertion What is the big-o complexity of adding a node to a tree? Depends on the height of a tree! Worst-case: have to take the longest path down to find where the node goes. Time is O(h), where h is the height of the tree.

Tree Heights What are the maximum and minimum heights of a tree with n nodes? Maximum height: all nodes in a chain. Height is O(n). Minimum height: Tree is as complete as possible. Height is O(log n). 4 2 1 3 5 7

Tree Heights What are the maximum and minimum heights of a tree with n nodes? Maximum height: all nodes in a chain. Height is O(n). Minimum height: Tree is as complete as possible. Height is O(log n). 4 2 1 3 5 7

Keeping the Height Low There are many modifications of the binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search tree that automatically adjusts itself to keep the height low. Details next time.

Walking Trees http://blog.pro-ecuador.com/wp-content/uploads/2010/03/floating-tree.jpg http://media.tumblr.com/tumblr_lp2fscga11qejdya.jpg

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7 1 2 3

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7 1 2 3

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7 1 2 3 4

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7 1 2 3 4

Walking a BST One advantage of a BST is that elements are stored in sorted order. We can iterate over the elements of a BST in sorted order by walking the tree recursively. 4 2 1 3 5 7 1 2 3 4 5 7

Tree Traversals There are three general types of tree traversals: Preorder: Visit the node, then visit the children. Inorder: Visit the left child, then the node, then the right child. Postorder: Visit the children, then visit the node.

Walking a Tree 4 2 1 3 5 7 Inorder Preorder Postorder 1 2 3 4 5 7 4 2 1 3 5 7 1 3 2 5 7 4

Getting Rid of Trees http://www.tigersheds.com/garden-resources/image.axd?picture=2010%2f%2fdeforestation1.jpg

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 1 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 1 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 1 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 1 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 1 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 3 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 2 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 5 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4 7

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. 4

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them.

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them.

Freeing a Tree Once we're done with a tree, we need to free all of its nodes. As with a linked list, we have to be careful not to use any nodes after freeing them. This is done as follows: Base case: There is nothing to delete in an empty tree. Recursive step: Delete both subtrees, then delete the current node. What kind of tree traversal is this?

Range Searches We can use BSTs to do range searches, in which we find all values in the BST within some range. For example: If values in a BST are dates, can find all events that occurred within some time window. If values in a BST are samples of a random variable, can find everything within one and two standard deviations above the mean.

The Intuition 2-1 4-2 0 3

The Intuition 2-1 4-2 0 3-1 +5

The Intuition 2-1 4-2 0 3-1 +5

The Intuition 2-1 4-2 0 3-1 +5

The Intuition 2-1 4-2 0 3-1 +5

The Intuition 2-1 4-2 0 3-1 +5

The Intuition 2-1 4-2 0 3-1 +5

The Intuition 2-1 4-2 0 3-1 +5

The Logic Base case: The empty tree has no nodes within any range. Recursive step: If this node is below the lower bound, recursively search the right subtree. If this node is above the upper bound, recursively search the left subtree. If this node is within bounds: Search the left subtree. Add this node to the output. Search the right subtree.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find.

Complexity of Range Searches How do we get a runtime for a range search? Depends on how many nodes we find. If there are k nodes within the range, we do at least O(k) work finding them. In addition, we have two border sets of nodes that are immediately outside that range. Each set has size O(h), where h is the height of the tree. Total work done is O(k + h). This is an output-sensitive algorithm.

Next Time Fun With Data Structures. Balanced binary search trees. Ternary search trees. DAWGs.