Question

Let T be a complete binary tree such that node v stores the entry (p(v), 0),...

Let T be a complete binary tree such that node v stores the entry (p(v), 0), where p(v) is the level number of v. Is tree T a heap? Why or why not?

I know that a complete binary tree is a heap, but shouldn't we also take into consideration the values that it is storing into the tree: (p(v), 0)? The heap tree could be either a min-heap or max-heap. If we order the the value based of p(v) which is the level number of v, then it would result in a min-heap since the higher numbers are at the bottom. However, if we order the value based of the 0, then this could be a max-heap, where the highest value is at the top. I guess in both cases it would result in a heap tree, but I'm wonder if we can store 2 numbers into the heap tree because most of the examples I see in the book have a variable and a number (A,5) to make it clear that the order of the tree is based of the number.

Homework Answers

Answer #1

Your query seems to be about what the nodes of the heap may contain. To know that you have to first know what a heap is used for. Usually there are two main puposes of a heap:

  1. In heap sort. Since in a heap, we have a specific order of the values in the node (children have either lower values or higher values than their parent depending on if we have max heap or min heap). This information can be used to keep extracting the root of the heap and fixing the heap (to replace the extracted root). The result is that we get the elements in a sorted order.
  2. As a priority queue. In this case, we order the nodes of the heap by priority. For example if every name that begins with M has higher priority than A which in turn has higher priority than T, then the root should have M, the child should have A and the subsequent child has T.

Obviously if only numbers are given and nothing is mentioned about priority order, then we assume that the max heap will have the highest number at the top.

Now think about the use of a priority queue. We have each node containing some amount of information and priority assigned to it. The highest priority is at the top. Children node have lower priority than parent node. Is there any real use of a "priority number/order" if there is no other information associated with the node? No. Rather we associate "priority number" to other information.

In fact, the following are also valid nodes for construction of a heap:

Names of the students in your class prioritized by their roll numbers.

Notice that the nodes above have a lot more information than just two fields.

The priority order doesn't need to be a number. It can be any entity as long as we can establish a order for it. As long as we know that entityX is lower than entityY, we can estasblish a priority order and create the heap.

In the example you gave in the question, we can assume that the priority order of the nodes of the tree can be established by "level number" and it will become a heap. You can also prioritize the nodes by the second field "0" and call it a heap.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider a binary search tree where each tree node v has a field v.sum which stores...
Consider a binary search tree where each tree node v has a field v.sum which stores the sum of all the keys in the subtree rooted at v. We wish to add an operation SumLE(K) to this binary search tree which returns the sum of all the keys in the tree whose values are less than or equal to K. (a) Describe an algorithm, SumLE(K), which returns the sum of all the keys in the tree whose values are less...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Input: A binary tree T in which each node x contains a field worth[x], which is...
Input: A binary tree T in which each node x contains a field worth[x], which is a (positive, zero, or negative) monetary value expressed as a real number. Define (monetary) heritage of a node x to be the total worth of ancestors of x minus the total worth of proper descendants of x. Output: A node x in T with maximum heritage. Note: other than the field worth and left/right child pointers, nodes of the given tree do not have...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
1. Complete the PDF. x P(X = x) x · P(X = x) 0 0.1 1...
1. Complete the PDF. x P(X = x) x · P(X = x) 0 0.1 1 0.4 2 3 0.2 Part (a) Find the probability that X = 2. Part (b) Find the expected value. 2. A school newspaper reporter decides to randomly survey 16 students to see if they will attend Tet (Vietnamese New Year) festivities this year. Based on past years, she knows that 21% of students attend Tet festivities. We are interested in the number of students...
Complete the following activities and then post your responses in the Activity #5 discussion forum (link...
Complete the following activities and then post your responses in the Activity #5 discussion forum (link below). Consider a short multiple choice quiz with three items, and each item has four choices (only one of the choices is correct). Suppose that you are taking this quiz but you are completely and utterly unprepared for it. That means that you only option for the quiz is to guess the answers. Suppose you are thinking about the first item: what's the probability...
Question 3 (a) [10 marks] FAEN102 students must attend t hours, where t ∈ [0,H], of...
Question 3 (a) [10 marks] FAEN102 students must attend t hours, where t ∈ [0,H], of lectures and pass two quizzes to be in good standing for the end of semester examination. The number of students who attended between t1 and t2 hours of lectures is de- scribed by the integral ? t2 20t2 dt, 0≤t1 <t2 ≤H. t1 As a result of COVID-19, some students attended less than H2 hours of lectures before the university was closed down and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT