Write a program to transform a free tree to a rooted tree. The free tree and the root are input by the user, and for the rooted tree, you output it level by level. (Hint: Use Adjacency Matrix or Adjacency Lists to store the tree, and use a queue to implement the transformation.)
code:
NodeTree.java
class NodeTree
{
int DataVal;
NodeTree leftNode, rightNode;
public NodeTree(int itemValue)
{
DataVal = itemValue;
leftNode = rightNode = null;
}
}
TransTree.java:
class TransTree
{
NodeTree rootNode;
public TransTree()
{
rootNode = null;
}
void printLevelOrderTree()
{
int hVal = DefineHeight(rootNode);
int iter;
for (iter=1; iter<=hVal; iter++)
printGivenLevelTree(rootNode, iter);
}
int DefineHeight(NodeTree rootNode)
{
if (rootNode == null)
return 0;
{
int LeftHeight = DefineHeight(rootNode.leftNode);
int RightHeight = DefineHeight(rootNode.rightNode);
if (LeftHeight > RightHeight)
return(LeftHeight+1);
return(RightHeight+1);
}
}
void printGivenLevelTree (NodeTree rootNode ,int Levels)
{
if (rootNode == null)
return;
if (Levels == 1)
System.out.print(rootNode.DataVal + " ");
else if (Levels > 1)
{
printGivenLevelTree(rootNode.leftNode, Levels-1);
printGivenLevelTree(rootNode.rightNode, Levels-1);
}
}
public static void main(String args[])
{
TransTree CreateFreetree = new TransTree();
CreateFreetree.rootNode= new NodeTree(1);
CreateFreetree.rootNode.leftNode= new NodeTree(2);
CreateFreetree.rootNode.rightNode= new NodeTree(3);
CreateFreetree.rootNode.leftNode.leftNode= new NodeTree(4);
CreateFreetree.rootNode.leftNode.rightNode= new NodeTree(5);
System.out.println("Level order traversal of binary tree is ");
]
CreateFreetree.printLevelOrderTree();
}
}
If any queries please get back to me
Thank You
Write a program to transform a free tree to a rooted tree. The free tree and...
Reverse of a digraph 30 Marks For a given set of digraphs, write a program that prints out the reverse of each digraph. Input format: described below under the heading “Digraph input format”. Note that adjacency lists are sorted. Output format: use the input format to output your result ensuring that the output adjacency lists are sorted. the input format: 4 1. 3 2. 3 0 3 1. 2 1 0 2. Reverse of a digraph 30 Marks For a...
Part A (70 points) :Write a program in a mall that will enable customers to reach the store they want to go to in theshortest way. The coordinates of the some important stores in the mall are given below : Cinema: (256.6, 92.3) Sport Center: (120.5, 5.5) Restaurant: (350.0, 165.5) Cafe: (102.3, 10.26) Bowling: (40.8, 72.35) Clothes Store: (92.5, 86.4) Toy Store: (90.5, 302.1)Roads connect these locations as below:Program stores the road network as an adjacency matrix. Locations will be...
Write a program in Java that will accept input and display a person's family tree. This program needs to have a root person that branches off to parents and grandparents and be made so that you can add people.
Battle Ship Game Write a Java program from scratch. Simple geometrical reasoning is at the core of Battleships. One important notion is adjacency, with two sub-types: edge adjacency and corner adjacency. For example, the square (4,3) is edge-adjacent to the square (3,3), it is corner-adjacent to the square (3,2), and it is not adjacent to the square (6,7). Write a program that reads a square from the user, and prints out three lists(each with a seperate method): 1) a list...
Using Java- Write a program that takes an arithmetic expression in an infix form, converts it to a postfix form and then evaluates it. Use linked lists for the infix and postfix queues and the operator and value stacks. You must use sperate Stack and Queue classes with a driver class to run the program. example Input : 111++ Output : (1+ (1+ 1)) Answer: 3
I need help with the C++ for the following problem: Write a program that can store and output 5 integers, where the user can input its value into an array named arrayValue. The program should implement the following: 1. Calculate the sum and average of the array. 2. Output the content (value) that stored in the array of five elements. 3. Continue to ask the user to store for storing data into another array.
Please use python to write the simple program. Exercise 2 - Summation Write a program to accept integer inputs until the user inputs a non-integer. Then, the program prints the summation of the inputted numbers. Hint: Use the function input() to get the user input. Use a variable total for calculating the summation. Need to use while loop for the repeated inputs. Use if statement with isdigit() function to check whether the user input is an integer or not. if...
It is C++ problems, please explain your each line of code as well. Task 1: Implement/ Hard Code a Doubly Linked Lists and make it a Stack. Do not use ::list:: class from the STD library for this example. The user will input a string and the program will output the string backwards. Displaying correct Stack implementation. Utilize the conventional static methods as needed. push() pop() empty() peek() peek() Sample Execution Please input String: happy yppah Task 2: Implement /...
Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...
In BlueJ using Java, write a program that can input and display a person’s family tree. This project requires a GUI class that allows the user to input the members of the family tree and print the family tree.