1 Order Statistics on Binary Trees Goal: find the k th element (in order) of a binary tree where 1 <= k <= N Why? –Find the median: k = n/2, or k =  n/2.

Slides:



Advertisements
Similar presentations
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Recursive Definitions and Structural Induction
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
CSE115/ENGR160 Discrete Mathematics 04/03/12 Ming-Hsuan Yang UC Merced 1.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CSE 326: Data Structures Lecture #7 Binary Search Trees Alon Halevy Spring Quarter 2001.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
1 Recitation 10. Induction Introduction. Induction is useful in proving properties of recursive algorithms, like their execution times. It is also the.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
Heap: A Special Kind of Tree
CSE 326: Data Structures Lecture #8 Binary Search Trees Alon Halevy Spring Quarter 2001.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
Induction and recursion
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 4: Induction and Recursion
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Building Java Programs Binary Search Trees; TreeSet.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
S. Raskhodnikova and A. Smith. Based on slides by C. Leiserson and E. Demaine. 1 Adam Smith L ECTURES Binary Search Trees Algorithms and Data Structures.
4.3 Recursive Definitions and Structural Induction Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Induction Proof. Well-ordering A set S is well ordered if every subset has a least element. [0, 1] is not well ordered since (0,1] has no least element.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
ICS 253: Discrete Structures I Induction and Recursion King Fahd University of Petroleum & Minerals Information & Computer Science Department.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
1 Recursive Definitions and Structural Induction CS 202 Epp section ??? Aaron Bloomfield.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
CS 1813 Discrete Mathematics, Univ Oklahoma Copyright © 2000 by Rex Page 1 Lecture 19 CS 1813 – Discrete Mathematics Trees and Inductive Definitions.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Lecture 9 Binary Trees Trees General Definition Terminology
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction What it is? Why is it a legitimate proof method? How to use it?
Chapter 4: Induction and Recursion
DAST Tirgul 7.
Recursive Objects (Part 4)
Binary search tree. Removing a node
UNIT III TREES.
Binary Search Tree (BST)
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Binary Search Tree Chapter 10.
Binary Trees, Binary Search Trees
CS200: Algorithms Analysis
Proving Properties of Recursive Functions and Data Structures
Data Structures and Algorithms for Information Processing
Trees.
Chapter 20: Binary Trees.
Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

1 Order Statistics on Binary Trees Goal: find the k th element (in order) of a binary tree where 1 <= k <= N Why? –Find the median: k = n/2, or k =  n/2  and  n/2  –Find quartiles: k = n/4, n/2, 3n/ t.size()  9 t.elementAt(1)  0 // min t.elementAt(3)  4 // 25%-ile t.elementAt(5)  6 // median t.elementAt(7)  10 // 75%-ile t.elementAt(9)  15 // max t 6

2 Maintaining Order Information Keep count tree sizes at each node –Update whenever tree is modified (easy) t

3 Using Order Information Searching for k th element: –If k == left.size + 1, return data –If k < left.size + 1, search for k th element in left subtree –If K > left.size + 1, search for (k-left.size-1) th element in right sub-tree t

4 Structural Induction We saw induction on natural numbers: Proof that property P holds for all natural numbers n –base case: P(0) –inductive hypothesis: Suppose P(n) holds –inductive case: P(n + 1) Natural numbers have an inductive definition: –base case: 0 is a natural number –inductive case: if n is a natural number, so is n + 1 –(and nothing else is a natural number, except those things created using these two rules) Coincidence?

5 Structural Induction We can generalize this to other structures that have an inductive definition E.g., a full binary tree can be defined as follows: –base case: make_tree(data) is a f-b-tree (consisting of one leaf node having given data) –inductive case: if left and right are f-b-trees, so is make_tree(left, right, data) (consisting of one internal node having given data and given left and right subtrees) –(and nothing else is a f-b-tree except those things created using these two rules) –(in particular, can’t have empty f-b-tree) How to do induction on these things?

6 Tree Induction Prove the following property for every f-b-tree P(t) : # leaf nodes in t = # internal nodes in t + 1 Base case: P(make_tree(data)) Inductive Hypothesis: Suppose P(left) and P(right) hold Inductive Case: P(make_tree(left, right, data))

7 Structural Induction Now we can prove (inductively) properties about all sorts of (inductively defined) things: –Natural Numbers, Rational Numbers, Real Numbers –Trees, BSTs, Linked Lists, Doubly-linked lists, etc. –Expressions (e.g. E : integer | E + E) –Java programs, methods, classes, etc. Ain’t induction grand?