Answer:
I am writing a Python function:
def is_maxheap(list):
#finds the length of the list
size=len(list)
#loop has a variable i which starts traversing from root til end of last #internal node
for i in range(int((size - 2) / 2) + 1):
if list[2 * i + 1] > list[i]: #If left child is greater than its parent then return #false
return False
if (2 * i + 2 < size and
list[2 * i + 2] > list[i]): #checks if right child is greater, if yes then #returns false
return False
return True
Explanation:
The function is_maxheap() traverses through all the internal nodes of the list iteratively. While traversing it checks if the node is greater than its children or not. To check the working of this function you can write a main() function to check the working on a given list. The main() function has a list of integers. It then calls is_maxheap() method by passing that list to the function. The program displays a message: "valid max heap" if the list represents valid max-heap otherwise it returns invalid max heap.
def main():
list = [10,7,8]
size = len(list)
if is_maxheap(list):
print("Valid Max Heap")
else:
print("Invalid Max Heap")
main()
The program along with its output is attached in a screenshot.
Answer:
The code is in java.
Explanation:
import java.util.Scanner;
class Main{
public static void main(String[] args) {
System.out.println("Enter 3 different numbers");
Scanner obj = new Scanner(System.in);
int a = obj.nextInt();
int b = obj.nextInt();
int c = obj.nextInt();
System.out.println("The middle element is : ");
//Check if a is the middle
if(a>b&&a<c || a>c&&a<b){
System.out.println(a);
}
//Check if b is the middle element
else if(b>a&&b<c || b<a&&b>c){
System.out.println(b);
}
else{
System.out.println(c);
}
}
}
OUTPUT:-
Record companies lose money because people download songs for free.
Answer:
The node of the 2-4 tree can have at most three values.
There are basically 3 types of nodes in the 2-4 tree which are as following:-
- 2-Node.
- 3-Node.
- 4-Node.
2-Node:-It contains 1 data element and two child pointers.
3-Node:-It has 2 data elements and three child pointers.
4-Node:-It has 3 data elements and four child pointers.
There is also leaf node which can contain 2,3 or 4 data elements but has no child pointers.