Question

Write a program to transform a free tree to a rooted tree. The free tree and...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

}

}

Sample Output: E: IJavaljdk1.6.0_17\bin>javac NodeTree.java E:\Java\jdk1. 6.0 17\bin>j avac TransTree. java E:\Java\jdk1.6.0_

If any queries please get back to me

Thank You

Add a comment
Know the answer?
Add Answer to:
Write a program to transform a free tree to a rooted tree. The free tree and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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 th...

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

  • Write a program in a mall that will enable customers to reach the store they want to go to in the shortest way.

    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 C++ program that implements a binary search tree (BST) to manage a number of...

    Write a C++ program that implements a binary search tree (BST) to manage a number of integer items with different priorities. In order to do so, you will need to maintain a queue as an item in the tree. Each queue item is an integer. All items in a given queue will have the same priority. As a new item with a user-specified priority comes in, it should be enqueued in the queue with items of that priority. A new...

  • Write a program in Java that will accept input and display a person's family tree. This program n...

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

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

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

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

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

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

  • In BlueJ using Java, write a program that can input and display a person’s family tree....

    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.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT