Some Terminologies
Total Page:16
File Type:pdf, Size:1020Kb
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. Example: Expression Trees Preorder, Postorder and Inorder • Postorder traversal – left, right, node – postfix expression • abc*+de*f+g*+ • Inorder traversal – left, node, right. • Leaves are operands (constants or variables) – infix expression • The other nodes (internal nodes) contain operators • a+b*c+d*e+f*g • Will not be a binary tree if some operators are not binary Tree traversal • Preorder • 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 2 1/9/2014 compare: Implementation of a general tree • Postorder Preorder, Postorder and Inorder Binary Search Trees • Stores keys in the nodes in a way so that searching, insertion and deletion can be done efficiently. Binary search tree property – For every node X, all the keys in its left subtree are smaller than the key value in X, and all the keys in its right subtree are larger than the key value in X Binary Trees Binary Search Trees • Possible operations on the Binary Tree ADT – parent – left_child, right_child – sibling – root, etc • Implementation – Because a binary tree has at most two children, we can keep direct pointers to them A binary search tree Not a binary search tree 3 1/9/2014 Binary search trees Two binary search trees representing the same set: • Average depth of a node is O(log N); maximum depth of a node is O(N) Implementation Searching (Find) • Find X: return a pointer to the node that has key X, or NULL if there is no such node • Time complexity – O(height of the tree) Searching BST Inorder traversal of BST • If we are searching for 15, then we are done. • Print out all the keys in sorted order • If we are searching for a key < 15, then we should search in the left subtree. • If we are searching for a key > 15, then we should search in the right subtree. Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20 4 1/9/2014 findMin/ findMax delete • Return the node containing the smallest element in Three cases: the tree (1) the node is a leaf • Start at the root and go left as long as there is a left – Delete it immediately child. The stopping point is the smallest element (2) the node has one child – Adjust a pointer from the parent to bypass that node • Similarly for findMax • Time complexity = O(height of the tree) insert delete • Proceed down the tree as you would with a find (3) the node has 2 children • If X is found, do nothing (or update something) – replace the key of that node with the minimum element at the • Otherwise, insert X at the last spot on the path traversed right subtree – delete the minimum element • Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. • Time complexity = O(height of the tree) • Time complexity = O(height of the tree) delete • When we delete a node, we need to consider how we take care of the children of the deleted node. Priority Queues – Binary – This has to be done such that the property Heaps of the search tree is maintained. homes.cs.washington.edu/~anderson/ iucee/Slides_326.../ Heaps .ppt 30 5 1/9/2014 Applications of the Priority Queue Recall Queues • Select print jobs in order of decreasing length • FIFO: First-In, First-Out • Forward packets on routers in order of urgency • Some contexts where this seems right? • Select most frequent symbols for compression • Sort numbers, picking minimum first • Some contexts where some things should be allowed to skip ahead in the • Anything greedy line? 31 34 Queues that Allow Line Jumping Potential Implementations • Need a new ADT • Operations: Insert an Item, Remove the “Best” Item insert deleteMin Unsorted list (Array) O(1) O(n) Unsorted list (Linked-List) O(1) O(n) 6 2 Sorted list (Array) O(n) O(1)* 15 23 insert deleteMin 12 18 Sorted list (Linked-List) O(n) O(1) 45 3 7 32 35 Priority Queue ADT Recall From Lists, Queues, 1. PQueue data : collection of data with Stacks priority • Use an ADT that corresponds to your needs 2. PQueue operations • The right ADT is efficient, while an overly – insert general ADT provides functionality you – deleteMin aren’t using, but are paying for anyways 3. PQueue property : for two elements in the • Heaps provide O(log n) worst case for queue, x and y, if x has a lower priority both insert and deleteMin, O(1) average insert value than y, x will be deleted before y 33 36 6 1/9/2014 Brief interlude: Some Definitions: Binary Heap Properties A Perfect binary tree – A binary tree with all leaf nodes at the same depth. All 1. Structure Property internal nodes have 2 children. 2. Ordering Property height h h+1 11 2 – 1 nodes 2h – 1 non-leaves 5 21 2h leaves 2 9 16 25 1 3 7 10 13 19 22 30 37 40 Heap Structure Property Tree Review • A binary heap is a complete binary tree. Tree T Complete binary tree – binary tree that is A completely filled, with the possible exception root (T): of the bottom level, which is filled left to right. leaves (T): B C Examples : children (B) : parent (H) : D E F G siblings (E) : ancestors (F) : H I descendents (G) : subtree (C) : J K L M N 38 41 Representing Complete More Tree Terminology Binary Trees in an Array Tree T A depth (B) : 1 A From node i: height (G) : B C 2B 3 C 6 7 left child: 4D 5 E F G degree (B) : D E F G 89 10 11 12 right child: H I J K L parent: branching factor (T): H I implicit (array) implementation: J K L M N ABCDEFGHIJKL 0 1 2 3 4 5 6 7 8 9 10111213 39 42 7 1/9/2014 Why this approach to storage? Heap – Insert(val) Basic Idea: 1. Put val at “next” leaf position 2. Percolate up by repeatedly exchanging node until no longer needed 43 46 Heap Order Property Insert: percolate up 10 Heap order property : For every non-root 20 80 node X, the value in the parent of X is less than (or equal to) the value in X. 40 60 85 99 50 700 65 15 10 10 10 20 80 20 80 40 60 85 99 15 80 30 15 50 700 40 20 85 99 not a heap 50 700 65 60 44 47 Heap Operations Insert Code (optimized) • findMin: void insert(Object o) { int percolateUp(int hole, assert(!isFull()); Object val) { • insert(val): percolate up. while (hole > 1 && size++; val < Heap[hole/2]) • deleteMin: percolate down. newPos = Heap[hole] = Heap[hole/2]; percolateUp(size,o); hole /= 2; } 10 Heap[newPos] = o; return hole; } } 20 80 40 60 85 99 50 700 65 runtime: 45 (Code in book) 48 8 1/9/2014 Insert: 16, 32, 4, 69, 105, 43, 2 Heap – Deletemin 0 1 2 3 4 5 6 7 8 Basic Idea: 1. Remove root (that is always the min!) 2. Put “last” leaf node at root 3. Find smallest child of node 4. Swap node with its smallest child if needed. 5. Repeat steps 3 & 4 until no swaps needed. 49 52 DeleteMin: percolate down 10 20 15 Data Structures 40 60 85 99 50 700 65 Binary Heaps 15 20 65 40 60 85 99 50 700 50 53 DeleteMin Code (Optimized) Building a Heap Object deleteMin() { int percolateDown(int hole, assert(!isEmpty()); Object val) { while (2*hole <= size) { 12 5 11 3 10 6 9 4 8 1 7 2 returnVal = Heap[1]; left = 2*hole; size--; right = left + 1; newPos = if (right ≤ size && Heap[right] < Heap[left]) percolateDown(1, target = right; Heap[size+1]); else Heap[newPos] = target = left; Heap[size + 1]; if (Heap[target] < val) { return returnVal; Heap[hole] = Heap[target]; } hole = target; } else runtime: break; } (code in book) return hole; 51 54 } 9 1/9/2014 Buildheap pseudocode Building a Heap private void buildHeap() { • Adding the items one at a time is O(n for ( int i = currentSize/2; i > 0; i-- ) log n) in the worst case percolateDown( i ); } • I promised O(n) for today runtime: 55 58 BuildHeap: Floyd’s Method Working on Heaps • What are the two properties of a heap? 12 – Structure Property – Order Property 5 11 3 10 6 9 • How do we work on heaps? – Fix the structure 4 8 1 7 2 – Fix the order 56 59 BuildHeap: Floyd’s Method BuildHeap: Floyd’s Method 12 5 11 12 5 11 3 10 6 9 4 8 1 7 2 Add elements arbitrarily to form a complete tree.