Presentation is loading. Please wait.

Presentation is loading. Please wait.

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 presentations


Presentation on theme: "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."— Presentation transcript:

1 Tree Data Structures

2 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?

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

4 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 }

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

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

7 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)

8 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 1 2 3 4567 1 2 3 4 5 6 7

9 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

10 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

11 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)); }

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

13 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)

14 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.

15 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; }

16 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

17 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; }

18 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

19 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

20 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)

21 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

22 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; }

23 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

24 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; } } }

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

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

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

28 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; } }

29 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

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

31 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)

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

33 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)

34 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

35 Heap Examples: Max Heaps: Max Heaps: 14 12 7 1086 Complete binary? Yes Max Tree Property? Yes 9 6 5 3 9 6 5 3 Complete binary? Yes Max Tree Property? Yes Complete binary? No Max Tree Property? Yes Not A Max Heap!

36 Heap Examples: Min Heaps: Min Heaps: 2 7 4 1086 Complete binary? Yes Min Tree Property? Yes 10 20 50 83 21 11 Complete binary? Yes Min Tree Property? Yes Complete binary? Yes Min Tree Property? No Not A Min Heap!

37 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!

38 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; }

39 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 }

40 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

41 Max Heap: Insertion 20 15 14 2 10 1 To Insert First Rule: Insert in correct place to preserve complete binary tree Second Rule: Ensure Max Tree property 1

42 Max Heap: Insertion 20 15 14 2 10 5 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 5 20 15 14 5 10 2 If inserting 25 instead of 5, would have to move all the way to root

43 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 }

44 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]

45 Max Heap: Deletion 20 15 14 5 10 2 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 2 15 14 5 10 15 14 2 5 10

46 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; }

47 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)

48 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


Download ppt "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 presentations


Ads by Google