Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello i have to implement binary tree (inorder) using java code here is my code.

ID: 3550599 • Letter: H

Question

Hello i have to implement binary tree (inorder) using java code


here is my code.


but i got too much error and why i just start to learn java.


could yoy explain and change my errors?


package binTree;

import java.util.Random;




public class Binary

{

private Binary leftNode,rightNode;

private int key;

public Binary(int key)

{

this.key=key;

}

}




public class BinTree{

public static void main(String[] args)

{

BinTree tree= new BinTree();

int value;

Random randomNumber = new Random();

System.out.println("Building binary tree...");

for(int i=1; i<=10;i++)

{

value= randomNumber.nextInt(100);

System.out.printf("%d", value);

tree.insertNode(value);

}

System.out.println(" Inorder traveral");

tree.treeWalk();

}

public void insertNode(int value)

{

if(key > value)

{

if(leftNode !=null)

{

leftNode.insertNode(value);

}

else

{

leftNode= new Binary(value);

}

}

if(key<value)

{

if(rightNode != null)

{

rightNode.insertNode(value);

}

else

{

rightNode= new Binary(value);

}

}

}

public void treeWalk()

{

if(leftNode != null)

leftNode.treeWalk();

System.out.println(key + "");

if(rightNode !=null)

rightNode.treeWalk();

}

}

Explanation / Answer


Here is the corrct implementation of Binary tree in Java

In insertNode() you are not passing the starting node of the binary to compare.


If the value is less than the Node value put it left elase put it right to the Node if there is no existing child exist.




================================================


package com.Project.com.Project.Chegg;


public class Binarytree {

public static void main(String[] args) {

new Binarytree().run();

}

static class Node {

Node left;

Node right;

int value;

public Node(int value) {

this.value = value;

}

}

public void run() {

Node root = new Node(5);

System.out.println("Binary Tree Example");

System.out.println("Building tree with root value " + root.value);

insert(root, 1);

insert(root, 8);

insert(root, 6);

insert(root, 3);

insert(root, 9);

System.out.println("Traversing tree in order");

printInOrder(root);

}

public void insert(Node node, int value) {

if (value < node.value) {

if (node.left != null) {

insert(node.left, value);

} else {

System.out.println(" Inserted " + value + " to left of "

+ node.value);

node.left = new Node(value);

}

} else if (value > node.value) {

if (node.right != null) {

insert(node.right, value);

} else {

System.out.println(" Inserted " + value + " to right of "

+ node.value);

node.right = new Node(value);

}

}

}

public void printInOrder(Node node) {

if (node != null) {

printInOrder(node.left);

System.out.println(" Traversed " + node.value);

printInOrder(node.right);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote