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:...
The one missing piece was inserting into a binary search tree; we'll take care of that...
The one missing piece was inserting into a binary search tree; we'll take care of that today and write the insert function, as well as a height function. Both functions will be implemented in the "bst.h" header file, and tested using a provided main program in "main.cpp". Step one is to implement the insert function --- go ahead and modify "bst.h", adding the necessary code to (1) allocate a new node, and (b) link it into the tree. Once you...
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 {...
I have written working code for this problem however it is not producing an output and...
I have written working code for this problem however it is not producing an output and it states is a wrong answer. Please just fix my code below and make it work for the sample test cases and all test cases. You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the...
This is an intro to Java question. Please include pseudo code for better understanding. I am...
This is an intro to Java question. Please include pseudo code for better understanding. I am hoping to use this to do some reviewing. We are mainly focusing on input and output declarations and invoking API methods. Problem 6: Log It (10 points) Use API (Data Structure Algorithms) High-Low is a simple number guessing game where one player thinks of a random integer number between 0 to some maximum value and another player attempts to guess that number. With each...