Chapter 6 Heaps

Total Page:16

File Type:pdf, Size:1020Kb

Chapter 6 Heaps Introduction some systems applications require that items be processed Chapter 6 in specialized ways printing Heaps may not be best to place on a queue some jobs may be more ________________ small 1-page jobs should be printed before a 100-page job operating system _________________ processes run only for a slice of time queue: FIFO some jobs may take too long other jobs are more important and should not wait 2 Heap Model Heap Implementation specialized queue required heaps can be implemented with heap (priority queue) , with insertions at head provides at least insert _______________ deleteMin deleteMin: finds, returns, and removes min (or max) ordered linked list (worse due to number of ) other operations common insert used in other applications deleteMin external sorting binary search tree algorithms insert discrete event simulation deleteMin since only min is deleted, tree will be _______________ overkill since other included operations not required 3 4 Binary Heap Binary Heap binary heap common structure property simply termed heap completely filled two properties except level, which is filled from left to right ________________ complete binary tree has between and nodes heap _____________ height: operations can one of the properties can be represented with an array (no necessary) operation must continue until heap properties have been array position restored, which is typically simple left child in right child in parent in operations simple maximum heap size must be ______________________ 5 6 Binary Heap Binary Heap heap-order property allows operations to be performed quickly want to be able to find minimum quickly smallest element at ___________ any should also be a heap any node should be than all its descendants 7 8 Heap Operations Binary Heap two main heap operations example: insert 14 insert deleteMin insert need to maintain structure property create in next available location (at bottom of tree) place new value there if possible otherwise, slide into hole and bubble up hole continue until new value can be inserted termed strategy 9 10 Heap Operations Heap Operations insert (cont.) insert (cont.) could have used repeated swaps, but a swap requires could require as much as _____________ assignments on average, percolation terminates ___________ if element percolated up levels on average 2.607 comparisons are required swap method: assignments non-swap method: assignments if new element is smaller than all others in heap, hole will percolate to the __________ hole will be at index 1 and we will break out of the loop if extra check for 1 in loop, adds unnecessary time could place a copy of new value in position ____ 11 12 Heap Operations Binary Heap deleteMin example: deleteMin (13) finding minimum easy minimum more difficult hole is created at root element in complete binary tree must move if last element can be placed in hole, done child into hole hole slides down one level repeat until last element can be placed in hole worst case: average case: 13 14 Binary Heap Heap Operations example: deleteMin (13) (cont.) other heap operations finding minimum ___________ finding maximum not possible without scan through entire heap maximum in one of the leaves could use separate data structure, such as _____________ 15 16 Heap Operations Heap Operations other heap operations (cont.) other heap operations (cont.) decreaseKey buildHeap lowers value at position by given amount place items into the tree in any ____________ percolate up maintains property example: change process for more run time use percolateDown( ) from node increaseKey increases value at position by given amount percolate down example: drop process priority if taking ______________ remove decreaseKey to root, then deleteMin example: process is by user 17 18 Binary Heap Binary Heap example: start with random placement example: (cont.) start with percolateDown(7) each dashed line represents 2 comparisons 19 20 Heap Applications Heap Applications selection problem selection problem (cont.) from a list of elements, find th largest if heap is used original algorithm build heap of elements list and index th element perform deleteMin operations with simple sort, element extracted is th smallest element alternative algorithm if , read ____ elements into array and sort them another algorithm using heap smallest is in th position as in previous algorithm, but put elements in heap other elements processed one by one, placing them into is in th position the array other elements processed one by one, placing them into running time the array if , running time 21 22 Heap Applications -Heaps event simulation -heap bank where customers arrive and wait until one of tellers same as heap, but all nodes have children is available tends to be more than binary heap customer and based running time reduced to on probability distribution function deleteMin more expensive since more _______________ compute statistics on the length of time a customer must required wait, or the length of the line useful when heap is too large to fit entirely into main need to consider event that will occur in the least amount memory of time 4-heaps may outperform 2-heaps (binary heaps) heap can be used to order events 23 24 -Heaps Leftist Heaps example: -heap with one weakness of heaps so far is two heaps is difficult three data structures that can help heaps heaps binomial queues 25 26 Leftist Heaps Leftist Heaps leftist heaps leftist heaps (cont.) can be difficult to design a data structure for ____________ null path length (npl): length of shortest path from current that uses an array, but runs efficiently node to a node without children linked data structure therefore required npl of a node with zero or one child is 0 leftist heap npl of a null pointer is -1 structure and ordering properties of binary heaps npl of each node is 1 more than the minimum of the null difference is that heap is not ______________________ path lengths of its ________________ very unbalanced is desired leftist heap property npl of the left child is at least as large as that of the right child biases tree to left subtree 27 28 Leftist Heaps Leftist Heaps example: leftist heap leftist heaps operations all work should be done on the right path, which is guaranteed to be short and may destroy the leftist heap property not difficult to fix 29 30 Leftist Heaps Leftist Heaps leftist heaps operations (cont.) leftist heaps operations (cont.) merging merging example insertion special case of merging a 1-node heap with a larger heap if either of the two heaps is , return the other heap otherwise, compare __________ merge heap with the larger root with the right subheap of the heap with the smaller root 31 32 Leftist Heaps Leftist Heaps leftist heaps operations (cont.) leftist heaps operations (cont.) merging example merging example result is not leftist: left npl = 1, right npl = 2 33 34 Leftist Heaps Skew Heaps leftist heaps operations (cont.) self-adjusting version of leftist heap merging example relationship of skew heap to leftist heap is analogous to splay fix by swapping children trees and trees skew heaps binary trees with heap order but no structural constraint no information kept about null path length right path can be arbitrarily _________ worst case of all operations: for operations, total worst case: , or __________________ 35 36 Skew Heaps Skew Heaps skew heaps (cont.) skew heaps example fundamental operation is merging merge two skew heaps after merging, for leftists heaps, check both children for tree with larger root will merge onto tree with smaller root structure and swap children if needed in skew heaps, swap children except nodes on right paths do not swap children no extra required to maintain path lengths no tests required to determine when to swap children 37 38 Skew Heaps Skew Heaps skew heaps example (cont.) skew heaps example (cont.) recursively merge with the subheap of rooted at 8 make this heap the new left child of and the old left child of becomes the new right child heap happens to be ______________ 39 40 Binomial Queues Binomial Queues binomial queues binomial queues example keep a collection of heap-ordered trees, known as a ____________ at most one binomial tree of every ____________ heap order imposed on each binomial tree can represent any priority queue example: a priority queue of size 13 can be represented by the forest or 1101 worst case of all operations: 41 42 Binomial Queues Binomial Queues binomial queue of size 6 example binomial queue operations minimum element found by scanning of all trees found in can keep ongoing information to reduce to merging two queues merge takes 43 44 Binomial Queues Binomial Queues merge example merge example (cont.) now we have three binomial trees of height 3; keep one and merge the other two merge of two trees 45 46 Binomial Queues Binomial Queues merge values 1 7 example merge values 1 7 example (cont.) insert 1 insert 5 insert 2 insert 6 insert 3 insert 4 insert 7 47 48 Binomial Queues Binomial Queues deleteMin example deleteMin example (cont.) after merge separate tree with minimum root from rest of tree remaining trees after removing min 49 50.
Recommended publications
  • Lecture 04 Linear Structures Sort
    Algorithmics (6EAP) MTAT.03.238 Linear structures, sorting, searching, etc Jaak Vilo 2018 Fall Jaak Vilo 1 Big-Oh notation classes Class Informal Intuition Analogy f(n) ∈ ο ( g(n) ) f is dominated by g Strictly below < f(n) ∈ O( g(n) ) Bounded from above Upper bound ≤ f(n) ∈ Θ( g(n) ) Bounded from “equal to” = above and below f(n) ∈ Ω( g(n) ) Bounded from below Lower bound ≥ f(n) ∈ ω( g(n) ) f dominates g Strictly above > Conclusions • Algorithm complexity deals with the behavior in the long-term – worst case -- typical – average case -- quite hard – best case -- bogus, cheating • In practice, long-term sometimes not necessary – E.g. for sorting 20 elements, you dont need fancy algorithms… Linear, sequential, ordered, list … Memory, disk, tape etc – is an ordered sequentially addressed media. Physical ordered list ~ array • Memory /address/ – Garbage collection • Files (character/byte list/lines in text file,…) • Disk – Disk fragmentation Linear data structures: Arrays • Array • Hashed array tree • Bidirectional map • Heightmap • Bit array • Lookup table • Bit field • Matrix • Bitboard • Parallel array • Bitmap • Sorted array • Circular buffer • Sparse array • Control table • Sparse matrix • Image • Iliffe vector • Dynamic array • Variable-length array • Gap buffer Linear data structures: Lists • Doubly linked list • Array list • Xor linked list • Linked list • Zipper • Self-organizing list • Doubly connected edge • Skip list list • Unrolled linked list • Difference list • VList Lists: Array 0 1 size MAX_SIZE-1 3 6 7 5 2 L = int[MAX_SIZE]
    [Show full text]
  • Advanced Data Structures
    Advanced Data Structures PETER BRASS City College of New York CAMBRIDGE UNIVERSITY PRESS Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo Cambridge University Press The Edinburgh Building, Cambridge CB2 8RU, UK Published in the United States of America by Cambridge University Press, New York www.cambridge.org Information on this title: www.cambridge.org/9780521880374 © Peter Brass 2008 This publication is in copyright. Subject to statutory exception and to the provision of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published in print format 2008 ISBN-13 978-0-511-43685-7 eBook (EBL) ISBN-13 978-0-521-88037-4 hardback Cambridge University Press has no responsibility for the persistence or accuracy of urls for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate. Contents Preface page xi 1 Elementary Structures 1 1.1 Stack 1 1.2 Queue 8 1.3 Double-Ended Queue 16 1.4 Dynamical Allocation of Nodes 16 1.5 Shadow Copies of Array-Based Structures 18 2 Search Trees 23 2.1 Two Models of Search Trees 23 2.2 General Properties and Transformations 26 2.3 Height of a Search Tree 29 2.4 Basic Find, Insert, and Delete 31 2.5ReturningfromLeaftoRoot35 2.6 Dealing with Nonunique Keys 37 2.7 Queries for the Keys in an Interval 38 2.8 Building Optimal Search Trees 40 2.9 Converting Trees into Lists 47 2.10
    [Show full text]
  • Kd Trees What's the Goal for This Course? Data St
    Today’s Outline - kd trees CSE 326: Data Structures Too much light often blinds gentlemen of this sort, Seeing the forest for the trees They cannot see the forest for the trees. - Christoph Martin Wieland Hannah Tang and Brian Tjaden Summer Quarter 2002 What’s the goal for this course? Data Structures - what’s in a name? Shakespeare It is not possible for one to teach others, until one can first teach herself - Confucious • Stacks and Queues • Asymptotic analysis • Priority Queues • Sorting – Binary heap, Leftist heap, Skew heap, d - heap – Comparison based sorting, lower- • Trees bound on sorting, radix sorting – Binary search tree, AVL tree, Splay tree, B tree • World Wide Web • Hash Tables – Open and closed hashing, extendible, perfect, • Implement if you had to and universal hashing • Understand trade-offs between • Disjoint Sets various data structures/algorithms • Graphs • Know when to use and when not to – Topological sort, shortest path algorithms, Dijkstra’s algorithm, minimum spanning trees use (Prim’s algorithm and Kruskal’s algorithm) • Real world applications Range Query Range Query Example Y A range query is a search in a dictionary in which the exact key may not be entirely specified. Bellingham Seattle Spokane Range queries are the primary interface Tacoma Olympia with multi-D data structures. Pullman Yakima Walla Walla Remember Assignment #2? Give an algorithm that takes a binary search tree as input along with 2 keys, x and y, with xÃÃy, and ÃÃ ÃÃ prints all keys z in the tree such that x z y. X 1 Range Querying in 1-D
    [Show full text]
  • Binary Trees, Binary Search Trees
    Binary Trees, Binary Search Trees www.cs.ust.hk/~huamin/ COMP171/bst.ppt Trees • Linear access time of linked lists is prohibitive – Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)? Trees • A tree is a collection of nodes – The collection can be empty – (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T1, T2, ...., Tk, each of whose roots are connected by a directed edge from r Some Terminologies • Child and parent – Every node except the root has one parent – A node can have an arbitrary number of children • Leaves – Nodes with no children • Sibling – nodes with same parent Some Terminologies • Path • Length – number of edges on the path • Depth of a node – length of the unique path from the root to that node – The depth of a tree is equal to the depth of the deepest leaf • Height of a node – length of the longest path from that node to a leaf – all leaves are at height 0 – The height of a tree is equal to the height of the root • Ancestor and descendant – Proper ancestor and proper descendant Example: UNIX Directory Binary Trees • A tree in which no node can have more than two children • The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1. Example: Expression Trees • Leaves are operands (constants or variables) • The other nodes (internal nodes) contain operators • Will not be a binary tree if some operators are not binary Tree traversal • Used to print out the data in a tree in a certain order • Pre-order traversal – Print the data at the root – Recursively print out all data in the left subtree – Recursively print out all data in the right subtree Preorder, Postorder and Inorder • Preorder traversal – node, left, right – prefix expression • ++a*bc*+*defg Preorder, Postorder and Inorder • Postorder traversal – left, right, node – postfix expression • abc*+de*f+g*+ • Inorder traversal – left, node, right.
    [Show full text]
  • Skew Heaps and Binomial Qs • Amortized Runtime • Skew Heaps • Binomial Queues Ashish Sabharwal Autumn, 2003 • Comparing Implementations of Priority Qs
    Today’s Outline CSE 326: Data Structures • Binary Heaps: average runtime of insert • Leftist Heaps: re-do proof of property #1 Topic #5: Skew Heaps and Binomial Qs • Amortized Runtime • Skew Heaps • Binomial Queues Ashish Sabharwal Autumn, 2003 • Comparing Implementations of Priority Qs 2 Binary Heaps: Average runtime of Insert Right Path in a Leftist Tree is Short (#1) Recall: Insert-in-Binary-Heap(x) { Claim: The right path is as short as any in the tree. Put x in the next available position Proof: (By contradiction) percolateUp(last node) Pick a shorter path: D1 < D2 } Say it diverges from right path at x x ≤ How long does this percolateUp(last node) take? npl(L) D1-1 because of the path of – Worst case: (tree height), i.e. (log n) length D1-1 to null L R D – Average case: (1) Why?? 1 D2 ≥ npl(R) D2-1 because every node on right path is leftist Average runtime of insert in binary heap = ¡ (1) 3 Leftist property at x violated! 4 A Twist in Complexity Analysis: Skew Heaps The Amortized Case Problems with leftist heaps If a sequence of M operations takes O(M f(n)) time, – extra storage for npl we say the amortized runtime is O(f(n)). – extra complexity/logic to maintain and check npl – two pass iterative merge (requires stack!) • Worst case time per operation can still be large, say O(n) – right side is “often” heavy and requires a switch • Worst case time for any sequence of M operations is O(M f(n)) Solution: skew heaps • Average time per operation for any sequence is O(f(n)) – blind adjusting version of leftist heaps Is this the same
    [Show full text]
  • Exam Questions Chapter 1
    Exam questions Mark Allen Weiss School of Computer Science Florida International University University Park Miami FL Abstract This rep ort contains a collection of multiplechoice questions organized by b o ok chapter that can b e used 1 a for examinations Answers are provided at the end This rep ort was typeset in L T X Original source is E available Chapter What is the approximate value of log a b c d e none of the ab ove If log n equals what is the value of log n a b c d e none of the ab ove When p erforming a pro of by induction which is the case that is trivially true a the basis b the inductive hypothesis c the lemma d the theorem e none of the ab ove 1 Many of these questions app ear in dierent form in the Instructors Resource Manual for Algorithms Data Structures and Problem Solving with C published by AddisonWesley The following routine violates which rules of recursion function Recurse N Integer return Integer is begin if N then return else return N Recurse N Recurse N end if end Recurse a No base case b Fails to make progress c Performs redundant work d Two of the ab ove e All of a b and c Which of the following is the most likely result of failing to make progress towards a base case in a recursive call a compiler enters into an innite lo op b error at compilation time c exception is raised at run time d recursive routine enters an innite lo op when it runs e recursive routine terminates with bad value but no other error Answers B A A D C Chapter Which of the following functions grows fastest
    [Show full text]
  • Data Structures Binary Search Trees • Dictionary ADT / Search ADT • Quick Tree Review • Binary Search Trees
    Today’s Outline CSE 326: Data Structures Binary Search Trees • Dictionary ADT / Search ADT • Quick Tree Review • Binary Search Trees 5/30/2008 1 5/30/2008 2 The Dictionary ADT ADTs Seen So Far •jfogarty • Priority Queue • Data: James •Stack Fogarty –Push –Insert – a set of insert(jfogarty, ….) CSE 666 –Pop – DeleteMin (key, value) • phenry pairs Peter • Queue Henry CSE 002 – Enqueue find(boqin) – Dequeue • Operations: • boqin Then there is decreaseKey… • boqin – Insert (key, Bo, Qin, … Bo Qin value) CSE 002 – Find (key) Need pointer! Why? The Dictionary ADT is also Because find not efficient. – Remove (key)called the “Map ADT” 5/30/2008 3 5/30/2008 4 A Modest Few Uses Implementations insert find delete Θ(1) Θ(n) Θ(n) •Sets • Unsorted Linked-list • Dictionaries • Networks : Router tables Θ(1) Θ(n) Θ(n) • Unsorted array • Operating systems : Page tables • Compilers : Symbol tables log n + n Θ(log n) log n + n Probably the most widely used ADT! • Sorted array SO CLOSE! What limits the performance? 5/30/2008 5 5/30/2008 Time to move elements, can we mimic BinSearch with BST?6 Tree Calculations Tree Calculations Example Recall: height is max t How high is this tree? A number of edges from root to a leaf B C D E F G Find the height of the tree... H I height(t) = 1 + max {height(t.left), height(t.right)} height(B) = 1 runtime: height(C) = 4 J K L L Θ(N) (constant time for each node; so height(A) = 5 each node visited twice) M N 5/30/2008 7 5/30/2008 8 More Recursive Tree Calculations: Inorder Traversal Tree Traversals void traverse(BNode t){
    [Show full text]
  • Chapter 6 Priority Queues
    Chapter 6 Priority Queues Introduction Want to delete from queue according to priority. o Max priority queue – delete the greatest. o Min priority queue – delete the least. Insert normally, but delete based on priority. We can implement priority queues using binary search trees, ordered or unordered lists, ordered or unordered arrays, etc. Let assume a linked list implementation. o Unordered . Insert – (1) . Delete – (n) . o Ordered . Insert – (n) . Delete – (1) . What if we used a BST? What would happen with successive deletes? A splay tree? What would happen with successive deletes? Is there a way of getting the good run time without having to have the expense of pointers? Heaps Heap – complete binary tree in which each node is smaller than its parent. o Is this the same thing as a binary search tree (BST)? The binary tree for the heap is implemented as an array. This allows us easy access to children and parents as seen in the previous chapter. Used as a priority queue – regular insertion, priority deletion. Insertion – put the new node at the first empty position, and sift the element up (if needed). See Figure 1. o (height) (log n) Figure 1 Insertion into a Heap Priority Queues Page 1 Deletion – select root. Swap with last position (which will no longer be part of queue), and sift- down. See Figure 2. o (height) (log n) Figure 2 Deletion from Heap Initialization – insert n items into tree O(n log n) worst case BUT experiments have shown that on average, it only moves up an element 1.6 levels.
    [Show full text]
  • Red-Black Trees Important: There Are Two Questions N:O 6 – One About AVL Trees and One About Red-Black Trees
    Data structures and algorithms Re-examination Solution suggestions Thursday, 2020-08-20, 8:30–12:30, in Canvas and Zoom Examiner(s) Peter Ljunglöf, Nick Smallbone, Pelle Evensen. Allowed aids Course literature, other books, the internet. You are not allowed to discuss the problems with anyone! Submitting Submit your answers in one single PDF or Word or OpenOffice document. ​ ​ You can write your answers directly into the red boxes in this file. Pen & paper Some questions are easier to answer using pen and paper, which is fine! In that case, take a photo of your answer and paste into your answer document. Make sure the photo is readable! Assorted notes You may answer in English or Swedish. Excessively complicated answers might be rejected. Write legibly – we need to be able to read and understand your answer! Exam review If you want to discuss the grading, please contact Peter via email. There are 6 basic, and 3 advanced questions, and two points per question. So, the highest possible mark is 18 in total (12 from the basic and 6 from the advanced). Here is what you need for each grade: ● To pass the exam, you must get 8 out of 12 on the basic questions. ​ ​ ● To get a four, you must get 9 out of 12 on the basic questions, plus 2 out of 6 on the advanced. ​ ​ ● To get a five, you must get 10 out of 12 on the basic questions, plus 4 out of 6 on the advanced. ​ ​ ● To get VG (DIT181), you must get 9 out of 12 on the basic, plus 3 out of 6 on the advanced.
    [Show full text]
  • Amortized Complexity Verified
    J. Automated Reasoning manuscript No. (will be inserted by the editor) Amortized Complexity Verified Tobias Nipkow and Hauke Brinkop Abstract A framework for the analysis of the amortized complexity of functional data structures is formalized in the proof assistant Isabelle/HOL and applied to a number of standard examples and to the following non-trivial ones: skew heaps, splay trees, splay heaps and pairing heaps. The proofs are completely algebraic and are presented in some detail. 1 Introduction Amortized analysis [43,7] bounds the average cost of an operation in a sequence of operations in the worst case. In this paper we formalize a simple framework for amortized analysis of functional programs in the theorem prover Isabelle/HOL [37] and apply it to both the easy standard examples and the more challenging examples of skew heaps, splay trees, splay heaps and pairing heaps. This is an extended version of a previous publication [32]: the framework is generalized from unary to n-ary operations and an analysis of pairing heaps has been added. We are aiming for a particularly lightweight framework that supports proofs at a high level of abstraction. Therefore all algorithms are modeled as recursive functions in the logic. The contributions of the paper are as follows: – A lightweight and flexible framework for amortized complexity analysis in a theorem prover. – The first complexity analyses for skew heaps, splay trees, splay heaps and pairing heaps in a theorem prover. – The first purely algebraic proof of the amortized complexity of pairing heaps. The last point needs some explanation. For pairing heaps we had to recast the original proof by Fredman et al.
    [Show full text]
  • Data Structures
    Data structures PDF generated using the open source mwlib toolkit. See http://code.pediapress.com/ for more information. PDF generated at: Thu, 17 Nov 2011 20:55:22 UTC Contents Articles Introduction 1 Data structure 1 Linked data structure 3 Succinct data structure 5 Implicit data structure 7 Compressed data structure 8 Search data structure 9 Persistent data structure 11 Concurrent data structure 15 Abstract data types 18 Abstract data type 18 List 26 Stack 29 Queue 57 Deque 60 Priority queue 63 Map 67 Bidirectional map 70 Multimap 71 Set 72 Tree 76 Arrays 79 Array data structure 79 Row-major order 84 Dope vector 86 Iliffe vector 87 Dynamic array 88 Hashed array tree 91 Gap buffer 92 Circular buffer 94 Sparse array 109 Bit array 110 Bitboard 115 Parallel array 119 Lookup table 121 Lists 127 Linked list 127 XOR linked list 143 Unrolled linked list 145 VList 147 Skip list 149 Self-organizing list 154 Binary trees 158 Binary tree 158 Binary search tree 166 Self-balancing binary search tree 176 Tree rotation 178 Weight-balanced tree 181 Threaded binary tree 182 AVL tree 188 Red-black tree 192 AA tree 207 Scapegoat tree 212 Splay tree 216 T-tree 230 Rope 233 Top Trees 238 Tango Trees 242 van Emde Boas tree 264 Cartesian tree 268 Treap 273 B-trees 276 B-tree 276 B+ tree 287 Dancing tree 291 2-3 tree 292 2-3-4 tree 293 Queaps 295 Fusion tree 299 Bx-tree 299 Heaps 303 Heap 303 Binary heap 305 Binomial heap 311 Fibonacci heap 316 2-3 heap 321 Pairing heap 321 Beap 324 Leftist tree 325 Skew heap 328 Soft heap 331 d-ary heap 333 Tries 335 Trie
    [Show full text]
  • Some Terminologies
    1/9/2014 Some Terminologies Binary Trees, Binary Search Trees www.cs.ust.hk/~huamin/COMP17 1/bst. ppt • Child and parent – Every node except the root has one parent – A node can have an arbitrary number of children • Leaves – Nodes with no children • Sibling – nodes with same parent Trees Some Terminologies • Linear access time of linked lists is • Path prohibitive • Length – number of edges on the path – Does there exist any simple data structure • Depth of a node for which the running time of most – length of the unique path from the root to that node operations (search, insert, delete) is O(log – The depth of a tree is equal to the depth of the deepest leaf N)? • Height of a node – length of the longest path from that node to a leaf – all leaves are at height 0 – The height of a tree is equal to the height of the root • Ancestor and descendant – Proper ancestor and proper descendant Trees Example: UNIX Directory • A tree is a collection of nodes – The collection can be empty – (recursive definition) If not empty, a tree consists of a distinguished node r (the root ), and zero or more nonempty subtrees T1, T 2, ...., T k, each of whose roots are connected by a directed edge from r 1 1/9/2014 Binary Trees Preorder, Postorder and Inorder • A tree in which no node can have more than two children • Preorder traversal – node, left, right – prefix expression • ++a*bc*+*defg • The depth of an “average” binary tree is considerably smaller than N, eventhough in the worst case, the depth can be as large as N – 1.
    [Show full text]