Can you take a look at my code that why the maxDepth function is not working?
public class BinaryTree {
class Node{
int key;
Node left,right;
public Node(int item) {
key =
item;
left = right =
null;
}
}
Node root;
public void BinaryTree(){
root = null;
}
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
if(root == null) {
root = new
Node(key);
return
root;
}
if(key < root.key)
root.left =
insertRec(root.left, key);
if(key > root.key)
root.right =
insertRec(root.right, key);
return root;
}
void inorder() {
inorderRec(root);
}
void inorderRec(Node node) {
if(node != null) {
inorderRec(node.left);
System.out.println(node.key + " ");
inorderRec(node.right);
}
}
void preorder() {
preorderRec(root);
}
void preorderRec(Node node) {
if(node != null) {
System.out.println(node.key);
inorderRec(node.left);
inorderRec(node.right);
}
}
void postorder() {
postorderRec(root);
}
void postorderRec(Node node) {
if(node != null) {
postorderRec(node.left);
postorderRec(node.right);
System.out.println(node.key);
}
}
int maxDepth() {
return(maxDepth(root));
}
int maxDepth(Node node) {
if(node == null) {
return 0;
}
else {
int lDepth =
maxDepth(node.left);
int rDepth =
maxDepth(node.right);
return
(Math.max(lDepth, rDepth)+1);
}
}
public static void main(String[] args) {
BinaryTree tree = new
BinaryTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
// System.out.println("Inorder traversal of binary tree is
");
// tree.inorder();
// System.out.println("Postorder traversal of binary tree is
");
// tree.postorder();
// System.out.println("Preorder traversal of binary tree is
");
// tree.preorder();
// System.out.println("Height of binary tree is ");
tree.maxDepth(tree.root);
}
}
// TEXT CODE
// The modified code is highlighted in bold
// The problem in your code was that you were simply
calling the maxDepth() method but not printing the
// the value returned by the maxDepth() method
import java.util.*;
public class BinaryTree {
class Node{
int key;
Node left,right;
public Node(int item) {
key = item;
left = right = null;
}
}
Node root;
public void BinaryTree(){
root = null;
}
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
if(root == null) {
root = new Node(key);
return root;
}
if(key < root.key)
root.left = insertRec(root.left, key);
if(key > root.key)
root.right = insertRec(root.right, key);
return root;
}
void inorder() {
inorderRec(root);
}
void inorderRec(Node node) {
if(node != null) {
inorderRec(node.left);
System.out.println(node.key + " ");
inorderRec(node.right);
}
}
void preorder() {
preorderRec(root);
}
void preorderRec(Node node) {
if(node != null) {
System.out.println(node.key);
inorderRec(node.left);
inorderRec(node.right);
}
}
void postorder() {
postorderRec(root);
}
void postorderRec(Node node) {
if(node != null) {
postorderRec(node.left);
postorderRec(node.right);
System.out.println(node.key);
}
}
int maxDepth() {
return(maxDepth(root));
}
int maxDepth(Node node) {
if(node == null) {
return 0;
}
else {
int lDepth = maxDepth(node.left);
int rDepth = maxDepth(node.right);
return (Math.max(lDepth, rDepth)+1);
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
System.out.println("Inorder traversal of binary tree is ");
tree.inorder();
System.out.println("Postorder traversal of binary tree is ");
tree.postorder();
System.out.println("Preorder traversal of binary tree is ");
tree.preorder();
System.out.println("Height of binary tree is: " +
tree.maxDepth());
}
}
// OUTPUT:
Inorder traversal of binary tree is 20 30 40 50 60 70 80 Postorder traversal of binary tree is 20 40 30 60 80 70 50 Preorder traversal of binary tree is 50 20 30 40 60 70 80 Height of binary tree is: 3
Can you take a look at my code that why the maxDepth function is not working?...
using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...
Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...
Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method: InOrder(Node theRoot), PreOrder(Node theRoot), PostOrder(Node theRoot), FindMin(), FindMax(), Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...
In Java. How would this method look? LinkedBinaryTree.java import java.util.Iterator; public class LinkedBinaryTree implements BinaryTreeADT { private BinaryTreeNode root; /** * Creates an empty binary tree. */ public LinkedBinaryTree() { root = null; } /** * Creates a binary tree from an existing root. */ public LinkedBinaryTree(BinaryTreeNode root) { this.root = root; } /** * Creates a binary tree with the specified element...
Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None def search(self, find_data): if self.data == find_data: return self elif find_data < self.data and self.left != None: return self.left.search(find_data) elif find_data > self.data and self.right != None: return self.right.search(find_data) else: return None def get_left(self): return self.left def get_right(self): return self.right def set_left(self, tree): self.left = tree def set_right(self, tree): self.right = tree def set_data(self, data): self.data = data def get_data(self): return self.data def traverse(root,order):...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
Hello, I've been working on this for a while and I can't figure the rest out. Need asap if anyone can help. maxBSt,minBST,isBST, and inOrder package lab7; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class TreeExercise { /* * Construct BST from preorder traversal */ public static Node<Integer> consBSTfromPreOrder(int[] arr, int start, int end) { if(start > end) return null; Node<Integer> root = new Node<Integer>(arr[start],...
JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....