trees trees are a very useful data structure. many different kinds of trees are used in computer...
DESCRIPTION
Trees Introduction Binary Trees Binary Tree Traversal Additional Binary Tree Operations Heaps Binary Search TreesTRANSCRIPT
![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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/28.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/29.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/30.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/31.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/33.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/39.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/40.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/41.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/42.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/43.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/44.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/45.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/46.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/47.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/48.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/49.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/50.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/51.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/52.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/53.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/54.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/55.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/56.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/57.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/58.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/59.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/60.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/61.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/62.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/63.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/64.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/65.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/66.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/67.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/68.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/69.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/70.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/71.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/72.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/73.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/74.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/75.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/76.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/77.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/78.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/79.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/80.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/81.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/82.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/83.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/84.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/85.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/86.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/87.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/88.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/89.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/90.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/91.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/92.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/93.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/94.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/95.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/96.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/97.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/98.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/99.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/100.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/101.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/102.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/103.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/104.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/105.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/106.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/107.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/108.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/109.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/110.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/111.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/112.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/113.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/114.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/115.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/116.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/117.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/118.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/119.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/120.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/121.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/122.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/123.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/124.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/125.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/126.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/127.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/128.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/129.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/130.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/131.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/132.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/133.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/134.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/135.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/136.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/137.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/138.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/139.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/140.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/141.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/142.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/143.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/144.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/145.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/146.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/147.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/148.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/149.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/150.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/151.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/152.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/153.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/154.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/155.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/156.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/157.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/158.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/159.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/160.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/161.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/162.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/163.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/164.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/165.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081515/5a4d1af07f8b9ab05997dd2a/html5/thumbnails/166.jpg)
166
Exercises
• Page 204, Exercise 3• Page 204, Exercise 4• Page 215, Exercise 2• Page 230, Exercise 4