I have been learning java recently. And I created two trees. I need to write code(equals method) to compare two trees and if they are the same, then output true or false.
My Code:
public class TreePrint {
public static void main(String[] args) {
Tree<String> rootFolder = new Tree<>("RootFolder");
Node<String> video = rootFolder.addChild("Video");
Node<String> music = rootFolder.addChild("Music");
Node<String> picture = rootFolder.addChild("Picture");
video.addChild("Terminator");
video.addChild("Die Hard");
video.addChild("Rocky");
music.addChild("Eminem");
Node<String> picture01 = picture.addChild("Picasso");
picture01.addChild("Do Vinci");
Node<String> picture02 = picture01.addChild("Adil");
picture02.addChild("Cartoon");
picture02.addChild("Comics");
Tree2<String> rootFolder1 = new Tree2<>("RootFolder1");
Node<String> video1 = rootFolder1.addChild("Video");
Node<String> music1 = rootFolder1.addChild("Music");
Node<String> picture1 = rootFolder1.addChild("Picture");
video1.addChild("Terminator");
video1.addChild("Die Hard");
video1.addChild("Rocky");
music1.addChild("Eminem");
Node<String> picture001 = picture1.addChild("Picasso");
picture001.addChild("Do Vinci");
Node<String> picture002 = picture001.addChild("Adil");
picture002.addChild("Cartoon");
picture002.addChild("Comics");
printTree(rootFolder);
printTree(rootFolder1);
boolean b1 = rootFolder.contains("P0");
System.out.println(b1);
boolean b2 = rootFolder1.contains("Eminem");
System.out.println(b2);
}
private static <T> void printTree(Node<T> node) {
printTree(node, 0);
}
private static <T> void printTree(Node<T> node, int level) {
printNode(node, level);
if (node.getChildren() != null) {
for (Node childNode : node.getChildren()) {
printTree(childNode, level + 1);
}
}
}
private static <T> void printNode(Node<T> kid, int level) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(kid.getData());
}
}
first tree:
public class Tree<T> extends Node<T> {
public Tree(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}
public class Node<T> {
private T data;
private final List<Node<T>> children = new ArrayList<>();
private final Node<T> parent;
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
}
public void addChild(Node<T> node) {
children.add(node);
}
public Node<T> addChild(T nodeData) {
Node<T> newNode = new Node<T>( nodeData, this );
children.add( newNode );
return newNode;
}
public List<Node<T>> iterate() {
return children;
}
public void remove(Node<T> node) {
children.remove(node);
}
public List<Node<T>> getChildren() {
return children;
}
public Node getParent() {
return parent;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
Second Tree:
public class Tree2<T> extends Node<T> {
public Tree2(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}
Add the following method to your Node class. Since your tree is also a node - you should be able to compare two trees. FYI, This is autogenerated by Eclipse.
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (children == null) {
if (other.children != null)
return false;
} else if (!children.equals(other.children))
return false;
if (data == null) {
if (other.data != null)
return false;
} else if (!data.equals(other.data))
return false;
return true;
}
/* Given two trees, return true if they are
structurally identical */
boolean identicalTrees(Node a, Node b)
{
/*1. both empty */
if (a == null && b == null)
return true;
/* 2. both non-empty -> compare them */
if (a != null && b != null)
return (a.data == b.data
&& identicalTrees(a.left, b.left)
&& identicalTrees(a.right, b.right));
/* 3. one empty, one not -> false */
return false;
}
then just use this method for the root nodes of your two trees.
Related
While revising for my final, I came across binary trees (regular ones). After going through the lecture, I started to solve previous labs.
My problem here is that only 6 is inserted into the tree. What is wrong with my method?
What I'm trying to input:
tree.insert(1); tree.insert(15); tree.insert(7);
tree.insert(13); tree.insert(58); tree.insert(6);
The Node class:
public class Node {
public int key;
public Node left_child;
public Node right_child;
// The Constructor(s).
public Node() {
}
public Node(int key) {
this.key = key;
left_child = null;
right_child = null;
}
// The Get Methods.
public int getKey() {
return key;
}
public Node getLeft_child() {
return left_child;
}
public Node getRight_child() {
return right_child;
}
// The Set Methods.
public void setKey(int key) {
this.key = key;
}
public void setLeft_child(Node left_child) {
this.left_child = left_child;
}
public void setRight_child(Node right_child) {
this.right_child = right_child;
}
}
The BinaryTree class:
public class BinaryTree {
private Node root;
// The Constructor Method(s)
public BinaryTree() {
root = null;
}
public boolean is_empty() {
return (root == null);
}
public void insert(int k) {
insert(root, k);
}
private Node insert(Node x, int k) {
Node p = new Node(k);
if (x == null) {
root = p;
}
else {
if (x.left_child != null) {
insert(x.left_child, k);
}
else {
insert(x.right_child, k);
}
}
return x;
}
// Printing Method(s).
public void print_inorder() {
print_inorder(root);
}
public void print_inorder(Node node) {
if (node == null) {
return;
}
print_inorder(node.left_child);
System.out.print(node.key + " ");
print_inorder(node.right_child);
}
public void print_preorder() {
print_preorder(root);
}
public void print_preorder(Node node) {
if (node == null) {
return;
}
System.out.print(node.key + " ");
print_preorder(node.left_child);
print_preorder(node.right_child);
}
public void print_postorder() {
print_postorder(root);
}
public void print_postorder(Node node) {
if (node == null) {
return;
}
print_postorder(node.left_child);
print_postorder(node.right_child);
System.out.print(node.key + " ");
}
}
This will have a node to the left if a number is even and a node to the right if a node is odd.
I had to change your if condition because the way it was it was never going to use the left node.
private void insert(Node x, int k) {
Node p = new Node(k);
if (root == null) {
this.root = p;
return;
}
if (x.getKey() - k % 2 == 0) {
if (x.left_child == null) {
x.left_child = p;
} else {
insert(x.left_child, k);
}
} else {
if (x.right_child == null) {
x.right_child = p;
} else {
insert(x.right_child, k);
}
}
}
When I use insert() to inset new nodes into the binary tree it only inserts the new nodes on the place of child nodes even when root node already have left and right child. It is not visiting the child nodes to make deeper levels of binary tree.
Sorry for the bad English.
class Node
{
int key;
String value;
Node lc = null;
Node rc = null;
Node(int k,String v)
{
key = k;
value = v;
}
public String toString()
{
return value + "is" + key;
}
}
class BT
{
Node root;
public void insert(int k,String v)
{
Node newnode = new Node(k,v);
if(root == null)
{
System.out.println("root");
root = newnode;
return;
}
Node n = root;
while(n != null)
{
if(newnode.key <= n.key)
{
n = n.lc;
System.out.println("left");
if(n==null){n = newnode; break;}
}
else
{
n = n.rc;
System.out.println("right");
if(n==null){n = newnode; break;}
}
}
System.out.println("loop ended");
return;
}
}
public class test
{
public static void main(String arg[])
{
BT list = new BT();
list.insert(19,"one");
list.insert(67,"sixtyseven");
list.insert(5,"five");
list.insert(12,"twelve");
list.insert(67,"sixtyseven");
}
}
You never change the lc and rc links. Try something like this:
if(newnode.key <= n.key)
{
if(n.lc==null){n.lc = newnode; break;}
n = n.lc;
System.out.println("left");
}
else
{
if(n.rc==null){n.rc = newnode; break;}
n = n.rc;
System.out.println("right");
}
What about using recursion? It's much more clear to realise.
public final class BinaryTree {
private static final boolean ADD_TO_PARENT = true;
private static final boolean ALREADY_ADDED = false;
private Node root;
public void add(int key, String value) {
Node node = new Node(key, value);
if (add(node, root) == ADD_TO_PARENT)
root = node;
}
private static boolean add(Node node, Node parent) {
if (parent == null)
return ADD_TO_PARENT;
if (node.key <= parent.key) {
if (add(node, parent.left) == ADD_TO_PARENT)
parent.left = node;
} else if (add(node, parent.right) == ADD_TO_PARENT)
parent.right = node;
return ALREADY_ADDED;
}
public static final class Node {
private final int key;
private final String value;
private Node left;
private Node right;
public Node(int key, String value) {
this.key = key;
this.value = value;
}
#Override
public String toString() {
return value + " is " + key;
}
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Guys i created two trees. I wrote a method but it does not work correctly.
My method does not work recursively and prints only "false".
In general, I need to recurse through the elements of a tree, and If two trees are similar, then it should output - "true". If two rees not similar, then it should output - false. Please help me write code eqauls method in my trees
My code:
public class TreePrint {
public static void main(String[] args) {
Tree<String> rootFolder = new Tree<>("RootFolder");
Node<String> video = rootFolder.addChild("Video");
Node<String> music = rootFolder.addChild("Music");
Node<String> picture = rootFolder.addChild("Picture");
video.addChild("Terminator");
video.addChild("Die Hard");
video.addChild("Rocky");
music.addChild("Eminem");
Node<String> picture01 = picture.addChild("Picasso");
picture01.addChild("Do Vinci");
Node<String> picture02 = picture01.addChild("NN");
picture02.addChild("Cartoon");
picture02.addChild("Comics");
Tree2<String> rootFolder1 = new Tree2<>("RootFolder");
printTree(rootFolder);
printTree(rootFolder1);
boolean b1 = rootFolder.contains("P0");
//System.out.println(b1);
boolean b2 = rootFolder1.contains("Eminem");
//System.out.println(b2);
System.out.println(rootFolder.equals(rootFolder1));
}
private static <T> void printTree(Node<T> node) {
printTree(node, 0);
}
private static <T> void printTree(Node<T> node, int level) {
printNode(node, level);
if (node.getChildren() != null) {
for (Node childNode : node.getChildren()) {
printTree(childNode, level + 1);
}
}
}
private static <T> void printNode(Node<T> kid, int level) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(kid.getData());
}
}
public class Tree<T> extends Node<T> {
public Tree(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
public boolean equals(Object obj) {
return isEquals(obj);
}
private boolean isEquals(Object obj
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Node other = (Node) obj;
if (children == null) {
if (other.children != null) {
return false;
}
} else if (!children.equals(other.children)) {
return false;
}
if (data == null) {
if (other.data != null) {
return false;
}
} else if (!data.equals(other.data)) {
return false;
}
return true;
}
}
public class Node<T> {
private T data;
private final List<Node<T>> children = new ArrayList<>();
private final Node<T> parent;
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
}
public void addChild(Node<T> node) {
children.add(node);
}
public Node<T> addChild(T nodeData) {
Node<T> newNode = new Node<T>(nodeData, this);
children.add(newNode);
return newNode;
}
public List<Node<T>> iterate() {
return children;
}
public void remove(Node<T> node) {
children.remove(node);
}
public List<Node<T>> getChildren() {
return children;
}
public Node getParent() {
return parent;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
public class Tree2<T> extends Node<T> {
public Tree2(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}
The problem is is this line
if (getClass() != obj.getClass()) {
return false;
}
As one object is of class Tree and other is of Tree2
I have no idea why do you need two classes Tree and Tree2 but:
TreePoint
public class TreePoint {
public static void main(String[] args) {
Tree<String> rootFolder = new Tree<>("RootFolder");
Node<String> video = rootFolder.addChild("Video");
Node<String> music = rootFolder.addChild("Music");
Node<String> picture = rootFolder.addChild("Picture");
video.addChild("Terminator");
video.addChild("Die Hard");
video.addChild("Rocky");
music.addChild("Eminem");
Node<String> picture01 = picture.addChild("Picasso");
picture01.addChild("Do Vinci");
Node<String> picture02 = picture01.addChild("NN");
picture02.addChild("Cartoon");
picture02.addChild("Comics");
Tree2<String> rootFolder1 = new Tree2<>("RootFolder");
Node<String> video1 = rootFolder1.addChild("Video");
Node<String> music1 = rootFolder1.addChild("Music");
Node<String> picture1 = rootFolder1.addChild("Picture");
video1.addChild("Terminator");
video1.addChild("Die Hard");
video1.addChild("Rocky");
music1.addChild("Eminem");
Node<String> picture011 = picture1.addChild("Picasso");
picture011.addChild("Do Vinci");
Node<String> picture021 = picture011.addChild("NN");
picture021.addChild("Cartoon");
picture021.addChild("Comics");
printTree(rootFolder);
printTree(rootFolder1);
System.out.println(rootFolder.equals(rootFolder1));
}
private static <T> void printTree(Node<T> node) {
printTree(node, 0);
}
private static <T> void printTree(Node<T> node, int level) {
printNode(node, level);
if (node.getChildren() != null) {
for (Node childNode : node.getChildren()) {
printTree(childNode, level + 1);
}
}
}
private static <T> void printNode(Node<T> kid, int level) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(kid.getData());
}
}
Tree
import java.util.List;
public class Tree<T> extends Node<T> {
public Tree(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}
Node
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Node<T> {
public T data;
public final List<Node<T>> children = new ArrayList<>();
public final Node<T> parent;
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
}
public void addChild(Node<T> node) {
children.add(node);
}
public Node<T> addChild(T nodeData) {
Node<T> newNode = new Node<T>(nodeData, this);
children.add(newNode);
return newNode;
}
public List<Node<T>> iterate() {
return children;
}
public void remove(Node<T> node) {
children.remove(node);
}
public List<Node<T>> getChildren() {
return children;
}
public Node getParent() {
return parent;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
Node<?> node = (Node<?>) o;
return Objects.equals(data, node.data) &&
Objects.equals(children, node.children);
}
#Override
public int hashCode() {
return Objects.hash(data, children, parent);
}
}
Tree2
import java.util.List;
public class Tree2<T> extends Node<T> {
public Tree2(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}
Hello I implemented a multiset using a linkedlist and I want to implement the multiset using sorted linkedlist. This is multiset abstract class.
import java.io.PrintStream;
public abstract class Multiset<T> {
/**
* Delimiter string for print operation.
*/
protected static final String printDelim = " | ";
public abstract void add(T item);
public abstract int search(T item);
public abstract void removeOne(T item);
public abstract void removeAll(T item);
public abstract void print(PrintStream out);
}
This is my implementation of linkedlist.
import java.io.PrintStream;
public class LinkedListMultiset<T> extends Multiset<T> {
protected Node mHead;
protected int mLength;
public LinkedListMultiset() {
// Implement me!
mHead = null;
mLength = 0;
}
public void add(T item) {
Node newNode = new Node((String) item);
if (mHead == null)
mHead = newNode;
else {
Node currNode = mHead;
Node parentNode = null;
while (currNode != null) {
if (currNode.getValue().
equals(newNode.getValue())) {
currNode.addCounter();
return;
}
parentNode = currNode;
currNode = currNode.getNext();
}
parentNode.setNext(newNode);
}
mLength++;
}
public int search(T item) {
Node currNode = mHead;
while (currNode != null) {
if (currNode.getValue().equals((String) item)) {
return currNode.getCounter();
}
currNode = currNode.getNext();
}
return 0;
}
public void removeOne(T item) {
Node currNode = mHead;
Node lastNode = null;
while (currNode != null) {
if (currNode.getValue().equals((String) item)) {
currNode.minusCounter();
if (currNode.getCounter() == 0) {
if (currNode == mHead)
mHead = currNode.getNext();
else
lastNode.setNext
(currNode.getNext());
mLength--;
}
return;
}
lastNode = currNode;
currNode = currNode.getNext();
}
}
public void removeAll(T item) {
Node currNode = mHead;
Node lastNode = null;
while (currNode != null) {
if (currNode.getValue().equals((String) item)) {
if (currNode == mHead)
mHead = currNode.getNext();
else
lastNode.setNext(currNode.getNext());
mLength--;
return;
}
lastNode = currNode;
currNode = currNode.getNext();
}
}
public void print(PrintStream out) {
Node currNode = mHead;
while (currNode != null) {
out.printf("%s | %d\n", currNode.getValue()
, currNode.getCounter());
currNode = currNode.getNext();
}
}
private class Node {
protected String mValue;
protected Node mNext;
int counter;
public Node(String value) {
mValue = value;
mNext = null;
counter = 1;
}
public void addCounter() {
counter++;
}
public void minusCounter() {
counter--;
}
public int getCounter() {
return counter;
}
public String getValue() {
return mValue;
}
public Node getNext() {
return mNext;
}
public void setValue(String value) {
mValue = value;
}
public void setNext(Node next) {
mNext = next;
}
}
}
I want to implement sorted linkedlist but I want to change my code as minimum as possible.
So I was given an assignment in which I had to find and fix many errors in a somewhat large and sloppy code. I'm down to what looks to be the last one and I can't find the solution to this one. I've read similar scenarios where people get the same mistake but I can't relate them to my code. This is where I get the error: Temp = new BinaryNode(AId,AValue); saying
constructor BinaryNode in class BinaryNode cannot be applied to given
types;
Btree Class
package evidencia2datos;
public class BTree {
private BinaryNode Root;
private int NoOfNodes;
private BTree()
{
Root = null;
NoOfNodes = 0;
}
//operaciones
public boolean IsEmpty() //busca valor en NoOfNodes
{
return(NoOfNodes == 0);
}
public BinaryNode gRoot()
{
return Root;
}
public int Count() //valor de NoOfNodes
{
return NoOfNodes;
}
//size del arbol
public int Size(BinaryNode ATree)
{
if (ATree == null)
return 0;
else
return(1 + Size(ATree.gLeft()) + Size(ATree.gRight()));
}
//niveles
public int Height(BinaryNode ATree)
{
if (ATree == null)
return 0;
else
return (1 + Math.max(Height(ATree.gLeft()), Height(ATree.gRight())));
}
//traversales
public void PreOrder(BinaryNode ATree)
{
if (ATree != null)
{
System.out.println(ATree.gData());
PreOrder(ATree.gLeft());
PreOrder(ATree.gRight());
}
}
public void InOrder(BinaryNode ATree)
{
if (ATree != null)
{
InOrder(ATree.gLeft());
System.out.println(ATree.gData());
InOrder(ATree.gRight());
}
}
public void PostOrder(BinaryNode ATree)
{
if (ATree != null)
{
PostOrder(ATree.gLeft());
PostOrder(ATree.gRight());
System.out.println(ATree.gData());
}
}
//insertar valores
public void Insert(int AId, Object AValue)
{
BinaryNode Temp,Current,Parent;
if(Root == null)//tree is empty
{
Temp = new BinaryNode(AId,AValue);
Root = Temp;
NoOfNodes++;
}
else//tree is not empty
{
Temp = new BinaryNode(AId,AValue);
Current = Root;
while(true)//never ending while loop
{
Parent = Current;
if(AId < Current.gKey())
{//go left
Current = Current.gLeft();
if (Current == null)
{
Parent.sLeft(Temp);
NoOfNodes++;
return;//jump out of loop
}
}
else
{ //go right
Current = Current.gRight();
if(Current == null)
{
Parent.sRight(Temp);
NoOfNodes++;
return;
}
}
}
}
}
//search
public BinaryNode Find(int AKey)
{
BinaryNode Current = null;
if(!IsEmpty())
{
Current = Root; //start search at top of tree
while(Current.gKey() != AKey)
{
if(AKey < Current.gKey())
Current = Current.gLeft();
else
Current = Current.gRight();
if(Current == null)
return null;
}
}
return Current;
}
//succesor
public BinaryNode GetSuccessor(BinaryNode ANode)
{
BinaryNode Current,Successor,SuccessorParent;
Successor = ANode;
SuccessorParent = ANode;
Current = ANode.gRight();
while(Current !=null)
{
SuccessorParent = Successor;
Successor = Current;
Current = Current.gLeft();
}
if(Successor != ANode.gRight())
{
SuccessorParent.sLeft(Successor.gRight());
Successor.sRight(ANode.gRight());
}
return Successor;
}
public boolean Delete (int AKey)
{
BinaryNode Current, Parent;
boolean IsLeftChild = true;
Current = Root;
Parent = Root;
while (Current.gKey() != AKey)
{
Parent = Current;
if (AKey < Current.gKey())
{
IsLeftChild = true;
Current = Current.gLeft();
}
else
{
IsLeftChild = false;
Current = Current.gRight();
}
if(Current == null)
return false;
}
// if no children delete the node
if (Current.gLeft() == null && Current.gRight() == null)
{
if (Current == Root)
Root = Current.gLeft();
else
if (IsLeftChild)
Parent.sLeft(Current.gRight());
else
Parent.sRight(Current.gRight());
}
// if no right child replace with left subtree
else
{
if (Current.gRight() == null)
{
if (Current == Root)
Root = Current.gRight();
else
if (IsLeftChild)
Parent.sLeft(Current.gLeft());
else
Parent.sRight(Current.gLeft());
}
// if no left child replace with right subtree
else
{
if (Current.gLeft() == null)
{
if (Current == Root)
Root = Current.gLeft();
else
if (IsLeftChild)
Parent.sLeft(Current.gRight());
else
Parent.sRight(Current.gRight());
}
// two children so replace in order of successor
else
{
BinaryNode Successor = GetSuccessor(Current);
if (Current == Root)
Root = Successor;
else
if (IsLeftChild)
Parent.sLeft(Successor);
else
Parent.sRight(Successor);
Successor.sLeft(Current.gLeft());
}
}
}
NoOfNodes--;
return true;
}
public static void main(String[] args) {
BTree MyTree = new BTree();
BinaryNode NodeAt;
MyTree.Insert(12,"Jorge");
MyTree.Insert(4,"Andres");
MyTree.Insert(11,"Javier");
MyTree.Insert(1,"Jose");
MyTree.Insert(100,"Paty");
MyTree.Delete(1);
MyTree.InOrder(MyTree.gRoot());
NodeAt = MyTree.Find(11);
if(NodeAt !=null)
System.out.println("Data in Node with Key 11 = " + NodeAt.gData());
System.exit(0);
}
}
BinaryNode Class
package evidencia2datos;
public class BinaryNode {
private int Key;
private Object Data;
private BinaryNode Left;
private BinaryNode Right;
public BinaryNode()
{
java.util.Scanner scaniar = new java.util.Scanner(System.in);
System.out.print("Enter in Key Value: ");
Key = scaniar.nextInt();
System.out.print("Enter in data: ");
Data = scaniar.nextInt();
Left = null;
Right = null;
}
//get
public int gKey()
{
return Key;
}
public Object gData()
{
return Data;
}
public BinaryNode gLeft()
{
return Left;
}
public BinaryNode gRight()
{
return Right;
}
//set
public void sKey(int AValue)
{
Key = AValue;
}
public void sData(Object AValue)
{
Data = AValue;
}
public void sLeft( BinaryNode AValue)
{
Left = AValue;
}
public void sRight( BinaryNode AValue)
{
Right = AValue;
}
}
You can create a new BinaryNode constructor which takes two arguments
BinaryNode (){
...
}
//Este es el nuevo constructor, como se puede observar
//toma dos argumentos
BinaryNode (int k, Object d){
key = k;
data = d;
...
}
I hope this will help you.