Why Use Binary Trees?

Similar documents
From Last Time: Remove (Delete) Operation

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Heaps. CSE 373 Data Structures

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

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:

Data Structures. Algorithm Performance and Big O Analysis

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

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

6 March Array Implementation of Binary Trees

Algorithms and Data Structures Written Exam Proposed SOLUTION

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

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

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

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

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

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

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

Analysis of Algorithms I: Binary Search Trees

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Converting a Number from Decimal to Binary

Binary Search Trees. Ric Glassey

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

Analysis of Binary Search algorithm and Selection Sort algorithm

TREE BASIC TERMINOLOGIES

Chapter 14 The Binary Search Tree

Ordered Lists and Binary Trees

Symbol Tables. Introduction

Binary Heap Algorithms

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

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Data Structure with C

Binary search algorithm

Algorithms and Data Structures

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

Algorithms and Data Structures

Data Structures and Algorithms

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

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

Binary Search Trees CMPSC 122

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

EE602 Algorithms GEOMETRIC INTERSECTION CHAPTER 27

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Persistent Binary Search Trees

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

Why you shouldn't use set (and what you should use instead) Matt Austern

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

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.

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Persistent Data Structures

IMPLEMENTING CLASSIFICATION FOR INDIAN STOCK MARKET USING CART ALGORITHM WITH B+ TREE

Cpt S 223. School of EECS, WSU

Full and Complete Binary Trees

Parallelization: Binary Tree Traversal

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Binary Search Trees (BST)

Data Structures and Data Manipulation

OPTIMAL BINARY SEARCH TREES

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

Data Structures Fibonacci Heaps, Amortized Analysis

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

Physical Data Organization

Data storage Tree indexes

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

M-way Trees and B-Trees

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Chapter 13: Query Processing. Basic Steps in Query Processing

Binary Trees and Huffman Encoding Binary Search Trees

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

Lecture Notes on Binary Search Trees

Common Data Structures

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

A Comparison of Dictionary Implementations

Binary Search Trees 3/20/14

DATABASE DESIGN - 1DL400

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Unit Storage Structures 1. Storage Structures. Unit 4.3

Chapter 8: Bags and Sets

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

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Algorithms. Margaret M. Fleck. 18 October 2010

Union-Find Algorithms. network connectivity quick find quick union improvements applications

External Sorting. Chapter 13. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Simple Balanced Binary Search Trees

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

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

Merkle Hash Trees for Distributed Audit Logs

External Sorting. Why Sort? 2-Way Sort: Requires 3 Buffers. Chapter 13

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

Chapter 8: Binary Trees

Java Software Structures

Data Warehousing und Data Mining

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

CS711008Z Algorithm Design and Analysis

CIS 631 Database Management Systems Sample Final Exam

Transcription:

Binary Search Trees

Why Use Binary Trees? Searches are an important application. What other searches have we considered? brute force search (with array or linked list) O(N) binarysearch with a pre-sorted array (not a list!) O(log(N)) Binary Search Trees are also O(log(N)) on average. So why use em? Because sometimes a tree is the more natural structure. Because insert and delete are also fast, O(logN). Not true for arrays.

So It s a Trade Off Array Lists O(N) insert O(N) delete O(N) search (assuming not pre-sorted) Linked Lists O(1) insert O(1) delete O(N) search Binary Search Tree O(log(N)) insert O(log(N)) delete O(log(N)) search on average, but occasionally (rarely) as bad as O(N).

Search Tree Concept Every node stores a value. Every left subtree (i.e., every node below and to the left) has a value less than that node. Every right subtree has a value greater than that node. 7 3 11 1 6 23 5

Question: Is This a Binary Search Tree? 7 3 11 1 6 23 5 9 No. Why not?

So S pose We Wanna Search Search for 4 1. start at root 7. 2. move to 3 on left, because 4 < 7. 7 3 11 1 6 23 5

So S pose We Wanna Search Search for 4. (cont.) 3. Now move to right because 4 > 3. 4. Now move to left because 4 < 6. 5. Now move to left because 4 < 5. But nowhere to go! 7 5. So done. Not in tree. 3 11 1 6 23 5

How Many Steps Did That Take? 7 to 3 to 6 to 5. Three steps (after the root). Will never be worse than the distance from the root to the furthest leaf (height!). On average splits ~twice at each node. 7 3 11 1 6 23 5

So Time To Search? So double the number of nodes at each layer. It s like doubling the counter variable each time through a for loop. How long does that take to run? Log(N) 3 7 11 1 6 23 5

Big-O of Search S pose tree bifurcates at every node. (This is an assumption that could be relaxed later). Each time we step down a layer in the tree, the # of nodes we have to search is cut in half. Holds half the remaining nodes. Holds half the remaining nodes.

Big-O of Search (cont. 1) For example, first we have to search 15 nodes = 2 4 1 then 7 nodes = 2 3 1 then 3 nodes = 2 2 1 then 1 node = 2 1-1 Now, note that tree has ~2 h+1-1 nodes where h = height of the tree. The height is the longest path (number of edges) from the root to the farthest leaf.

Big-O Search (cont. 2) So total number of nodes is N = 2 h+1-1 Or N @ 2 h+1 (true for big N) Now how many steps do we have to search? A max of h+1 steps (4 steps in the example above). What is h? Solve for it! log(n) = (h+1) * log(2) h = (logn/log2) - 1 So h = O( log N ). Wow! That s how long it takes to do a search.

findmin and findmax Can get minimum of a tree by always taking the left branch. Can get maximum of a tree by always taking right branch. 7 Example min 1 3 6 11 23 max 5

Inserting an Element Onto a Search Tree? Works just like find, but when reach the end of the tree, just insert. If the element is already on the tree, then add a counter to the node that keeps track of how many there are. Do an example with putting the following unordered array onto a binary search tree. {21, 1, 34, 2, 6, -4, -5, 489, 102, 47}

Insert Time How long to insert N elements? Each insert costs O(log(N)). There are N inserts. Therefore, O(Nlog(N)) Cool, our first example of something that takes NLogN time.

Deleting From Search Tree? Ugh. Hard to really remove. See what happens if erase a node. Not a search tree. Must adjust links There is a recursive approach (logn time), or can just reinsert all the elements in the subtree (NLogN time) Easiest to do lazy deletion. Usually are keeping track of duplicates stored in each node. So just decrement that counter. If goes below 1, then the node is empty. If node is empty, ignore the node when doing find s etc. So delete is O(logN) Tell me why?

Humdinger. OK, so on average, insert and delete take O(logN) time. But remember the tree we created with {21, 1, 34, 2, 6, -4, -5, 489, 102, 47}? Let s do the same thing with the array pre-sorted. {-5, -4, 1, 2, 6, 21, 34, 47, 102, 489}? Whoa, talk about unbalanced! -5-4 (Note: there isn t a unique tree for each set of data!) 1 2 6

Worst Case Scenario! Remember how everything depended on having an average of two children per node? Well, it ain t happenin here. In this case, the depth is N. So the worst case is that we could have O(N) time for each insert, delete, find. So if insert N elements, took O(N 2 ) time. Ugh.

Looking For Balance We want as many right branches as left branches. Whole branch of mathematics dealing with this. (har, har, very punny!) Complicated, but for random data, is usually irrelevant for small trees. Phew, so we are ok (usually).

Sample Implementation (Java) public class BinaryNode { public int value; public BinaryNode left; public BinaryNode right; public int numinnode; //optional keeps track of duplicates (and lazy deletion) } /**constructor can be null arguments*/ public BinaryNode(int n, BinaryNode lt, BinaryNode rt) { value = n; left = lt; right = rt; numinnode = 1; deleted = false; }

Sample Implementation (C) struct BinaryNode; typedef struct BinaryNode *BinaryNodePtr; struct BinaryNode { int value; BinaryNodePtr left; BinaryNodePtr right; } int numinnode; int deleted; //optional keeps track of duplicates //optional marks whether node is deleted

Sample Find (Java) /**Usually start with the root node.*/ public BinaryNode find(int n, BinaryNode node) { if(node == null) return null; if(n < node.value) return find(n, node.left); else if (n > node.value) return find(n, node.right); else return node; //match! } could be null Note: returns the node where n is living.

Sample Find (C) /*Usually start with the root node.*/ BinaryNodePtr find(int n, BinaryNodePtr node) { if(node == NULL) return NULL; if(n < node->value) return find(n, node->left); else if (n > node->value) return find(n, node->right); else return node; //match! }

Other Trees Many types of search trees Most have modifications for balancing B-Trees (not binary anymore) AVL-trees (restructures itself on inserts/deletes) splay-trees (ditto) etc.

So-So Dave Tree Each time insert a node, recreate the whole tree. 1. Keep a separate array list containing the values that are stored on the tree. so memory intensive! Twice the storage. 2. Now add the new value to the end of the array. 3. Make a copy of the array. ooooooh, expensive. O(N). 4. Randomly select an element from the copied array. because randomly ordered, will tend to balance the new tree O(1) 5. Add to new tree and delete from array. oooh, O(logN) insert ughh, O(N) delete 6. Repeat for each N. So what s the total time???? O(N 2 ) for inserts. Why?

Can You Do Better? Improve the So-So Dave Tree. p.s. It can be done! p.p.s. Consider storing in something faster like a linked list, stack, or queue You still have to work out details. p.p.p.s. That s the Super Dave Tree. p.p.p.p.s. Bonus karma points if your solution isn t the Super Dave Tree and is something radically different. p.p.p.p.p.s. No karma points if you use a splay tree, AVL tree, or other common approach, but mega-educational points for learning this extra material.