Tree Data Structures. Binary Tree Iterator Similar idea to Linked Lists: Define a class that handles...

48
Tree Data Tree Data Structures Structures

Transcript of Tree Data Structures. Binary Tree Iterator Similar idea to Linked Lists: Define a class that handles...

Page 1: 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.

Tree Data Tree Data StructuresStructures

Page 2: 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.

Binary Tree IteratorBinary Tree Iterator Similar idea to Linked Lists: Define a class that Similar idea to Linked Lists: Define a class that

handles the traversal of the complete tree for handles the traversal of the complete tree for us, allowingus, allowing Start at rootStart at root Movement to next nodeMovement to next node Visiting the dataVisiting the data Arbitrary stopping (don’t have to completely Arbitrary stopping (don’t have to completely

traverse)traverse)

Implement iteratively (not recursively) – to Implement iteratively (not recursively) – to allow for easy arbitrary stoppingallow for easy arbitrary stopping Iterative needs to emulate recursive traversal – use Iterative needs to emulate recursive traversal – use

a Stack?a Stack?

Page 3: 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.

InorderIterator ClassInorderIterator Class

class InorderIteratorclass InorderIterator{{

public: public: InorderIterator(BinaryTree& inputTree): InorderIterator(BinaryTree& inputTree): tree(inputTree) { CurrentNode = tree.root;};tree(inputTree) { CurrentNode = tree.root;};char* Next();char* Next();

private:private:const BinaryTree& tree;const BinaryTree& tree;Stack<BinaryTreeNode*> stack;Stack<BinaryTreeNode*> stack;BinaryTreeNode* currentNode;BinaryTreeNode* currentNode;

}}

Page 4: 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.

InorderIterator ClassInorderIterator Class

char *InorderIterator::Next()char *InorderIterator::Next(){{

while (currentNode) {while (currentNode) {stack.Add(currentNode);stack.Add(currentNode);currentNode = currentNode->leftChild; }currentNode = currentNode->leftChild; }

if (! stack.isEmpty()) {if (! stack.isEmpty()) {currentNode = *(stack.Delete(currentNode));currentNode = *(stack.Delete(currentNode));char& temp = &(currentNode->data);char& temp = &(currentNode->data);currentNode = currentNode->rightChild;currentNode = currentNode->rightChild;return &temp;return &temp;

}}else return 0; // traversed whole treeelse return 0; // traversed whole tree

}}

Page 5: 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.

InorderIterator InorderIterator ValidationValidation

+

* E

* D

/ C

A B Stack: +

*

*

/

A

+

*

*

/

+

*

*

+

*

+ +

*

*

/

Output: A

Page 6: 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.

InorderIterator InorderIterator ValidationValidation

+

* E

* D

/ C

A B Stack:+

*

*

/

Output: A

+

*

*

/

+

*

B

*

[ETC…]

Page 7: 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.

InorderIterator AnalysisInorderIterator Analysis Computational complexity:Computational complexity:

n nodes in treen nodes in tree Every node is placed on stack onceEvery node is placed on stack once

As you pass it on your way downAs you pass it on your way down Every node is removed from stack onceEvery node is removed from stack once

As you visit it on your way back upAs you visit it on your way back up

O(n) computationO(n) computation O(height) space -> bounded between:O(height) space -> bounded between:

O(logO(log22n) on low end (complete, full)n) on low end (complete, full) O(n) on high end (skewed)O(n) on high end (skewed)

Page 8: 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.

Level-Order TraversalLevel-Order Traversal

Rules:Rules: Instead of going down to children first, Instead of going down to children first,

go across to siblingsgo across to siblings Visits all nodes on a given level in left-Visits all nodes on a given level in left-

to-right orderto-right order

1

2 3

4 5 6 7

1 2 3 4 5 6 7

Page 9: 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.

Level Order TraversalLevel Order Traversal

Level order traversal also known as Level order traversal also known as breadth-first traversalbreadth-first traversal

Previous traversals made use of Previous traversals made use of stackstack Literally with iterative traversalLiterally with iterative traversal Figuratively with recursive traversalFiguratively with recursive traversal

To handle breadth-first search, need To handle breadth-first search, need a queue in place of a stacka queue in place of a stack

Page 10: 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.

Level Order TraversalLevel Order Traversal

Add root node to queueAdd root node to queue For a given node from the queueFor a given node from the queue

Visit nodeVisit node Add nodes left child to queueAdd nodes left child to queue Add nodes right child to queueAdd nodes right child to queue

Page 11: 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.

Level Order TraversalLevel Order Traversal

void BinaryTree::LevelOrder()void BinaryTree::LevelOrder(){{

Queue<BinaryTreeNode*> queue;Queue<BinaryTreeNode*> queue;BinaryTreeNode* currentNode = root;BinaryTreeNode* currentNode = root;while (currentNode) {while (currentNode) {

cout << currentNode->data;cout << currentNode->data;if (currentNode->leftChild) if (currentNode->leftChild)

queue.Add(currentNode->leftChild);queue.Add(currentNode->leftChild);if (currentNode->rightChild) if (currentNode->rightChild)

queue.Add(currentNode->rightChild;queue.Add(currentNode->rightChild;currentNode = currentNode =

*(queue.Delete(currentNode));*(queue.Delete(currentNode));}}

Page 12: 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.

LevelOrderLevelOrderValidationValidation

+

* E

* D

/ C

A B Queue:

Output: +

* E

*

E * D

E

* D D / C

*

Page 13: 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.

Binary Tree OperationsBinary Tree Operations

Extensions to Binary Tree class to Extensions to Binary Tree class to support:support: Copy Constructor (also very similar to = Copy Constructor (also very similar to =

operation)operation) Equivalence (==) (only takes 1 more Equivalence (==) (only takes 1 more

step to != implementation)step to != implementation)

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

Binary Tree OperationsBinary Tree Operations

Copy constructor:Copy constructor: Goal: Create a tree that mirrors the Goal: Create a tree that mirrors the

current treecurrent tree Do a tree traversal, creating new nodes Do a tree traversal, creating new nodes

in new tree as encounter first time in in new tree as encounter first time in current tree.current tree.

Page 15: 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.

Binary Tree Copy Binary Tree Copy ConstructorConstructor

BinaryTree::BinaryTree(const BinaryTree & input)BinaryTree::BinaryTree(const BinaryTree & input){ root = copy(input.root); }{ root = copy(input.root); }

BinaryTreeNode* copy(BinaryTreeNode* current)BinaryTreeNode* copy(BinaryTreeNode* current){{

if (current)if (current){{

BinaryTreeNode *temp = new BinaryTreeNode;BinaryTreeNode *temp = new BinaryTreeNode;temp->data = current->data;temp->data = current->data;temp->leftChild = copy(current->leftChild);temp->leftChild = copy(current->leftChild);temp->rightChild = copy(current->rightChild);temp->rightChild = copy(current->rightChild);return temp;return temp;

}}else return 0;else return 0;

}}

Page 16: 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.

Binary Tree EquivalenceBinary Tree Equivalence

Operator==:Operator==: Goal: Determine whether or not the two Goal: Determine whether or not the two

trees have the same data values and trees have the same data values and link orderingslink orderings

Do a tree traversal, Do a tree traversal, Check whether or not nodes in the same Check whether or not nodes in the same

placeplace Compare data valuesCompare data values

Page 17: 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.

Binary Tree EquivalenceBinary Tree Equivalencebool operator==(const BinaryTree & tree1, const BinaryTree & bool operator==(const BinaryTree & tree1, const BinaryTree &

tree2)tree2){ return equal(tree1.root, tree2.root); }{ return equal(tree1.root, tree2.root); }

bool equal(BinaryTreeNode* a, BinaryTreeNode* b)bool equal(BinaryTreeNode* a, BinaryTreeNode* b){{

if ((!a) && (!b)) return 1; // both null pointersif ((!a) && (!b)) return 1; // both null pointersif (a && b && (a->data == b->data) // same dataif (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 && equal(a->rightChild, b->rightChild) // same

rightrightreturn 1;return 1;

return 0;return 0;}}

Page 18: 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.

Binary Trees as Tools:Binary Trees as Tools:Exploiting TraversalExploiting Traversal

Satisifiability Problem:Satisifiability Problem: Given an arbitrary boolean expression, Given an arbitrary boolean expression,

determine if there is a set of determine if there is a set of assignments to the variables so that the assignments to the variables so that the expression is true.expression is true.

(!x1 && x2)(!x1 && x2) => x1 = false, x2 = true=> x1 = false, x2 = true

Satisfying assignmentSatisfying assignment

Page 19: 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.

Satisfiability RulesSatisfiability Rules

Expressions:Expressions: A variable is an expressionA variable is an expression If x, y are expressions, so areIf x, y are expressions, so are

x && y, x || y, and !xx && y, x || y, and !x Normal order of evaluation: Normal order of evaluation:

not > and > ornot > and > or Parentheses can alter order of Parentheses can alter order of

evaluationevaluation

Page 20: 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.

Satisfiability ProblemSatisfiability Problem

Represent expression in Binary TreeRepresent expression in Binary Tree

(x1 && !x2) || (!x1 && x3) || !x3(x1 && !x2) || (!x1 && x3) || !x3

||

!

x3

||

&&

! x3

x1

&&

x1 !

x2

Note: Terminals areall operands

Internal nodesare operators.

! operator only has right child (unary)

Page 21: 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.

Satisfiability ProblemSatisfiability Problem Evaluate tree in postfix order (LRV)Evaluate tree in postfix order (LRV) Allows proper passing up of computed Allows proper passing up of computed

subexpression valuessubexpression values Computing left and right child (boolean Computing left and right child (boolean

values) before computing yourself, whose values) before computing yourself, whose value is dependent on children.value is dependent on children.

Need to evaluate with all possible Need to evaluate with all possible instantiations of true/false for terminal instantiations of true/false for terminal nodesnodes

Page 22: 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.

Satisfiability ProblemSatisfiability 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 childrenValue – holds answer computed from children

class SatNodeclass SatNode{{

friend class SatTree;friend class SatTree;private:private:

SatNode *leftChild;SatNode *leftChild;TypesOfData data;TypesOfData data;bool value;bool value;SatNode* rightChild;SatNode* rightChild;

}}

Page 23: 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.

Satisfiability ProblemSatisfiability Problem

With each instantiation at the main With each instantiation at the main level, will set node->data = initial level, will set node->data = initial true/false value for terminal nodestrue/false value for terminal nodes

Propagate up, without losing Propagate up, without losing information on what types of information on what types of operations are performed at higher operations are performed at higher levelslevels

Page 24: 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.

Satisfiability ProblemSatisfiability Problem

Evaluation function: Rewrite of postorder traversal functionEvaluation function: Rewrite of postorder traversal function

void SatTree::PostOrderEval() {void SatTree::PostOrderEval() { PostOrderEval(root); }PostOrderEval(root); }

void SatTree::PostOrderEval(SatNode *s)void SatTree::PostOrderEval(SatNode *s){{

if (node) {if (node) {PostOrderEval(s->leftChild); PostOrderEval(s->rightChild);PostOrderEval(s->leftChild); PostOrderEval(s->rightChild);switch (s->data) {switch (s->data) {

case LogicalNot: s-> value = !(s->rightChild-case LogicalNot: s-> value = !(s->rightChild->value); break;>value); break;

case LogicalAnd: s->value = s->leftChild->value case LogicalAnd: s->value = s->leftChild->value && s->rightChild->value; break;&& s->rightChild->value; break;

case LogicalOr: s->value = s->leftChild->value || s-case LogicalOr: s->value = s->leftChild->value || s->rightChild->value; break;>rightChild->value; break;

case LogicalTrue: s->value = 1; break;case LogicalTrue: s->value = 1; break;case LogicalFalse: s-value = 0; break; } } }case LogicalFalse: s-value = 0; break; } } }

Page 25: 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.

Review QuestionsReview Questions

A

B

C

D

A

B C

D E

Inorder traversal? (LVR)

D C B A

D B E A C

Page 26: 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.

Review QuestionsReview Questions

A

B

C

D

A

B C

D E

Preorder traversal? (VLR)

A B C D

A B D E C

Page 27: 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.

Review QuestionsReview Questions

A

B

C

D

A

B C

D E

Postorder traversal? (LRV)

D C B A

D E B C A

Page 28: 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.

Review QuestionsReview 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; } }

Page 29: 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.

Priority QueuesPriority Queues

Standard queue definition:Standard queue definition: Add to back, remove from frontAdd to back, remove from front

Priority queue definition:Priority queue definition: Add arbitrary priority to back, remove highest Add arbitrary priority to back, remove highest

priority itempriority item Very common!Very common!

OS schedulingOS scheduling Packet switchingPacket switching

Page 30: 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.

Priority Queues – OS Job Priority Queues – OS Job SchedulingScheduling

CPU

turketwhls

turketwhg++

thomasdf -k

turketwhgnome

5 3 7 12

Next Job

rootbackup

15

Page 31: 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.

Priority QueuesPriority Queues

Other Examples:Other Examples: Selling service of a machine (renting Selling service of a machine (renting

carpet cleaners?)carpet cleaners?) Goal maximize number of users in a fixed Goal maximize number of users in a fixed

timetime Allow smallest homes (shortest rental time) to go Allow smallest homes (shortest rental time) to go

first (a first (a min prioritymin priority queue) queue) Goal maximize money in a fixed timeGoal maximize money in a fixed time

Allows homeowners willing to pay the most to go Allows homeowners willing to pay the most to go first (a first (a max prioritymax priority queue) queue)

Page 32: 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.

Priority QueuePriority Queue

Interface Definition:Interface Definition:

template <class Type>template <class Type>class MaxPriorityQueueclass MaxPriorityQueue{{

public:public:virtual void Insert(const Element<Type> & virtual void Insert(const Element<Type> &

toInsert) = 0;toInsert) = 0;virtual Element<Type>* Delete virtual Element<Type>* Delete

(Element<Type> &) = 0; //((Element<Type> &) = 0; //(DeleteMax or DeleteMax or DeleteMinDeleteMin))

}}

Page 33: 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.

Priority Queue Priority Queue Implementations 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)

Page 34: 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.

HeapsHeaps Heaps are a guaranteed fast way to Heaps are a guaranteed fast way to

implement priority queuesimplement priority queues

Two Heap definitions:Two Heap definitions: A max tree is a tree in which the key value in A max tree is a tree in which the key value in

each node is no smaller than the key values in each node is no smaller than the key values in its children (if any). A max heap is a complete its children (if any). A max heap is a complete binary tree that is also a max tree.binary tree that is also a max tree.

In a min tree, node keys are no larger than those In a min tree, node keys are no larger than those for a node’s children. Min heap is a complete for a node’s children. Min heap is a complete binary tree that is also min tree.binary tree that is also min tree.

Root is largest item in max tree, smallest item in Root is largest item in max tree, smallest item in min treemin tree

Page 35: 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.

Heap Examples:Heap Examples: Max Heaps:Max Heaps:

14

127

10 8 6

Complete binary? YesMax Tree Property? Yes

9

6

5

3

9

6

5

3

Complete binary? YesMax Tree Property? Yes

Complete binary? NoMax Tree Property? Yes

Not A Max Heap!

Page 36: 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.

Heap Examples:Heap Examples: Min Heaps:Min Heaps:

2

74

10 8 6

Complete binary? YesMin Tree Property? Yes

10

20

50

83

21

11

Complete binary? YesMin Tree Property? Yes

Complete binary? YesMin Tree Property? No

Not A Min Heap!

Page 37: 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.

Max Heap OperationsMax Heap Operations

Minimal Interface:Minimal Interface: Create an empty heapCreate an empty heap Insert new element into heapInsert new element into heap Delete largest element from the heapDelete largest element from the heap

Same functionality as priority queue!Same functionality as priority queue!

Page 38: 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.

Max Heap Definition: Max Heap Definition: VariablesVariables

Since heaps are complete binary trees, Since heaps are complete binary trees, simplest and densest implementation is via an simplest and densest implementation is via an arrayarray Could also implement with left, right Could also implement with left, right

pointers.pointers.

class MaxHeapclass MaxHeap{{

public:public:private:private:

Element<Type> *heap;Element<Type> *heap;int currentSize;int currentSize;int maxSize;int maxSize;

}}

Page 39: 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.

Max Heap Definition: Max Heap Definition: MethodsMethods

class MaxHeapclass MaxHeap{{

public:public:MaxHeap(int size);MaxHeap(int size);bool isFull();bool isFull();bool isEmpty();bool isEmpty();void insert(Element<KeyType> void insert(Element<KeyType>

toInsert);toInsert);Element<KeyType>* delete(KeyType& Element<KeyType>* delete(KeyType&

toDelete);toDelete);private:private:// see previous slide// see previous slide

}}

Page 40: 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.

Max Heap Max Heap ImplementationImplementation

ConstructorConstructor

template<class KeyType>template<class KeyType>MaxHeap<KeyType>::MaxHeap(int size)MaxHeap<KeyType>::MaxHeap(int size){{

maxSize = size;maxSize = size;currentSize = 0;currentSize = 0;heap = new Element<KeyType>[maxSize+1];heap = new Element<KeyType>[maxSize+1];// heap[0] not used – simplifies binary tree // heap[0] not used – simplifies binary tree

// mapping// mapping}}

isFull, isEmpty()isFull, isEmpty()

simple one-line conditional checkssimple one-line conditional checkscurrentSize == maxSize or currentSize == 0currentSize == maxSize or currentSize == 0

Page 41: 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.

Max Heap: InsertionMax Heap: Insertion

20

15

14

2

10

1

To Insert First Rule:Insert in correct placeto preserve completebinary tree

Second Rule:Ensure Max Treeproperty

1

Page 42: 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.

Max Heap: InsertionMax Heap: Insertion

20

15

14

2

10

5

To Insert First Rule:Insert in correct placeto preserve completebinary tree

Second Rule:Ensure Max TreeProperty

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

Page 43: 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.

Max Heap: InsertionMax Heap: Insertiontemplate <class KeyType>template <class KeyType>Void MaxHeap<KeyType>::Insert(const Element<Type> & Void MaxHeap<KeyType>::Insert(const Element<Type> &

toInsert)toInsert){{

if (isFull()) return;if (isFull()) return;currentSize = currentSize + 1;currentSize = currentSize + 1;for (int i = currentSize; true; )for (int i = currentSize; true; ){{

if (i == 1) break; // if at root stopif (i == 1) break; // if at root stopif (toInsert.key < heap[i/2].key) break; if (toInsert.key < heap[i/2].key) break;

// if smaller than parent stop (preserve max tree // if smaller than parent stop (preserve max tree property)property)

heap[i] = heap[i/2]; // swap with parentheap[i] = heap[i/2]; // swap with parenti = i /2;i = i /2; // set parent position as potential // set parent position as potential

insert insert // location // location}}heap[i] = toInsert;heap[i] = toInsert; // insert at appropriate place // insert at appropriate place

}}

Page 44: 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.

Max Heap: Insertion Big Max Heap: Insertion Big Oh AnalysisOh Analysis

Insertion: Insertion: Start Location: Leaf node (bottom of tree)Start Location: Leaf node (bottom of tree) End Location: Root maximally, often less than End Location: Root maximally, often less than

rootroot

Max distance between root and leaf node is Max distance between root and leaf node is loglog22(n+1)(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]

Page 45: 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.

Max Heap: DeletionMax Heap: Deletion

20

15

14

5

10 2

First Rule:Remove root node – Guaranteed largest

Second Rule:Preserve completebinary tree property -Move rightmost, lowestinto root position

Third Rule:Preserve max treeproperty-Repeatedly push rootdown, swapping with larger of two children

2

15

14

5

10

15

14

2

5

10

Page 46: 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.

Max Heap: Deletion Max Heap: Deletion ImplementationImplementation

template <class KeyType>template <class KeyType>Element<KeyType>* MaxHeap<KeyType>::DeleteMax(Element <KeyType> Element<KeyType>* MaxHeap<KeyType>::DeleteMax(Element <KeyType>

& x)& x){{

if (isEmpty()) return 0;if (isEmpty()) return 0;x = heap[1]; // grab root to returnx = heap[1]; // grab root to returnElement<KeyType> k = heap[currentSize]; // grab lowest rightmost itemElement<KeyType> k = heap[currentSize]; // grab lowest rightmost itemcurrentSize = currentSize – 1;currentSize = currentSize – 1;for (int i= 1, j = 2; j <= currentSize; )for (int i= 1, j = 2; j <= currentSize; ){{

if (j < currentSize) if (heap[j].key < heap[j+1].key) j++;if (j < currentSize) if (heap[j].key < heap[j+1].key) j++;// j points to the larger child now// j points to the larger child nowif (k.key >= heap[j].key) break; // in right place alreadyif (k.key >= heap[j].key) break; // in right place alreadyheap[i] = heap[j];heap[i] = heap[j]; // move child up if not in right place// move child up if not in right placei = j; j *= 2;i = j; j *= 2; // move i,j down to continue swapping// move i,j down to continue swapping

}}heap[i] = k;heap[i] = k; // put in right place// put in right placereturn &x;return &x;

}}

Page 47: 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.

Max Heap: Deletion Big Max Heap: Deletion Big O AnalysisO Analysis

1 step to remove root node1 step to remove root node 1 step to move bottom, right node to root1 step to move bottom, right node to root Potentially could move all the way down Potentially could move all the way down

the tree in preserving max tree propertythe tree in preserving max tree property Max height = logMax height = log22(n+1)(n+1) Each move is O(1) – a comparisonEach move is O(1) – a comparison

Overall work is O(logOverall work is O(log22n)n)

Page 48: 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.

Priority Queue Big Oh Priority Queue Big Oh Comparisons:Comparisons:

Max Heap implementation of priority Max Heap implementation of priority queue:queue: O(log n) insert O(log n) deleteO(log n) insert O(log n) delete

Unsorted arrayUnsorted array O(1) insertO(1) insert O(n) deleteO(n) delete

Sorted arraySorted array O(n) insertO(n) insert O(1) deleteO(1) delete