Tree Data Structures. Binary Tree Iterator Similar idea to Linked Lists: Define a class that handles the traversal of the complete tree for us, allowing.

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 Nell Dale Chapter 9 Trees Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
Information and Computer Sciences University of Hawaii, Manoa
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
DATA STRUCTURES AND ALGORITHMS Lecture Notes 8 Prepared by İnanç TAHRALI.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
CMSC 341 Binary Heaps Priority Queues. 2 Priority: some property of an object that allows it to be prioritized WRT other objects (of the same type) Priority.
Intro. to Data Structures Chapter 6 Priority Queue (Heap) Veera Muangsin, Dept. of Computer Engineering, Chulalongkorn University 1 Priority Queue.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
§3 Binary Tree Traversals —— visit each node exactly once L ::= move left ; R ::= move right ; V ::= visit the node. We will always traverse the left.
CS 367 Introduction to Data Structures Lecture 8.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
2 Binary Heaps What if we’re mostly concerned with finding the most relevant data?  A binary heap is a binary tree (2 or fewer subtrees for each node)
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Bohyung Han CSE, POSTECH
Binary Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Priority Queue and Heap
Presentation transcript:

Tree Data Structures

Binary Tree Iterator Similar idea to Linked Lists: Define a class that handles the traversal of the complete tree for us, allowing Similar idea to Linked Lists: Define a class that handles the traversal of the complete tree for us, allowing Start at root Start at root Movement to next node Movement to next node Visiting the data Visiting the data Arbitrary stopping (don’t have to completely traverse) Arbitrary stopping (don’t have to completely traverse) Implement iteratively (not recursively) – to allow for easy arbitrary stopping Implement iteratively (not recursively) – to allow for easy arbitrary stopping Iterative needs to emulate recursive traversal – use a Stack? Iterative needs to emulate recursive traversal – use a Stack?

InorderIterator Class class InorderIterator {public: InorderIterator(BinaryTree& inputTree): tree(inputTree) { CurrentNode = tree.root;}; char* Next(); private: const BinaryTree& tree; Stack stack; BinaryTreeNode* currentNode; }

InorderIterator Class char *InorderIterator::Next() { while (currentNode) { stack.Add(currentNode); currentNode = currentNode->leftChild; } if (! stack.isEmpty()) { currentNode = *(stack.Delete(currentNode)); char& temp = &(currentNode->data); currentNode = currentNode->rightChild; return &temp; } else return 0; // traversed whole tree }

InorderIterator Validation + *E * D / C A B Stack: + * * / A + * * / + * * + * + + * * / Output: A

InorderIterator Validation + *E * D / C A B Stack: + * * / Output: A + * * / + * B * [ETC…]

InorderIterator Analysis Computational complexity: Computational complexity: n nodes in tree n nodes in tree Every node is placed on stack once Every node is placed on stack once As you pass it on your way down As you pass it on your way down Every node is removed from stack once Every node is removed from stack once As you visit it on your way back up As you visit it on your way back up O(n) computation O(n) computation O(height) space -> bounded between: O(height) space -> bounded between: O(log 2 n) on low end (complete, full) O(log 2 n) on low end (complete, full) O(n) on high end (skewed) O(n) on high end (skewed)

Level-Order Traversal Rules: Instead of going down to children first, go across to siblings Instead of going down to children first, go across to siblings Visits all nodes on a given level in left-to-right order Visits all nodes on a given level in left-to-right order

Level Order Traversal Level order traversal also known as breadth-first traversal Level order traversal also known as breadth-first traversal Previous traversals made use of stack Previous traversals made use of stack Literally with iterative traversal Literally with iterative traversal Figuratively with recursive traversal Figuratively with recursive traversal To handle breadth-first search, need a queue in place of a stack To handle breadth-first search, need a queue in place of a stack

Level Order Traversal Add root node to queue Add root node to queue For a given node from the queue For a given node from the queue Visit node Visit node Add nodes left child to queue Add nodes left child to queue Add nodes right child to queue Add nodes right child to queue

Level Order Traversal void BinaryTree::LevelOrder() { Queue queue; BinaryTreeNode* currentNode = root; while (currentNode) { cout data; if (currentNode->leftChild) queue.Add(currentNode->leftChild); if (currentNode->rightChild) queue.Add(currentNode->rightChild; currentNode = *(queue.Delete(currentNode)); }

LevelOrder Validation + *E * D / C A B Queue: Output: + * E * E * D E * DD / C *

Binary Tree Operations Extensions to Binary Tree class to support: Extensions to Binary Tree class to support: Copy Constructor (also very similar to = operation) Copy Constructor (also very similar to = operation) Equivalence (==) (only takes 1 more step to != implementation) Equivalence (==) (only takes 1 more step to != implementation)

Binary Tree Operations Copy constructor: Copy constructor: Goal: Create a tree that mirrors the current tree Goal: Create a tree that mirrors the current tree Do a tree traversal, creating new nodes in new tree as encounter first time in current tree. Do a tree traversal, creating new nodes in new tree as encounter first time in current tree.

Binary Tree Copy Constructor BinaryTree::BinaryTree(const BinaryTree & input) { root = copy(input.root); } BinaryTreeNode* copy(BinaryTreeNode* current) { if (current) { BinaryTreeNode *temp = new BinaryTreeNode; temp->data = current->data; temp->leftChild = copy(current->leftChild); temp->rightChild = copy(current->rightChild); return temp; } else return 0; }

Binary Tree Equivalence Operator==: Operator==: Goal: Determine whether or not the two trees have the same data values and link orderings Goal: Determine whether or not the two trees have the same data values and link orderings Do a tree traversal, Do a tree traversal, Check whether or not nodes in the same place Check whether or not nodes in the same place Compare data values Compare data values

Binary Tree Equivalence bool operator==(const BinaryTree & tree1, const BinaryTree & tree2) { return equal(tree1.root, tree2.root); } bool equal(BinaryTreeNode* a, BinaryTreeNode* b) { if ((!a) && (!b)) return 1; // both null pointers if (a && b && (a->data == b->data) // same data && equal(a->leftChild,b->leftChild) // same left && equal(a->leftChild,b->leftChild) // same left && equal(a->rightChild, b->rightChild) // same right && equal(a->rightChild, b->rightChild) // same right return 1; return 0; }

Binary Trees as Tools: Exploiting Traversal Satisifiability Problem: Satisifiability Problem: Given an arbitrary boolean expression, determine if there is a set of assignments to the variables so that the expression is true. Given an arbitrary boolean expression, determine if there is a set of assignments to the variables so that the expression is true. (!x1 && x2)=> x1 = false, x2 = true Satisfying assignment

Satisfiability Rules Expressions: Expressions: A variable is an expression A variable is an expression If x, y are expressions, so are If x, y are expressions, so are x && y, x || y, and !x x && y, x || y, and !x Normal order of evaluation: Normal order of evaluation: not > and > or Parentheses can alter order of evaluation Parentheses can alter order of evaluation

Satisfiability Problem Represent expression in Binary Tree Represent expression in Binary Tree (x1 && !x2) || (!x1 && x3) || !x3 (x1 && !x2) || (!x1 && x3) || !x3 || ! x3 || && ! x3 x1 && x1 ! x2 Note: Terminals are all operands Internal nodes are operators. ! operator only has right child (unary)

Satisfiability Problem Evaluate tree in postfix order (LRV) Evaluate tree in postfix order (LRV) Allows proper passing up of computed subexpression values Allows proper passing up of computed subexpression values Computing left and right child (boolean values) before computing yourself, whose value is dependent on children. Computing left and right child (boolean values) before computing yourself, whose value is dependent on children. Need to evaluate with all possible instantiations of true/false for terminal nodes Need to evaluate with all possible instantiations of true/false for terminal nodes

Satisfiability Problem Update node definition: Update node definition: Data – holds type (and, or, not, true, false) Data – holds type (and, or, not, true, false) Value – holds answer computed from children Value – holds answer computed from children class SatNode { friend class SatTree; private: SatNode *leftChild; TypesOfData data; bool value; SatNode* rightChild; }

Satisfiability Problem With each instantiation at the main level, will set node->data = initial true/false value for terminal nodes With each instantiation at the main level, will set node->data = initial true/false value for terminal nodes Propagate up, without losing information on what types of operations are performed at higher levels Propagate up, without losing information on what types of operations are performed at higher levels

Satisfiability Problem Evaluation function: Rewrite of postorder traversal function Evaluation function: Rewrite of postorder traversal function void SatTree::PostOrderEval() {PostOrderEval(root); } void SatTree::PostOrderEval(SatNode *s) { if (node) { PostOrderEval(s->leftChild); PostOrderEval(s->rightChild); switch (s->data) { case LogicalNot: s-> value = !(s->rightChild->value); break; case LogicalAnd: s->value = s->leftChild->value && s- >rightChild->value; break; case LogicalOr: s->value = s->leftChild->value || s- >rightChild->value; break; case LogicalTrue: s->value = 1; break; case LogicalFalse: s-value = 0; break; } } }

Review Questions A B C D A BC DE Inorder traversal? (LVR) D C B A D B E A C

Review Questions A B C D A BC DE Preorder traversal? (VLR) A B C D A B D E C

Review Questions A B C D A BC DE Postorder traversal? (LRV) D C B A D E B C A

Review Questions Write a function to compute the number of leaf nodes in a binary tree. int BinaryTree::CountTerminals() {return CountTerminals(root); } int BinaryTree::CountTerminals(BinaryTreeNode* node) { int left = 0; int right = 0; if ((node->leftChild == 0) && (node->rightChild == 0)) return 1; else { if (node->leftChild != 0) left = CountTerminals(node->leftChild); if (node->rightChild != 0) right = CountTerminals(node- >rightChild); return left + right; } }

Priority Queues Standard queue definition: Standard queue definition: Add to back, remove from front Add to back, remove from front Priority queue definition: Priority queue definition: Add arbitrary priority to back, remove highest priority item Add arbitrary priority to back, remove highest priority item Very common! Very common! OS scheduling OS scheduling Packet switching Packet switching

Priority Queues – OS Job Scheduling CPU turketwh ls turketwh g++ thomas df -k turketwh gnome Next Job root backup 15

Priority Queues Other Examples: Other Examples: Selling service of a machine (renting carpet cleaners?) Selling service of a machine (renting carpet cleaners?) Goal maximize number of users in a fixed time Goal maximize number of users in a fixed time Allow smallest homes (shortest rental time) to go first (a min priority queue) Allow smallest homes (shortest rental time) to go first (a min priority queue) Goal maximize money in a fixed time Goal maximize money in a fixed time Allows homeowners willing to pay the most to go first (a max priority queue) Allows homeowners willing to pay the most to go first (a max priority queue)

Priority Queue Interface Definition: template template class MaxPriorityQueue {public: virtual void Insert(const Element & toInsert) = 0; virtual Element * Delete (Element &) = 0; //(DeleteMax or DeleteMin) }

Priority Queue Implementations List implementations (on linked list): List implementations (on linked list): Unordered list: Unordered list: Insert – attach at front of list: O(1) Insert – attach at front of list: O(1) Delete – search entire list: O(N) Delete – search entire list: O(N) Sorted list: Sorted list: Insert – find right place to insert: O(N) Insert – find right place to insert: O(N) Delete – pull off front: O(1) Delete – pull off front: O(1)

Heaps Heaps are a guaranteed fast way to implement priority queues Heaps are a guaranteed fast way to implement priority queues Two Heap definitions: Two Heap definitions: A max tree is a tree in which the key value in each node is no smaller than the key values in its children (if any). A max heap is a complete binary tree that is also a max tree. A max tree is a tree in which the key value in each node is no smaller than the key values in its children (if any). A max heap is a complete binary tree that is also a max tree. In a min tree, node keys are no larger than those for a node’s children. Min heap is a complete binary tree that is also min tree. In a min tree, node keys are no larger than those for a node’s children. Min heap is a complete binary tree that is also min tree. Root is largest item in max tree, smallest item in min tree Root is largest item in max tree, smallest item in min tree

Heap Examples: Max Heaps: Max Heaps: Complete binary? Yes Max Tree Property? Yes Complete binary? Yes Max Tree Property? Yes Complete binary? No Max Tree Property? Yes Not A Max Heap!

Heap Examples: Min Heaps: Min Heaps: Complete binary? Yes Min Tree Property? Yes Complete binary? Yes Min Tree Property? Yes Complete binary? Yes Min Tree Property? No Not A Min Heap!

Max Heap Operations Minimal Interface: Minimal Interface: Create an empty heap Create an empty heap Insert new element into heap Insert new element into heap Delete largest element from the heap Delete largest element from the heap Same functionality as priority queue!

Max Heap Definition: Variables Since heaps are complete binary trees, simplest and densest implementation is via an array Since heaps are complete binary trees, simplest and densest implementation is via an array Could also implement with left, right pointers. Could also implement with left, right pointers. class MaxHeap {public:private: Element *heap; int currentSize; int maxSize; }

Max Heap Definition: Methods class MaxHeap {public: MaxHeap(int size); bool isFull(); bool isEmpty(); void insert(Element toInsert); Element * delete(KeyType& toDelete); private: // see previous slide }

Max Heap Implementation Constructor template template MaxHeap ::MaxHeap(int size) { maxSize = size; currentSize = 0; heap = new Element [maxSize+1]; // heap[0] not used – simplifies binary tree // mapping // mapping} isFull, isEmpty() simple one-line conditional checks currentSize == maxSize or currentSize == 0

Max Heap: Insertion To Insert First Rule: Insert in correct place to preserve complete binary tree Second Rule: Ensure Max Tree property 1

Max Heap: Insertion To Insert First Rule: Insert in correct place to preserve complete binary tree Second Rule: Ensure Max Tree Property Repeatedly swap with parent if larger If inserting 25 instead of 5, would have to move all the way to root

Max Heap: Insertion template template Void MaxHeap ::Insert(const Element & toInsert) { if (isFull()) return; currentSize = currentSize + 1; for (int i = currentSize; true; ) { if (i == 1) break; // if at root stop if (toInsert.key < heap[i/2].key) break; // if smaller than parent stop (preserve max tree property) heap[i] = heap[i/2]; // swap with parent i = i /2; // set parent position as potential insert // location } heap[i] = toInsert; // insert at appropriate place }

Max Heap: Insertion Big Oh Analysis Insertion: Insertion: Start Location: Leaf node (bottom of tree) Start Location: Leaf node (bottom of tree) End Location: Root maximally, often less than root End Location: Root maximally, often less than root Max distance between root and leaf node is log 2 (n+1) Max distance between root and leaf node is log 2 (n+1) O(1) at each node (simple compare) O(1) at each node (simple compare) O(log n) total time O(log n) total time [better than sorted list implementation] [better than sorted list implementation]

Max Heap: Deletion First Rule: Remove root node – Guaranteed largest Second Rule: Preserve complete binary tree property - Move rightmost, lowest into root position Third Rule: Preserve max tree property- Repeatedly push root down, swapping with larger of two children

Max Heap: Deletion Implementation template template Element * MaxHeap ::DeleteMax(Element & x) { if (isEmpty()) return 0; x = heap[1]; // grab root to return Element k = heap[currentSize]; // grab lowest rightmost item currentSize = currentSize – 1; for (int i= 1, j = 2; j <= currentSize; ) { if (j < currentSize) if (heap[j].key < heap[j+1].key) j++; // j points to the larger child now if (k.key >= heap[j].key) break; // in right place already heap[i] = heap[j];// move child up if not in right place i = j; j *= 2;// move i,j down to continue swapping } heap[i] = k;// put in right place return &x; }

Max Heap: Deletion Big O Analysis 1 step to remove root node 1 step to remove root node 1 step to move bottom, right node to root 1 step to move bottom, right node to root Potentially could move all the way down the tree in preserving max tree property Potentially could move all the way down the tree in preserving max tree property Max height = log 2 (n+1) Max height = log 2 (n+1) Each move is O(1) – a comparison Each move is O(1) – a comparison Overall work is O(log 2 n) Overall work is O(log 2 n)

Priority Queue Big Oh Comparisons: Max Heap implementation of priority queue: Max Heap implementation of priority queue: O(log n) insert O(log n) delete O(log n) insert O(log n) delete Unsorted array Unsorted array O(1) insertO(n) delete O(1) insertO(n) delete Sorted array Sorted array O(n) insertO(1) delete O(n) insertO(1) delete