trees trees are a very useful data structure. many different kinds of trees are used in computer...

166
Trees

Upload: clementine-riley

Post on 08-Jan-2018

222 views

Category:

Documents


5 download

DESCRIPTION

Trees Introduction Binary Trees Binary Tree Traversal Additional Binary Tree Operations Heaps Binary Search Trees

TRANSCRIPT

Page 1: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Trees

Page 2: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

2

Trees

• Introduction• Binary Trees• Binary Tree Traversal• Additional Binary Tree Operations• Heaps• Binary Search Trees

Page 3: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Nature Lover’s View Of A Tree

root

branches

leaves

Page 4: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Computer Scientist’s View

branches

leavesroot

nodes

Page 5: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Linear Lists And Trees• Linear lists are useful for serially ordered data.

(e0, e1, e2, …, en-1) Days of week. Months in a year. Students in this class.

• Trees are useful for hierarchically ordered data. Employees of a corporation.

• President, vice presidents, managers, and so on.

Page 6: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Hierarchical Data And Trees

• The element at the top of the hierarchy is the root.

• Elements next in the hierarchy are the children of the root.

• Elements next in the hierarchy are the grandchildren of the root, and so on.

• Elements that have no children are leaves.

Page 7: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

great grand child of root

grand children of root

children of root

Example Tree

President

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

root

Page 8: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Definition

• A tree t is a finite nonempty set of elements.• One of these elements is called the root.• The remaining elements, if any, are

partitioned into trees, which are called the subtrees of t.

Page 9: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Subtrees

President

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

root

Page 10: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Leaves

President

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

Page 11: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Parent, Grandparent, Siblings, Ancestors, Descendants

President

VP1 VP2 VP3

Manager2 Manager Manager

Worker Bee

Manager1

Page 12: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Level 4

Level 3

Level 2

Levels

President

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

Level 1

Page 13: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Caution

• Some texts start level numbers at 0 rather than at 1.

• Root is at level 0.• Its children are at level 1.• The grand children of the root are at level 2.• And so on.• We shall number levels with the root at level 1.

Page 14: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

height = depth = number of levels

Level 4

Level 3

Level 2

President

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

Level 1

Page 15: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Node Degree = Number Of ChildrenPresident

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

3

2 1 1

0 0 1 0

0

Page 16: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Tree Degree = Max Node Degree

Degree of tree = 3.

President

VP1 VP2 VP3

Manager1 Manager2 Manager Manager

Worker Bee

3

2 1 1

0 0 1 0

0

Page 17: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree

• Finite (possibly empty) collection of elements.• A nonempty binary tree has a root element.• The remaining elements (if any) are partitioned

into two binary trees.• These are called the left and right subtrees of

the binary tree.

Page 18: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Differences Between A Tree & A Binary Tree

• No node in a binary tree may have a degree more than 2, whereas there is no limit on the degree of a node in a tree.

• A binary tree may be empty; a tree cannot be empty.

Page 19: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Differences Between A Tree & A Binary Tree

• The subtrees of a binary tree are ordered; those of a tree are not ordered.

a

b

a

b

• Are different when viewed as binary trees.• Are the same when viewed as trees.

Page 20: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Properties & Representation

Page 21: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Minimum Number Of Nodes• Minimum number of nodes in a binary tree whose height is h.• At least one node at each of first h levels.

minimum number of nodes is h

Page 22: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Maximum Number Of Nodes• All possible nodes at first h levels are present.

Maximum number of nodes

= 1 + 2 + 4 + 8 + … + 2h-1

= 2h - 1

Page 23: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Number Of Nodes & Height

• Let n be the number of nodes in a binary tree whose height is h.

• h <= n <= 2h – 1

• log2(n+1) <= h <= n

Page 24: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Full Binary Tree

• A full binary tree of a given height h has 2h – 1 nodes.

Height 4 full binary tree.

Page 25: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Numbering Nodes In A Full Binary Tree

• Number the nodes 1 through 2h – 1. • Number by levels from top to bottom.• Within a level number from left to right.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 26: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Node Number Properties

• Parent of node i is node i / 2, unless i = 1.• Node 1 is the root and has no parent.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 27: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Node Number Properties

• Left child of node i is node 2i, unless 2i > n, where n is the number of nodes.

• If 2i > n, node i has no left child.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 28: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Node Number Properties

• Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes.

• If 2i+1 > n, node i has no right child.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 29: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Complete Binary Tree With n Nodes

• Start with a full binary tree that has at least n nodes.

• Number the nodes as described earlier.• The binary tree defined by the nodes

numbered 1 through n is the unique n node complete binary tree.

Page 30: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Example

• Complete binary tree with 10 nodes.

1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

Page 31: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Representation

• Array representation.• Linked representation.

Page 32: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Array Representation• Number the nodes using the numbering scheme

for a full binary tree. The node that is numbered i is stored in tree[i].

tree[]0 5 10

a b c d e f g h i j

b

a

c

d e f g

h i j

1

2 3

4 5 6 7

8 9 10

Page 33: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Right-Skewed Binary Tree

• An n node binary tree needs an a r ray w hose length is between n+1 and 2n.

a

b

1

3

c7

d15

tree[]0 5 10

a - b - - - c - - - - - - -15d

Page 34: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Linked Representation

• Each binary tree node is represented as an object whose data type is TreeNode.

• The space required by an n node binary tree is n * (space required by one node).

Page 35: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Struct binaryTreeNodetemplate <class T>

class TreeNode

{

T data;

TreeNode<T> *leftChild,

*rightChild;

TreeNode()

{leftChild = rightChild = NULL;}

// other constructors come here

};

Page 36: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Linked Representation Examplea

cb

d

f

e

g

hleftChilddatarightChild

root

Page 37: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Some Binary Tree Operations• Determine the height.• Determine the number of nodes.• Make a clone.• Determine if two binary trees are clones.• Display the binary tree.• Evaluate the arithmetic expression

represented by a binary tree.• Obtain the infix form of an expression.• Obtain the prefix form of an expression.• Obtain the postfix form of an expression.

Page 38: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Traversal

• Many binary tree operations are done by performing a traversal of the binary tree.

• In a traversal, each element of the binary tree is visited exactly once.

• During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

Page 39: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Traversal Methods

• Preorder• Inorder• Postorder• Level order

Page 40: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Traversal Methods

• In a traversal of a binary tree, each element of the binary tree is visited exactly once.

• During the visit of an element, all action (make a clone, display, evaluate the operator, etc.) with respect to this element is taken.

Page 41: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Traversal Methods

• Preorder• Inorder• Postorder• Level order

Page 42: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Preorder Traversaltemplate <class T>void PreOrder(TreeNode<T> *t){ if (t != NULL) { Visit(t); PreOrder(t->leftChild); PreOrder(t->rightChild); }}

Page 43: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Preorder Example (Visit = print)a

b c

a b c

Page 44: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Preorder Example (Visit = print)a

b c

d ef

g h i j

a b d g h e i c f j

Page 45: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Preorder Of Expression Tree

+a b

-c d

+e f

*

/

Gives prefix form of expression!

/ * + a b - c d + e f

Page 46: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder Traversaltemplate <class T>void InOrder(TreeNode<T> *t){ if (t != NULL) { InOrder(t->leftChild); Visit(t); InOrder(t->rightChild); }}

Page 47: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder Example (Visit = print)a

b c

b a c

Page 48: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder Example (Visit = print)a

b c

d ef

g h i j

g d h b e i a f j c

Page 49: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder By Projection (Squishing)a

b c

d ef

g h i j

g d h b e i a f j c

Page 50: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder Of Expression Tree

+a b

-c d

+e f

*

/

Gives infix form of expression (sans parentheses)!

ea + b * c d / + f-

Page 51: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Postorder Traversaltemplate <class T>void PostOrder(TreeNode<T> *t){ if (t != NULL) { PostOrder(t->leftChild); PostOrder(t->rightChild); Visit(t); }}

Page 52: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Postorder Example (Visit = print)a

b c

b c a

Page 53: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Postorder Example (Visit = print)a

b c

d ef

g h i j

g h d i e b j f c a

Page 54: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Postorder Of Expression Tree

+a b

-c d

+e f

*

/

Gives postfix form of expression!

a b + c d - * e f + /

Page 55: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Traversal Applicationsa

b c

d ef

g h i j

• Make a clone.

• Determine height.

•Determine number of nodes.

Page 56: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Level Order

Let t be the tree root.while (t != NULL){ visit t and put its children on a FIFO queue; if FIFO queue is empty, set t = NULL; otherwise, pop a node from the FIFO queue

and call it t;}

Page 57: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Level-Order Example (Visit = print)a

b c

d ef

g h i j

a b c d e f g h i j

Page 58: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Construction• Suppose that the elements in a binary tree

are distinct.• Can you construct the binary tree from

which a given traversal sequence came?• When a traversal sequence has more than

one element, the binary tree is not uniquely defined.

• Therefore, the tree from which the sequence was obtained cannot be reconstructed uniquely.

Page 59: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Some Examplespreorder

= aba

b

a

b

inorder = ab

b

a

a

b

postorder = ab

b

a

b

a

level order = ab

a

b

a

b

Page 60: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Tree Construction

• Can you construct the binary tree, given two traversal sequences?

• Depends on which two sequences are given.

Page 61: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Preorder And Postorder

preorder = ab a

b

a

bpostorder = ba

• Preorder and postorder do not uniquely define a binary tree.

• Nor do preorder and level order (same example).

• Nor do postorder and level order (same example).

Page 62: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder And Preorder• inorder = g d h b e i a f j c• preorder = a b d g h e i c f j• Scan the preorder left to right using the

inorder to separate left and right subtrees.• a is the root of the tree; gdhbei are in the

left subtree; fjc are in the right subtree.

a

gdhbei fjc

Page 63: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder And Preorder

• preorder = a b d g h e i c f j• b is the next root; gdh are in the left

subtree; ei are in the right subtree.

a

gdhbei fjc

a

gdh

fjcb

ei

Page 64: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder And Preorder

• preorder = a b d g h e i c f j• d is the next root; g is in the left

subtree; h is in the right subtree.

a

gdh

fjcb

ei

a

g

fjcb

eid

h

Page 65: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder And Postorder

• Scan postorder from right to left using inorder to separate left and right subtrees.

• inorder = g d h b e i a f j c• postorder = g h d i e b j f c a• Tree root is a; gdhbei are in left subtree; fjc

are in right subtree.

Page 66: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inorder And Level Order

• Scan level order from left to right using inorder to separate left and right subtrees.

• inorder = g d h b e i a f j c• level order = a b c d e f g h i j• Tree root is a; gdhbei are in left subtree; fjc

are in right subtree.

Page 67: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Priority Queues

Two kinds of priority queues:• Min priority queue.• Max priority queue.

Page 68: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Min Priority Queue

• Collection of elements.• Each element has a priority or key.• Supports following operations:

empty size insert an element into the priority queue (push) get element with min priority (top) remove element with min priority (pop)

Page 69: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Max Priority Queue

• Collection of elements.• Each element has a priority or key.• Supports following operations:

empty size insert an element into the priority queue (push) get element with max priority (top) remove element with max priority (pop)

Page 70: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Complexity Of Operations

Use a heap.

empty, size, and top => O(1) time

insert (push) and remove (pop) => O(log n) time where n is the size of the priority queue

Page 71: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

ApplicationsSorting• use element key as priority• insert elements to be sorted into a priority queue• remove/pop elements in priority order

if a min priority queue is used, elements are extracted in ascending order of priority (or key)

if a max priority queue is used, elements are extracted in descending order of priority (or key)

Page 72: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Sorting ExampleSort five elements whose keys are 6, 8, 2, 4, 1

using a max priority queue. Insert the five elements into a max priority queue. Do five remove max operations placing removed

elements into the sorted array from right to left.

Page 73: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

After Inserting Into Max Priority Queue

Sorted Array

68

2

41 Max Priority

Queue

Page 74: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

After First Remove Max Operation

Sorted Array

6

2

41

8

Max Priority Queue

Page 75: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

After Second Remove Max Operation

Sorted Array

2

41

86

Max Priority Queue

Page 76: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

After Third Remove Max Operation

Sorted Array

21

864

Max Priority Queue

Page 77: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

After Fourth Remove Max Operation

Sorted Array

1

8642

Max Priority Queue

Page 78: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

After Fifth Remove Max Operation

Sorted Array

86421

Max Priority Queue

Page 79: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Complexity Of Sorting

Sort n elements. n insert operations => O(n log n) time. n remove max operations => O(n log n) time. total time is O(n log n). compare with O(n2) for insertion sort.

Page 80: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Heap Sort

Uses a max priority queue that is implemented as a heap.

Initial insert operations are replaced by a heap initialization step that takes O(n) time.

Page 81: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Machine Scheduling m identical machines (drill press, cutter, sander,

etc.) n jobs/tasks to be performed assign jobs to machines so that the time at which

the last job completes is minimum

Page 82: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Machine Scheduling Example

3 machines and 7 jobsjob times are [6, 2, 3, 5, 10, 7, 14]possible schedule

A

B

Ctime ----------->

6

2

3

7

13

13

21

Page 83: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Machine Scheduling Example

Finish time = 21Objective: Find schedules with minimum finish time.

A

B

Ctime ----------->

6

2

3

7

13

13

21

Page 84: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

LPT Schedules

Longest Processing Time first.Jobs are scheduled in the order

14, 10, 7, 6, 5, 3, 2

Each job is scheduled on the machine on which it finishes earliest.

Page 85: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

LPT Schedule

[14, 10, 7, 6, 5, 3, 2]

A

B

C

14

10

7 13

15

16

16

Finish time is 16!

Page 86: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

LPT Schedule

• LPT rule does not guarantee minimum finish time schedules.

• (LPT Finish Time)/(Minimum Finish Time) <= 4/3 - 1/(3m) where m is number of machines.

• Usually LPT finish time is much closer to minimum finish time.

• Minimum finish time scheduling is NP-hard.

Page 87: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Complexity Of LPT Scheduling• Sort jobs into decreasing order of task time.

O(n log n) time (n is number of jobs)• Schedule jobs in this order.

assign job to machine that becomes available first must find minimum of m (m is number of machines)

finish times takes O(m) time using simple strategy so need O(mn) time to schedule all n jobs.

Page 88: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Using A Min Priority Queue

• Min priority queue has the finish times of the m machines.

• Initial finish times are all 0.• To schedule a job remove machine with

minimum finish time from the priority queue.• Update the finish time of the selected machine

and insert the machine back into the priority queue.

Page 89: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Using A Min Priority Queue

• m put operations to initialize priority queue• 1 remove min and 1 insert to schedule each job• each insert and remove min operation takes

O(log m) time• time to schedule is O(n log m)• overall time is

O(n log n + n log m) = O(n log (mn))

Page 90: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Min Tree DefinitionEach tree node has a value.Value in any node is the minimum value in

the subtree for which that node is the root.Equivalently, no descendent has a smaller

value.

Page 91: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Min Tree Example

2

4 9 3

4 8 7

9 9

Root has minimum element.

Page 92: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Max Tree Example

9

4 9 8

4 2 7

3 1

Root has maximum element.

Page 93: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Min Heap Definition

• complete binary tree• min tree

Page 94: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Min Heap With 9 Nodes

Complete binary tree with 9 nodes.

Page 95: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Min Heap With 9 Nodes

Complete binary tree with 9 nodes that is also a min tree.

2

4

6 7 9 3

8 6

3

Page 96: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Max Heap With 9 Nodes

Complete binary tree with 9 nodes that is also a max tree.

9

8

6 7 2 6

5 1

7

Page 97: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Heap Height

Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).

Page 98: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

9 8 7 6 7 2 6 5 11 2 3 4 5 6 7 8 9 1

00

A Heap Is Efficiently Represented As An Array

9

8

6 7 2 6

5 1

7

Page 99: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Moving Up And Down A Heap

9

8

6 7 2 6

5 1

7

1

2 3

4 5 6 7

8 9

Page 100: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

Complete binary tree with 10 nodes.

9

8

6 7 2 6

5 1

7

7

Page 101: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 5.

9

8

6 7 2 6

5 1

7

75

Page 102: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

7

7

Page 103: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 20.

9

8

6

7

2 6

5 1

7

77

Page 104: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

Page 105: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 20.

9

86

7

2 6

5 1

7

77

20

Page 106: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

Complete binary tree with 11 nodes.

9

86

7

2 6

5 1

7

77

20

Page 107: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 15.

9

86

7

2 6

5 1

7

77

20

Page 108: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 15.

9

8

6

7

2 6

5 1

7

77

20

8

Page 109: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Inserting An Element Into A Max Heap

New element is 15.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 110: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Complexity Of Insert

Complexity is O(log n), where n is heap size.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 111: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Max element is in the root.

8

6

7

2 6

5 1

7

77

20

8

9

15

Page 112: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

After max element is removed.

8

6

7

2 6

5 1

7

77 8

9

15

Page 113: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Heap with 10 nodes.

8

6

7

2 6

5 1

7

77 8

9

15

Reinsert 8 into the heap.

Page 114: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 115: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

Page 116: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Reinsert 8 into the heap.

6

7

2 6

5 1

7

77

9

15

8

Page 117: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Max element is 15.

6

7

2 6

5 1

7

77

9

15

8

Page 118: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

After max element is removed.

6

7

2 6

5 1

7

77

9

8

Page 119: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Heap with 9 nodes.

6

7

2 6

5 1

7

77

9

8

Page 120: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Reinsert 7.

6 2 6

5 1

79

8

Page 121: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

Page 122: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Removing The Max Element

Reinsert 7.

6 2 6

5 1

7

9

8

7

Page 123: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Complexity Of Remove Max Element

Complexity is O(log n).

6 2 6

5 1

7

9

8

7

Page 124: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

8

4

7

6 7

8 9

3

710

1

11

5

2

Page 125: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

Start at rightmost array position that has a child.

8

4

7

6 7

8 9

3

710

1

11

5

2

Index is n/2.

Page 126: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

Move to next lower array position.

8

4

7

6 7

8 9

3

710

1

5

11

2

Page 127: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

4

7

6 7

8 9

3

710

1

5

11

2

Page 128: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 7

8 4

3

710

1

5

11

2

Page 129: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 7

8 4

3

710

1

5

11

2

Page 130: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

2

Page 131: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

2

Page 132: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

710

1

5

11

Find a home for 2.

Page 133: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

75

1

11

Find a home for 2.

10

Page 134: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

1

11

Done, move to next lower array position.

10

5

Page 135: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

1

11

10

5

Find home for 1.

Page 136: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

11

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

10

5

Find home for 1.

Page 137: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Find home for 1.

Page 138: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Find home for 1.

Page 139: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Initializing A Max Heap

8

9

7

6 3

8 4

7

72

11

10

5

Done.

1

Page 140: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Time Complexity

87

6 3

4

7

710

11

5

2

9

8

1Height of heap = h.

Number of subtrees with root at level j is <= 2 j-1.

Time for each subtree is O(h-j+1).

Page 141: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Complexity

Time for level j subtrees is <= 2j-1(h-j+1) = t(j).

Total time is t(1) + t(2) + … + t(h-1) = O(n).

Page 142: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Binary Search Trees

• Dictionary Operations: IsEmpty() Get(key) Insert(key, value) Delete(key)

Page 143: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Definition Of Binary Search Tree

• A binary tree.• Each node has a (key, value) pair.• For every node x, all keys in the left

subtree of x are smaller than that in x.• For every node x, all keys in the right

subtree of x are greater than that in x.

Page 144: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Example Binary Search Tree

20

10

6

2 8

15

40

30

25

Only keys are shown.

Page 145: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Operation Ascend()

20

10

6

2 8

15

40

30

25

Do an inorder traversal. O(n) time.

Page 146: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Operation Get()

20

10

6

2 8

15

40

30

25

Complexity is O(height) = O(n), where n is number of nodes/elements.

Page 147: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Operation Insert()

20

10

6

2 8

15

40

30

25

Insert a pair whose key is 35.

35

Page 148: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Operation Insert()

Insert a pair whose key is 7.

20

10

6

2 8

15

40

30

25 35

7

Page 149: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Operation Insert()

20

10

6

2 8

15

40

30

25

Insert a pair whose key is 18.

35

7

18

Page 150: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Operation Insert()

20

10

6

2 8

15

40

30

25

Complexity of Insert() is O(height).

35

7

18

Page 151: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

The Operation Delete()

Four cases:

No element with delete key.

Element is in a leaf.

Element is in a degree 1 node.

Element is in a degree 2 node.

Page 152: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Leaf

Delete a leaf element. key = 7

20

10

6

2 8

15

40

30

25 35

7

18

Page 153: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Leaf (contd.)

Delete a leaf element. key = 35

20

10

6

2 8

15

40

30

25 35

7

18

Page 154: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 1 Node

Delete from a degree 1 node. key = 40

20

10

6

2 8

15

40

30

25 35

7

18

Page 155: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 1 Node (contd.)

Delete from a degree 1 node. key = 15

20

10

6

2 8

15

40

30

25 35

7

18

Page 156: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

Delete from a degree 2 node. key = 10

20

10

6

2 8

15

40

30

25 35

7

18

Page 157: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

Page 158: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

Page 159: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

20

8

6

2 8

15

40

30

25

Replace with largest key in left subtree (or smallest in right subtree).

35

7

18

Page 160: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

20

8

6

2 8

15

40

30

25

Largest key must be in a leaf or degree 1 node.

35

7

18

Page 161: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Another Delete From A Degree 2 Node

Delete from a degree 2 node. key = 20

20

10

6

2 8

15

40

30

25 35

7

18

Page 162: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

Page 163: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

20

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

Page 164: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

18

10

6

2 8

15

40

30

25

Replace with largest in left subtree.

35

7

18

Page 165: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

Delete From A Degree 2 Node

18

10

6

2 8

15

40

30

25

Complexity is O(height).

35

7

Page 166: Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these

166

Exercises

• Page 204, Exercise 3• Page 204, Exercise 4• Page 215, Exercise 2• Page 230, Exercise 4