Sol: I implemented a binary tree for my asignment and I have attached the code from the same:
class Node {
int value;
Node left;
Node right;
Node(int value) {
this.value = value;
right = null;
left = null;
}
}
public class BinaryTree {
Node root;
private Node addRecursive(Node current, int value) {
if (current == null) {
return new Node(value);
}
if (value < current.value) {
current.left = addRecursive(current.left, value);
} else if (value > current.value) {
current.right = addRecursive(current.right, value);
} else {
// value already exists
return current;
}
return current;
}
public void add(int value) {
root = addRecursive(root, value);
}
private BinaryTree createBinaryTree() {
BinaryTree bt = new BinaryTree();
bt.add(6);
bt.add(4);
bt.add(8);
bt.add(3);
bt.add(5);
bt.add(7);
bt.add(9);
return bt;
}
private boolean containsNodeRecursive(Node current, int value) {
if (current == null) {
return false;
}
if (value == current.value) {
return true;
}
return value < current.value
? containsNodeRecursive(current.left, value)
: containsNodeRecursive(current.right, value);
}
public boolean containsNode(int value) {
return containsNodeRecursive(root, value);
}
//1. Post order traversal
public void traversePostOrder(Node node) {
if (node != null) {
traversePostOrder(node.left);
traversePostOrder(node.right);
System.out.print(" " + node.value);
}
}
// The output will be : 3 5 4 7 9 8 6
//2. In order
public void traverseInOrder(Node node) {
if (node != null) {
traverseInOrder(node.left);
System.out.print(" " + node.value);
traverseInOrder(node.right);
}
}
//Output will be 3 4 5 6 7 8 9
//3. Pre order
public void traversePreOrder(Node node) {
if (node != null) {
System.out.print(" " + node.value);
traversePreOrder(node.left);
traversePreOrder(node.right);
}
}
//Output will be 6 4 3 5 8 7 9
//4. depth
int mDepth(Node node)
{
if (node == null)
return 0;
else
{
int lDepth = mDepth(node.left);
int rDepth = mDepth(node.right);
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
}
Create graph binary tree with java code Then the output - post order - in order...
Needed in Java Code Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 1.Print the Pre-order of this tree 2. Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three...
Draw the binary tree whose in-order and post-order traversals of the nodes are: a. In-order: ASO FUZDBPQ Post-order: A OSFDZP BQU b. In-order: ASO FUZDBPQ Pre-order: ZSA FOUPDBQ
Describe a binary tree T for which the pre-order and post-order traversals result in precisely the same ordering of T’s nodes. (That is, pre-order-traverse(T) = post-order-traverse(T).)
(Pre-order and post-order traversal). Implement the textbook's
algorithm (not the Java implementation) for
pre-order and post-order traversal. To "process" or "visit" a node
means to print the data at that node.
Pre-order traversal output: 10 6 4 8 18 15 21
Post-order traversal output: 4 8 6 15 21 18 10
Additionally, create and traverse the following trees:
Algorithm preorder(p) perform the "visit" action for position p this happens before any recursion for each child c in children(p) do recursively...
Implement a method (in java) to print all the element of a binary search tree in pre-order traversal order. (CANNOT TAKE A PARAMETER) Thank you! public void printTree() { //chegg code here only! }
Trees Traversals Please write a Java program to traverse a binary tree in in-order and pre-order, and to plot a binary tree into a 2-dimensional array. You may write many recursive methods for this project. You are not allowed to use any existing Java classes such as ArrayList or Vector or Tree. Please stop your program if the user enters 0 as the tree selection. Your program must run the following Test Case 1 plus two more test cases to...
The in-order traversal of a binary tree processes the nodes in the order ABCD. The post-order traversal of the same binary tree processes the nodes in the order ACDB. Draw the tree below.
Using Java. Create a binary tree given a set of numbers (e.g., list of numbers separated by spaces). If is not balanced Code Double rotation.
Question 6 (1 point) Given a binary tree. The pre-order of this tree is: acefbd. The in-order of the tree is: cfeabd What is the post order of this tree?
Generate a binary search tree for following numbers and perform in-order and post-order traversals: 50, 40, 80, 20, 0, 30, 10, 90, 60, 70 (JAVA)