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...
Please help me with me. I did the first part to write the operations but in the driver its shows me errors, can't fix it. Help me. Need help with this to run the application. /** * BinaryTreeNode represents a node in a binary tree with a left and * right child. * * @author Java Foundations * @version 4.0 */ public class BinaryTreeNode { protected T element; protected BinaryTreeNode left, right; /** * Creates a new tree node with...
Please implement a right rotation funtion: private Node rightRotate(Node root) { } Remember to return the new root of the subtree to the parent so the parent can set it to be its child package trees; public class BinaryTree> { private Node root; //private int size; public static int sumTree(Node root) { if(root== null) { return 0; } int center = 0; if( root.item % 2 == 0) { center = root.item; } int left = sumTree(root.left); int right =...
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...
Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{ BSTTreeNode left, right; String data; public BSTTreeNode(){ left = null; right = null; data = null; } public BSTTreeNode(String n){ left = null; right = null; data = n; } public void setLeft(BSTTreeNode n){ left = n; } public void setRight(BSTTreeNode n){ ...
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...
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; }...