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

Slides:



Advertisements
Similar presentations
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue.
Advertisements

1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees, Binary Trees, and Binary Search Trees COMP171.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Nature Lover’s View Of A Tree root branches leaves.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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,
Fundamentals of Python: From First Programs Through Data Structures
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Priority Queues, Heaps & Leftist Trees
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Trees Nature Lover’s View Of A Tree root branches leaves.
© University of Auckland Trees CS 220 Data Structures & Algorithms Dr. Ian Watson.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
درختها Trees ساختمان داده ها والگوريتمها مقايسه ليست خطي و درخت Linear lists are useful for serially ordered data. – (e 0, e 1, e 2, …, e n-1 ) – Days.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Trees  Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
Priority Queue. Priority Queues Queue (FIFO). Priority queue. Deletion from a priority queue is determined by the element priority. Two kinds of priority.
Priority Queues Two kinds of priority queues: Min priority queue. Max priority queue. Nov 4,
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Disusun Oleh : Budi Arifitama Pertemuan ke-8. Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
Initializing A Max Heap input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Lab 4 Due date: March 29. Linked Representation Each binary tree node is represented as an object whose data type is binaryTreeNode. The space required.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Trees Chapter 15.
Priority Queues An abstract data type (ADT) Similar to a queue
Trees Another Abstract Data Type (ADT)
Trees Trees 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 Trees 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 Trees, Binary Search Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Trees Another Abstract Data Type (ADT)
Binary Tree Traversal Methods
Binary Trees, Binary Search Trees
Priority Queues An abstract data type (ADT) Similar to a queue
Binary Tree Properties & Representation
การวิเคราะห์และออกแบบขั้นตอนวิธี
Binary Tree Traversal Methods
Trees Trees 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 Trees, Binary Search Trees
A Heap Is Efficiently Represented As An Array
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Trees Trees 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 Introduction Binary Trees Binary Tree Traversal Additional Binary Tree Operations Heaps Binary Search Trees

Nature Lover’s View Of A Tree leaves branches root

Computer Scientist’s View root leaves branches nodes Branches meet at nodes.

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.

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.

Example Tree root President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee children of root grand children of root great grand child of root

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.

Subtrees root President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee

Leaves President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

Parent, Grandparent, Siblings, Ancestors, Descendants President VP3 VP1 VP2 Manager Manager1 Manager2 Manager Worker Bee

Levels Level 1 President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee Level 2 Level 3 Level 4

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.

height = depth = number of levels President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee Level 1

Node Degree = Number Of Children President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 1 1

Tree Degree = Max Node Degree President VP1 VP2 VP3 Manager1 Manager2 Manager Worker Bee 3 2 1 Note that it is possible to have a tree whose degree is (say) 3 and whose root has a degree that is < 3. Degree of tree = 3.

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.

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.

Differences Between A Tree & A Binary Tree The subtrees of a binary tree are ordered; those of a tree are not ordered. a b Are different when viewed as binary trees. Are the same when viewed as trees.

Binary Tree Properties & Representation

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

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

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

Full Binary Tree A full binary tree of a given height h has 2h – 1 nodes. Height 4 full binary tree.

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 6 5 7 8 9 10 11 12 13 14 15

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent.

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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.

Node Number Properties 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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.

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.

Example Complete binary tree with 10 nodes. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Complete binary tree with 10 nodes.

Binary Tree Representation Array representation. Linked representation.

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]. b a c d e f g h i j 1 2 3 4 5 6 7 8 9 10 tree[] 5 10 a b c d e f g h i j

Right-Skewed Binary Tree 1 3 c 7 d 15 tree[] 5 10 a - b c 15 d An n node binary tree needs an array whose length is between n+1 and 2n.

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). Notice that a linked representation always take space that is linear in the number of elements in the tree. An array representation may take an exponential amount of space!

The Struct binaryTreeNode template <class T> class TreeNode { T data; TreeNode<T> *leftChild, *rightChild; TreeNode() {leftChild = rightChild = NULL;} // other constructors come here }; TreeNode could have 2 additional constructors. One sets the element field to a user-specified value and the child fields to NULL; the other sets all 3 fields to user-specified values. Struct is better than class as everyone who uses a TreeNode will need to access its fields.

Linked Representation Example c b d f e g h leftChild data rightChild root

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.

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.

Binary Tree Traversal Methods Preorder Inorder Postorder Level order

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.

Binary Tree Traversal Methods Preorder Inorder Postorder Level order

Preorder Traversal template <class T> void PreOrder(TreeNode<T> *t) { if (t != NULL) Visit(t); PreOrder(t->leftChild); PreOrder(t->rightChild); } t is the root of the subtree being traversed.

Preorder Example (Visit = print) b c a b c

Preorder Example (Visit = print) b c d e f g h i j During the traversal, t starts at the root, moves to the left child b of the root, then to the left child d of b. When the traversal of the left subtree of b is complete, t, once again, points to the node b. The t moves into the right subtree of b. When the traversal of this right subtree is complete, t again points to b. Following this, t points to a. We see that t points to every node in the binary tree three times – once when you get to the node from its parent (or in the case of the root, t is initially at the root), once when you return from the left subtree of the node, and once when you return from the node’s right subtree. Of these three times that t points to a node, the node is visited the first time. a b d g h e i c f j

Preorder Of Expression Tree + a b - c d e f * / / * + a b - c d + e f Gives prefix form of expression!

Inorder Traversal template <class T> void InOrder(TreeNode<T> *t) { if (t != NULL) InOrder(t->leftChild); Visit(t); InOrder(t->rightChild); } t is the root of the subtree being traversed.

Inorder Example (Visit = print) b c b a c

Inorder Example (Visit = print) b c d e f g h i j g d h b e i a f j c

Inorder By Projection (Squishing) a b c d e f g h i j g d h b e i a f j c

Inorder Of Expression Tree + a b - c d e f * / e a + b * c d / f - Gives infix form of expression (sans parentheses)!

Postorder Traversal template <class T> void PostOrder(TreeNode<T> *t) { if (t != NULL) PostOrder(t->leftChild); PostOrder(t->rightChild); Visit(t); } t is the root of the subtree being traversed.

Postorder Example (Visit = print) b c b c a

Postorder Example (Visit = print) b c d e f g h i j g h d i e b j f c a

Postorder Of Expression Tree + a b - c d e f * / a b + c d - * e f + / Gives postfix form of expression!

Traversal Applications b c d e f g h i j Make a clone using postorder traversal … clone the left subtree, clone the right subtree, clone the root in the visit step. Determine height using postorder traversal … determine the height of the left subtree, determine the height of the right subtree, in the visit step add 1 to the max of the already determined heights of the left and right subtrees. Determine number of nodes using preorder, inorder, or postorder traversal … initialize a counter to 0, add 1 to the counter in the visit step. Make a clone. Determine height. Determine number of nodes.

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

Level-Order Example (Visit = print) b c d e f g h i j a b c d e f g h i j

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.

Some Examples preorder = ab inorder = ab postorder = ab level order = ab a b a b

Binary Tree Construction Can you construct the binary tree, given two traversal sequences? Depends on which two sequences are given.

Preorder And Postorder preorder = ab a b a b postorder = 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).

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

Inorder And Preorder preorder = a b d g h e i c f j gdhbei fjc 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 gdh fjc b ei

Inorder And Preorder preorder = a b d g h e i c f j gdh fjc b ei 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 g fjc b ei d h

Inorder And Postorder inorder = g d h b e i a f j c 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.

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.

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

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)

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)

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

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

Sorting Example Sort 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.

After Inserting Into Max Priority Queue 8 4 6 Max Priority Queue 1 2 Sorted Array

After First Remove Max Operation 4 6 Max Priority Queue 1 2 8 Sorted Array

After Second Remove Max Operation 4 Max Priority Queue 1 2 6 8 Sorted Array

After Third Remove Max Operation Max Priority Queue 1 2 4 6 8 Sorted Array

After Fourth Remove Max Operation Max Priority Queue 1 2 4 6 8 Sorted Array

After Fifth Remove Max Operation Max Priority Queue 1 2 4 6 8 Sorted Array

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.

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.

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

Machine Scheduling Example 3 machines and 7 jobs job times are [6, 2, 3, 5, 10, 7, 14] possible schedule 6 13 A Example schedule is constructed by scheduling the jobs in the order they appear in the given job list (left to right); each job is scheduled on the machine on which it will complete earliest. 2 7 21 B 3 13 C time ----------->

Machine Scheduling Example 6 13 A 2 7 21 B 3 13 C time -----------> Finish time = 21 Objective: Find schedules with minimum finish time.

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.

LPT Schedule [14, 10, 7, 6, 5, 3, 2] 14 16 A 10 15 B 7 13 16 C Finish time is 16!

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.

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.

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.

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

Min Tree Definition Each 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.

Root has minimum element. Min Tree Example 2 4 9 3 4 8 7 9 9 Root has minimum element.

Root has maximum element. Max Tree Example 9 4 9 8 4 2 7 3 1 Root has maximum element.

complete binary tree min tree Min Heap Definition complete binary tree min tree

Complete binary tree with 9 nodes. Min Heap With 9 Nodes Complete binary tree with 9 nodes.

Complete binary tree with 9 nodes that is also a min tree. Min Heap With 9 Nodes 2 4 6 7 9 3 8 Complete binary tree with 9 nodes that is also a min tree.

Complete binary tree with 9 nodes that is also a max tree. Max Heap With 9 Nodes 9 8 6 7 2 5 1 Complete binary tree with 9 nodes that is also a max tree.

Heap Height Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1). Since height must be an integer, the precise expression for height is ceiling(log2 (n+1)).

A Heap Is Efficiently Represented As An Array 9 8 6 7 2 5 1 9 8 7 6 7 2 6 5 1 1 2 3 4 5 6 7 8 9 10

Moving Up And Down A Heap 9 8 6 7 2 5 1 3 4

Inserting An Element Into A Max Heap 9 8 6 7 2 5 1 7 Complete binary tree with 10 nodes.

Inserting An Element Into A Max Heap 9 8 7 6 7 2 6 5 1 5 7 New element is 5.

Inserting An Element Into A Max Heap 9 8 7 6 7 2 6 5 1 7 7 New element is 20.

Inserting An Element Into A Max Heap 9 8 7 6 2 6 5 1 7 7 7 New element is 20.

Inserting An Element Into A Max Heap 9 7 6 8 2 6 5 1 7 7 7 New element is 20.

Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 20.

Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 Complete binary tree with 11 nodes.

Inserting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 15.

Inserting An Element Into A Max Heap 20 9 7 6 2 6 5 1 7 7 8 7 8 New element is 15.

Inserting An Element Into A Max Heap 20 15 7 6 9 2 6 5 1 7 7 8 7 8 New element is 15.

Complexity is O(log n), where n is heap size. Complexity Of Insert 8 6 7 2 5 1 20 9 15 Complexity is O(log n), where n is heap size.

Removing The Max Element 8 6 7 2 5 1 20 9 15 Max element is in the root.

Removing The Max Element 15 7 6 9 2 6 5 1 7 7 8 7 8 After max element is removed.

Removing The Max Element 15 7 6 9 2 6 5 1 7 7 8 7 8 Heap with 10 nodes. Reinsert 8 into the heap.

Removing The Max Element 15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

Removing The Max Element 15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.

Removing The Max Element 15 9 7 6 8 2 6 5 1 7 7 7 Reinsert 8 into the heap.

Removing The Max Element 15 9 7 6 8 2 6 5 1 7 7 7 Max element is 15.

Removing The Max Element 9 7 6 8 2 6 5 1 7 7 7 After max element is removed.

Removing The Max Element 9 7 6 8 2 6 5 1 7 7 7 Heap with 9 nodes.

Removing The Max Element 9 7 6 8 2 6 5 1 Reinsert 7.

Removing The Max Element 9 7 6 8 2 6 5 1 Reinsert 7.

Removing The Max Element 9 8 7 6 7 2 6 5 1 Reinsert 7.

Complexity Of Remove Max Element 6 2 5 1 7 9 8 Complexity is O(log n).

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

Initializing A Max Heap 1 2 3 4 5 6 7 8 9 7 7 11 10 8 Start at rightmost array position that has a child. Index is n/2.

Initializing A Max Heap 1 2 3 4 11 6 7 8 9 10 7 7 5 8 Move to next lower array position.

Initializing A Max Heap 1 2 3 4 11 6 7 8 9 7 7 5 10 8

Initializing A Max Heap 1 2 3 9 11 6 7 8 4 7 7 5 10 8

Initializing A Max Heap 1 2 3 9 11 6 7 8 4 7 7 5 10 8

Initializing A Max Heap 1 2 7 9 11 6 3 8 4 7 7 5 10 8

Initializing A Max Heap 1 2 7 9 11 6 3 8 4 7 7 5 10 8

Initializing A Max Heap 1 11 7 9 6 3 8 4 10 7 7 5 8 Find a home for 2.

Initializing A Max Heap 1 11 7 9 10 6 3 8 4 8 7 7 5 Find a home for 2.

Initializing A Max Heap 1 11 7 9 10 6 3 8 4 7 7 2 8 5 Done, move to next lower array position.

Initializing A Max Heap 1 11 7 9 10 6 3 8 4 7 7 2 5 8 Find home for 1.

Initializing A Max Heap 11 7 9 10 6 3 8 4 7 7 2 8 5 Find home for 1.

Initializing A Max Heap 11 10 7 9 6 3 8 4 7 7 2 5 8 Find home for 1.

Initializing A Max Heap 11 10 7 9 5 6 3 8 4 7 7 2 8 Find home for 1.

Initializing A Max Heap 11 10 7 9 5 6 3 8 4 7 7 2 1 8 Done.

Time Complexity 11 9 7 8 5 6 3 1 4 10 7 7 2 8 Height of heap = h. Number of subtrees with root at level j is <= 2 j-1. Time for each subtree is O(h-j+1).

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

Binary Search Trees Dictionary Operations: IsEmpty() Get(key) Insert(key, value) Delete(key)

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.

Example Binary Search Tree 20 10 40 6 15 30 25 2 8 Only keys are shown.

The Operation Ascend() 20 10 6 2 8 15 40 30 25 Ascend is not a standard dictionary operation. Do an inorder traversal. O(n) time.

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.

The Operation Insert() 20 10 6 2 8 15 40 30 25 35 Insert a pair whose key is 35.

The Operation Insert() 20 10 6 2 8 15 40 30 25 35 7 Insert a pair whose key is 7.

The Operation Insert() 20 10 40 6 15 30 18 25 35 2 8 7 Insert a pair whose key is 18.

The Operation Insert() 20 10 40 6 15 30 18 25 35 2 8 7 Complexity of Insert() is O(height).

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. Case 1, either do nothing or report an error.

Delete From A Leaf 20 10 6 2 8 15 40 30 25 35 7 18 Delete a leaf element. key = 7

Delete From A Leaf (contd.) 20 10 6 2 8 15 40 30 25 35 7 18 Delete a leaf element. key = 35

Delete From A Degree 1 Node 20 10 6 2 8 15 40 30 25 35 7 18 Delete from a degree 1 node. key = 40

Delete From A Degree 1 Node (contd.) 20 10 6 2 8 15 40 30 25 35 7 18 Delete from a degree 1 node. key = 15

Delete From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Delete from a degree 2 node. key = 10

Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

Delete From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

Delete From A Degree 2 Node 20 8 40 6 15 30 18 25 35 2 8 7 Largest key must be in a leaf or degree 1 node.

Another Delete From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Delete from a degree 2 node. key = 20

Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

Delete From A Degree 2 Node 20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

Delete From A Degree 2 Node 18 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

Delete From A Degree 2 Node 18 10 40 6 15 30 25 35 2 8 7 Complexity is O(height).

Exercises Page 204, Exercise 3 Page 204, Exercise 4