Question

In: Computer Science

Write a simple Java code to make a Binary Tree for the following tree: Don’t use...

Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation

/** Class to encapsulate a tree node. */

protected static class Node implements Serializable {

Solutions

Expert Solution

Note:- Since No binary tree is given in the question, so I assume the binary tree and nodes as given below

Code:-

class TreeNode{
public char data;
public TreeNode left;
public TreeNode right;
  
public TreeNode(char data) {
this.data = data;
left = null;
right = null;
}
}

public class Main {
public static void inorder(TreeNode root) {
if(root == null)return;
inorder(root.left);
System.out.print(root.data + " ");
inorder(root.right);
}
  
public static void preorder(TreeNode root) {
if(root == null)return;
System.out.print(root.data + " ");
preorder(root.left);
preorder(root.right);
}
  
public static void postorder(TreeNode root) {
if(root == null)return;
postorder(root.left);
postorder(root.right);
System.out.print(root.data + " ");
}
  
public static void main(String args[]) {
TreeNode root = new TreeNode('1');
root.left = new TreeNode('2');
root.right = new TreeNode('3');
root.left.left = new TreeNode('4');
root.left.right = new TreeNode('5');
root.right.left = new TreeNode('6');
root.right.right = new TreeNode('7');
  
System.out.print("This is preorder traversal => ");
preorder(root);
System.out.println("\n+++++++++++++++");
System.out.print("This is postorder traversal => ");
postorder(root);
System.out.println("\n+++++++++++++++");
System.out.print("This is inorder traversal => ");
inorder(root);
}
  
}

  

Output:-

Thank You....!!!!
For any query, please comment.
If you are satisfied with the above answer, then please thumbs up or upvote the answer.


Related Solutions

CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a...
CODE IN JAVA** I(a). Given a pointer to the root of a binary tree write a routine that will mark (use a negative number like -999 for the info field) every node in the tree that currently has only a left son. You can assume the root of the tree has both a right and left son. When finished tell me how many nodes had only a left son as well as how many nodes are in the tree in...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
Make the following assumptions: Assume a binary search tree holds integer numbers Write a pseudocode for...
Make the following assumptions: Assume a binary search tree holds integer numbers Write a pseudocode for a binary tree search algorithm that searches in the tree (starting at root) for a node which meets the following requirements, and prints a message when found: (a) has a value that is one third as large as either its left or right child node. Think carefully about what steps are needed to do the search, and review the insert and search methods for...
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT