This question already exists:
Write an address book in java using a binary search tree [closed]
Closed 9 years ago.
Hello I am writing an AddressBook application in java and I have written the whole program. The only thing is that its not working as expected and there are no errors in the code so i am unable to troubleshoot it. Any help would be much appriciated.
EDIT: This is not a duplicate question as this includes all the methods. The other one didn't have the main method and is incomplete and as it was closed i was forced to ask a new question. Makes sense?
package com.addressbook;
public abstract class KeyedItem<KT extends Comparable<? super KT>> {
private KT searchKey;
public KeyedItem(KT key){
searchKey = key;
}
public KT getKey(){
return searchKey;
}
}
package com.addressbook;
public class People extends KeyedItem<String> {
private String address;
private String phone;
public People(String n, String a, String p){
super(n);
this.address = a;
this.phone = p;
}
public void setAddress(String a){
address = a;
}
public void setPhone(String p){
phone = p;
}
public String toString(){
return "Name:" + getKey() + "\nAddress:" + address + "\nPhone:" + phone;
}
public String getTheKey(){
return getKey();
}
}
package com.addressbook;
public class BinaryNode{
// Friendly data; accessible by other package routines
private People people; // The data in the node
private BinaryNode left; // Left child
private BinaryNode right; // Right child
// Constructors
public BinaryNode(People pe, BinaryNode l, BinaryNode r) {
people = pe;
left = l;
right = r;
}
public BinaryNode(People pe) {
people = pe;
left = right = null;
}
public void setData(People p){
people = p;
}
public String getSearch(){
return people.getTheKey();
}
public People getData(){
return people;
}
public BinaryNode getLeft(){
return left;
}
public BinaryNode getRight(){
return right;
}
}
package com.addressbook;
import com.addressbook.People;
import com.addressbook.BinaryNode;
public class AddressBook {
private BinaryNode root;
public AddressBook() {
super();
}
public AddressBook(People p) {
super();
root = new BinaryNode(p);
}
public void insert(People p){
insert(p, root);
}
public People get(String key) {
BinaryNode node = root;
while (node != null) {
if (key.compareTo(node.getSearch()) == 0) {
return node.getData();
}
if (key.compareTo(node.getSearch()) < 0) {
node = node.getLeft();
} else {
node = node.getRight();
}
}
return null;
}
protected BinaryNode insert(People p, BinaryNode node) {
if (node == null) {
return new BinaryNode(p);
}
if (p.getTheKey().compareTo(node.getSearch()) == 0) {
return new BinaryNode(p, node.getLeft(), node.getRight());
} else {
if (p.getTheKey().compareTo(node.getSearch()) < 0) { // add to left subtree
insert(p, node.getLeft());
} else { // add to right subtree
insert(p, node.getRight());
}
}
return node;
}
public void remove(String key) {
remove(key, root);
}
protected BinaryNode remove(String k, BinaryNode node) {
if (node == null) { // key not in tree
return null;
}
if (k.compareTo(node.getSearch()) == 0) { // remove this node
if (node.getLeft() == null) { // replace this node with right child
return node.getRight();
} else if (node.getRight() == null) { // replace with left child
return node.getLeft();
} else {
// replace the value in this node with the value in the
// rightmost node of the left subtree
node = getRightmost(node.getLeft());
// now remove the rightmost node in the left subtree,
// by calling "remove" recursively
remove(node.getSearch(), node.getLeft());
// return node; -- done below
}
} else { // remove from left or right subtree
if (k.compareTo(node.getSearch()) < 0) {
// remove from left subtree
remove(k, node.getLeft());
} else { // remove from right subtree
remove(k, node.getRight());
}
}
return node;
}
protected BinaryNode getRightmost(BinaryNode node) {
assert(node != null);
BinaryNode right = node.getRight();
if (right == null) {
return node;
} else {
return getRightmost(right);
}
}
protected String toString(BinaryNode node) {
if (node == null) {
return "";
}
return node.getData().toString() + "(" + toString(node.getLeft()) + ", " +
toString(node.getRight()) + ")";
}
public static void main(String[] arguments) {
AddressBook tree = new AddressBook();
People p1 = new People("person 1", "adresa 1", "404040404");
People p2 = new People("person 2", "adresa 2", "4040434345");
People p3 = new People("person 3", "adresa 3", "346363463");
People p4 = new People("person 4", "adresa 4", "435346346");
People p5 = new People("person 5", "adresa 5", "4363907402");
tree.insert(p1);
tree.insert(p2);
tree.insert(p3);
tree.insert(p4);
tree.insert(p5);
System.out.println(tree.get("person 1"));
}
}
On one hand:
public void insert(People p){
insert(p, root);
}
but that method begins with
protected BinaryNode insert(People p, BinaryNode node) {
if (node == null) {
return new BinaryNode(p);
}
which means, considering both pieces together, you always ignore the new root and hence your tree never fills. Try this instead:
public void insert(People p){
root = insert(p, root);
}
In the same manner you ignore the return value of insert inside insert too. You should handle them in a similar manner.
Related
I am having trouble removing a node from the user input and properly going to the last node, so it will be ready to add a new node after. I am refactoring this code to a larger implementation. However, I am unable to remove the node and go to the last node after. This is also using user input to find the proper node to remove. This is a generic linked list of a comparable type.
import java.util.Scanner;
import java.io.*;
class MyGenericList <T extends Comparable<T>>
{
private class Node<T>
{
T value;
Node<T> next;
}
private Node<T> first = null;
int count = 0;
public void add(T element)
{
Node<T> newnode = new Node<T>();
newnode.value = element;
newnode.next = null;
if (first == null)
{
first = newnode;
}
else
{
Node<T> lastnode = gotolastnode(first);
lastnode.next = newnode;
}
count++;
}
public void remove(T element)
{
Node<T> nn = new Node<T>();
Node<T> cur = first.next;
Node<T> prev = first;
nn.value = element;
boolean deleted = false;
while(cur != null && deleted == false)
{
if(cur.equals(element)) //data cannot be resolved or is not a field
{
prev.next = cur.next;
this.count--;
deleted = true;
}
}
prev = gotolastnode(prev);
prev.next = nn;
}
public T get(int pos)
{
Node<T> Nodeptr = first;
int hopcount=0;
while (hopcount < count && hopcount<pos)
{ if(Nodeptr != null)
{
Nodeptr = Nodeptr.next;
}
hopcount++;
}
return Nodeptr.value;
}
private Node<T> gotolastnode(Node<T> nodepointer)
{
if (nodepointer== null )
{
return nodepointer;
}
else
{
if (nodepointer.next == null)
return nodepointer;
else
return gotolastnode( nodepointer.next);
}
}
}
class Employee implements Comparable<Employee>
{
String name;
int age;
#Override
public int compareTo(Employee arg0)
{
// TODO Auto-generated method stub
return 0;
// implement compareto method here.
}
Employee( String nm, int a)
{
name =nm;
age = a;
}
}
class City implements Comparable<City>
{
String name;
int population;
City( String nm, int p)
{
name =nm;
population = p;
}
#Override
public int compareTo(City arg0) {
// TODO Auto-generated method stub
return 0;
// implement compareto method here.
}
}
public class GenericLinkedList
{
public static void main(String[] args) throws IOException
{
MyGenericList<Employee> ml = new MyGenericList<>();
ml.add(new Employee("john", 32));
ml.add(new Employee("susan", 23));
ml.add(new Employee("dale", 45));
ml.add(new Employee("eric", 23));
Employee e1 = ml.get(0);
System.out.println( "Name " + e1.name + " Age "+ e1.age );
ml.remove(new Employee("john", 32));
System.out.println( "Name " + e1.name + " Age "+ e1.age );
ml.add(new Employee("jerry", 35));
Employee e2 = ml.get(2);
System.out.println( "Name " + e2.name + " Age "+ e2.age );
}
}
The implementation of your remove method was faulty. Please see the fixed remove method below. Comments have been added in order to explain the changes.
The solution was tested via an online Java IDE and is verified to work properly.
public void remove(T element)
{
if(first == null) { // edge case - empty list
return;
}
else if(first.value.equals(element)) { // edge case - removing the first element
first = first.next;
this.count--;
return;
}
//Node<T> nn = new Node<T>(); // no need to create a new node, but rather remove an existing node.
Node<T> cur = first.next;
Node<T> prev = first;
//nn.value = element; //no need to create a new node and set its value attribute
boolean deleted = false;
while(cur != null && deleted == false)
{
if(cur.value.equals(element)) //data cannot be resolved or is not a field
{
prev.next = cur.next;
this.count--;
deleted = true;
}
else { // added missing advancement of the loop iterator - cur. prev must also be advanced
cur = cur.next;
prev = prev.next;
}
}
// This implementation adds the removed element to the end of the list, meaning
// it is not a remove method, but rather a move to the end implementation.
// In order to conform to what a remove method does, the last two code lines were commented out.
//prev = gotolastnode(prev);
//prev.next = nn;
}
You must also add an overridden implementation of equals in the Employee class (and other classes) which is used by your list:
class Employee implements Comparable<Employee>
{
String name;
int age;
#Override
public int compareTo(Employee arg0)
{
// sort first by name, then by age
if(name.equals(arg0.name)) {
return age - arg0.age;
}
return name.compareTo(arg0.name);
}
Employee( String nm, int a)
{
name =nm;
age = a;
}
#Override
public boolean equals(Object emp) {
boolean result = false;
if(emp != null && emp instanceof Employee) {
Employee e = (Employee)emp;
result = name.equals(e.name) && (age == e.age);
}
return result;
}
}
Hi I'm fairly new to programming and have a project I was hoping to get some help with. I'm supposed to read some names and phone numbers from a text file and then use them to populate a binary search tree. Each line in the text file (there are 5 lines total) has one person's name (first and last) along with their phone number. I'm then supposed to use a search key to look for the name of each person. Here are the instructions:
Write a program that provides a way for you to store and retrieve telephone numbers. Design a console program that provides the following operations:
Add: Adds a person’s name and phone number to the phone book.
Delete: Deletes a given person’s name and phone number from the phone book, given only the name.
Find: Locates a person’s phone number, given only the person’s name.
Change: Changes a person’s phone number, given the person’s name and new phone number.
Quit: Quits the application, after first saving the phone book in a text file.
You can proceed as follows:
Design and implement the class Person, which represents the name and phone number of a person. You will store instances of this class in the phone book. Design and implement the class PhoneBook, which represents the phone book. The class should contain a binary search tree as a data field. This tree contains the people in the book. Add methods that use a text file to save and restore the tree. Design and implement the class Menu, which provides the program’s user interface.
The program should read data from a text file when it begins and save data into the text file when the user quits the program.
Where I'm having trouble is populating the BST. When I try to populate the tree I get this error message "incompatible types: String cannot be converted to KeyedItem" How do I change the KeyedItem class so that it will work? Also, if I can overcome the KeyedItem issues, will the code I've written in the Menu class be sufficient to populate the BST? I know there's a lot of info here; I thank you in advance for any help you can give me.
Here are my classes:
Menu class:
public static void main(String[]args) throws IOException{
String read = "";
PhoneBook btree = new PhoneBook(); //creates bst object from phonebook class
BufferedReader input = new BufferedReader(new FileReader("names.txt"));
for (int i=0; i<5; i++){
read = input.readLine(); //reads each lin of text file and stores it in var read
}
btree.insert(read); //this is where I get the ERROR message
}
}
KeyedItem search class:
public abstract class KeyedItem<KT extends Comparable<? super KT>> {
private KT searchKey;
public KeyedItem(KT key){ //constructor
searchKey = key;
}
Phonebook class:
public class PhoneBook<T extends KeyedItem<KT>, KT extends Comparable<? super KT>> extends BinaryTreeBasis<T> {
public PhoneBook(){
}
public PhoneBook(T rootItem){
super(rootItem);
}
public void setRootItem(T newItem) //I believe this class is not always used
throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
public void insert(T newItem){
root = insertItem(root, newItem);
}
public T retrieve(KT searchKey){
return retrieveItem(root, searchKey);
}
public void delete(KT searchKey) throws TreeException{
root = deleteItem(root, searchKey);
}
public void delete(T item) throws TreeException{
root = deleteItem(root, item.getKey());
}
protected TreeNode<T> insertItem(TreeNode<T> tNode, T newItem){
TreeNode<T> newSubtree;
if(tNode == null){
tNode = new TreeNode<T>(newItem, null, null);
return tNode;
}
T nodeItem = tNode.item;
if(newItem.getKey().compareTo(nodeItem.getKey()) < 0) {
newSubtree = insertItem(tNode.leftChild, newItem);
tNode.leftChild = newSubtree;
return tNode;
}
else {
newSubtree = insertItem(tNode.rightChild, newItem);
tNode.rightChild = newSubtree;
return tNode;
}
}
protected T retrieveItem (TreeNode<T> tNode, KT searchKey) {
T treeItem;
if(tNode == null){
treeItem = null;
}
else {
T nodeItem = tNode.item;
if (searchKey.compareTo(nodeItem.getKey()) == 0) {
treeItem = tNode.item;
}
else if (searchKey.compareTo(nodeItem.getKey()) < 0){
treeItem = retrieveItem(tNode.leftChild, searchKey);
}
else {
treeItem = retrieveItem(tNode.rightChild, searchKey);
}
}
return treeItem;
}
protected TreeNode<T> deleteItem(TreeNode<T> tNode, KT searchKey) {
TreeNode<T> newSubtree;
if (tNode == null){
throw new TreeException("TreeException: Item not found");
}
else{
T nodeItem = tNode.item;
if (searchKey.compareTo(nodeItem.getKey()) == 0){
tNode = deleteNode(tNode);
}
else if (searchKey.compareTo(nodeItem.getKey()) < 0){
newSubtree = deleteItem(tNode.leftChild, searchKey);
tNode.leftChild = newSubtree;
}
else {
newSubtree = deleteItem(tNode.rightChild, searchKey);
tNode.rightChild = newSubtree;
}
}
return tNode;
}
protected TreeNode<T> deleteNode(TreeNode<T> tNode){
T replacementItem;
if((tNode.leftChild == null) &&
(tNode.rightChild == null)){
return null;
}
else if (tNode.leftChild == null){
return tNode.rightChild;
}
else if (tNode.rightChild == null){
return tNode.leftChild;
}
else {
replacementItem = findLeftmost(tNode.rightChild);
tNode.item = replacementItem;
tNode.rightChild = deleteLeftmost(tNode.rightChild);
return tNode;
}
}
protected T findLeftmost(TreeNode<T> tNode){
if (tNode.leftChild == null) {
return tNode.item;
}
else {
return findLeftmost(tNode.leftChild);
}
}
protected TreeNode<T> deleteLeftmost(TreeNode<T> tNode){
if (tNode.leftChild == null){
return tNode.rightChild;
}
else{
tNode.leftChild = deleteLeftmost(tNode.leftChild);
return tNode;
}
}
}
public KT getKey(){
return searchKey;
}
}
Person class:
public class Person extends KeyedItem<String>{
private FullName name;
private String phoneNumber;
public Person(String id, FullName name, String phone){ //constructor
super(id);
this.name = name;
phoneNumber = phone;
}
public String toString(){
return getKey() + " # " + name;
}
}
TreeNode class:
public class TreeNode<T> {
T item;
TreeNode<T> leftChild;
TreeNode<T> rightChild;
public TreeNode(T newItem){ //constructor
item = newItem;
leftChild = null;
rightChild = null;
}
public TreeNode(T newItem, TreeNode<T> left, TreeNode<T> right){
item = newItem;
leftChild = left;
rightChild = right;
}
}
BinaryTreeBasis class:
public abstract class BinaryTreeBasis<T> {
protected TreeNode<T> root;
public BinaryTreeBasis(){ //default constructor
root = null;
}
public BinaryTreeBasis(T rootItem){
root = new TreeNode<T>(rootItem, null, null);
}
public boolean isEmpty(){
return root == null;
}
public void makeEmpty(){
root = null;
}
public T getRootItem() throws TreeException {
if (root == null){
throw new TreeException("Tree Exception: Empty Tree");
}
else{
return root.item;
}
}
public abstract void setRootItem(T newItem);
}
Fullname class:
public class FullName implements java.lang.Comparable<Object>{ //change from object?
private String firstName;
private String lastName;
public FullName(String first, String last){
firstName = first;
lastName = last;
}
public int compareTo(Object rhs){
FullName other = (FullName)rhs;
if(lastName.compareTo(((FullName)other).lastName)==0){
return firstName.compareTo(((FullName)other).firstName);
}
else {
return lastName.compareTo(((FullName)other).lastName);
}
}
}
Tree Iterator class:
public class TreeIterator<T> implements java.util.Iterator<T> {
private BinaryTreeBasis<T> binTree;
private TreeNode<T> currentNode;
private LinkedList <TreeNode<T>> queue;
public TreeIterator(BinaryTreeBasis<T> bTree){
binTree = bTree;
currentNode = null;
queue = new LinkedList <TreeNode<T>>();
}
public boolean hasNext(){
return !queue.isEmpty();
}
public T next()
throws java.util.NoSuchElementException{
currentNode = queue.remove();
return currentNode.item;
}
public void remove()
throws UnsupportedOperationException{
throw new UnsupportedOperationException();
}
public void setPreorder(){
queue.clear();
preorder(binTree.root);
}
public void setInorder(){
queue.clear();
inorder(binTree.root);
}
public void setPostorder(){
queue.clear();
postorder(binTree.root);
}
private void preorder(TreeNode<T> treeNode){
if(treeNode != null){
queue.add(treeNode);
preorder(treeNode.leftChild);
preorder(treeNode.rightChild);
}
}
private void inorder(TreeNode<T> treeNode){
if(treeNode != null){
inorder(treeNode.leftChild);
queue.add(treeNode);
inorder(treeNode.rightChild);
}
}
private void postorder(TreeNode<T> treeNode){
if(treeNode != null){
postorder(treeNode.leftChild);
postorder(treeNode.rightChild);
queue.add(treeNode);
}
}
}
So i'm very much so stuck on my delete function. I keep getting about 20 errors throughout and several are different ones. When I delete the entire thing from my code though, it compiles, so I know it's only this that is missing and messing up my code, but I also really need it in my code of course! here's the delete function and I'll also list my node Class under it as well
public Node delete(Node t, int key){
if (t == null){
return t;
}
else if (key < t.getKey()){
t.getLeft() = delete(t.left, key);
}
else if(key > t.getKey()){
t.getRight() = delete(t.right, key);
}
else{
//Deleting a node with no subtrees
if (t.getLeft() == null && t.getRight() == null){
t = null;
t--;
}
//Node only has one subtree
else if(t.getLeft() != null && t.getRight() == null){
t = t.setLeft();
t--;
}
//Node only has one subtree again for opposite side
else if (t.getLeft() == null && t.getRight() != null){
t = t.setLeft();
t--;
}
//Finding minimum in the right side, and replace
else if (t.getLeft() != null && t.getRight() != null){
temp = t;
if (t.getRight().getRight() == null && t.getRight().getLeft() == null){
temp = successor(t.getLeft());
}
else{
temp = successor(t.getRight());
}
t.key = temp.key;
if (t.getRight().getRight() == null || t.getLeft().getLeft() == null){
t.setLeft() = delete(t.left, t.key);
}
else{
t.setRight() = delete(t.right, t.key);
}
}
}
}
public Node successor(Node t){
while (t.getLeft() != null){
t = t.left;
}
}
Here's my Node Class for reference to some of the functions like getLeft() and so on:
public class Node{
private Node next;
private String name;
private int ssn;
private int key;
private Node left;
private Node right;
public Node(String name, int ssn){
this.name = name;
this.ssn = ssn;
}
public Node getRight(){
return this.right;
}
public Node getLeft(){
return this.left;
}
public void setRight(Node p){
right = p;
}
public void setLeft(Node p){
left = p;
}
public void setNext(Node n){
this.next = n;
}
public int getSSN(){
return this.ssn;
}
public int getKey(){
return ssn%10000;
}
public String getName(){
return name;
}
public Node getNext(){
return this.next;
}
public void setSSN(int ssn){
this.ssn= ssn;
}
}
I am having trouble figuring out how to implement a binary search iterator.
My question is how do i implement an Iterator traversing "in order" while not using the collection classes?
Well the thing is that i have not a clue about how to add "parent" because i find the first 4 items in the iterator when going down but i dont seem to be able to go up since the "parent" either throws nullpointer or dont get the right items.
so how do i add "parent"?
void add(String string) {
if (string.compareTo(value) < 0) {
if(left.left == null){
left.parent = left;
}
if (left == null) {
size++;
left = new Node(string);
if...
thanks.
I suggest a three classes design:
BinarySearchTree: Public class, It represent a Binary search Tree. It contains the tree root as BinaryTreeNode.
BinaryTreeNode: Private nested class, It represent a Node, It has the key and the references: to children and to Its parent.
BinarySearchTreeIterator: Private nested class, It represent an iterator, It use a reference to a BinaryTreeNode to know the current element.
public class BinarySearchTree implements Iterable<String> {
private BinaryTreeNode root = null;
private int elements;
#Override
public Iterator<String> iterator() {
return new BinarySearchTreeIterator(root);
}
private static class BinarySearchTreeIterator implements Iterator<String> {
private BinaryTreeNode node;
public BinarySearchTreeIterator(BinaryTreeNode node) {
if (node != null) {
this.node = smallest(node);
} else {
this.node = node;
}
}
#Override
public boolean hasNext() {
return node != null;
}
private static BinaryTreeNode smallest(BinaryTreeNode n) {
if (n.left != null) {
return smallest(n.left);
} else {
return n;
}
}
#Override
public String next() {
String result = node.key;
if (node.right != null) {
node = smallest(node.right);
} else {
while (node.parent != null && node.parent.left != node) {
node = node.parent;
}
node = node.parent;
}
return result;
}
}
private static class BinaryTreeNode {
private String key;
private BinaryTreeNode parent;
private BinaryTreeNode left;
private BinaryTreeNode right;
public BinaryTreeNode(String key) {
this.key = key;
}
}
public boolean insert(String key) {
if (key == null) {
return false;
}
int lastElements = elements;
this.root = insert(key, root, null);
return lastElements < elements;
}
private BinaryTreeNode insert(String key, BinaryTreeNode node, BinaryTreeNode parent) {
BinaryTreeNode result = node;
if (node == null) {
result = new BinaryTreeNode(key);
result.parent = parent;
this.elements++;
} else {
int compare = key.compareTo(node.key);
if (compare < 0) {
result.left = insert(key, node.left, node);
} else if (compare > 0) {
result.right = insert(key, node.right, node);
}
}
return result;
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
String[] strings = {"l", "f", "t", "c", "g", "p", "u"};
for (String string : strings) {
System.out.println("insert: '" + string + "' " + tree.insert(string));
}
System.out.println("--");
for (String s : tree) {
System.out.println(s);
}
}
}
It prints:
insert: 'l' true
insert: 'f' true
insert: 't' true
insert: 'c' true
insert: 'g' true
insert: 'p' true
insert: 'u' true
--
c
f
g
l
p
t
u
By the function height(Node) I have the height of each node in my tree.for my each Node in tree, i want to determine subtraction of height of left Node from Right Node.
I just should iterate through the tree. but I have StackOverflow error in my code.
give me some help. I think there is something wrong in other part of my program.
public class AvlTree {
private Node root = new Node (0,0,"","");
void devision( Node tmp){
tmp=root;
if(tmp.getLeftChild()!=null){
tmp.devisionNumber=height(tmp.getLeftChild())-height(tmp.getRightChild());
devision(tmp.getLeftChild());
}
if(tmp.getRightChild()!=null){
tmp.devisionNumber=height(tmp.getLeftChild())-height(tmp.getRightChild());
devision(tmp.getRightChild());
}
}
int height(Node node){
if(node==null)
return -1;
int left=height(node.getLeftChild());
int right=height(node.getRightChild());
if(left>right)
{
return left+1;
}
else
return right +1;
}
void insert (Node newNode ){
Node tmp=root;
if(root.getStdNum()==0){
root=newNode;
root.setLeftChild(null);
root.setRightChild(null);
root.setRoot(null);
}
else{
while(true) {
if(newNode.getStdNum()<tmp.getStdNum()){
if(tmp.getLeftChild()==null)
break;
else
tmp=tmp.getLeftChild();
}
if(newNode.getStdNum()>tmp.getStdNum()){
if(tmp.getRightChild()==null)
break;
else
tmp=tmp.getRightChild();
}
}
////////////////////////
if(newNode.getStdNum()<tmp.getStdNum())
{
tmp.setLeftChild(newNode);
newNode.setRoot(tmp);
}
if(newNode.getStdNum()>tmp.getStdNum())
{
tmp.setRightChild(newNode);
newNode.setRoot(tmp);
}
}
}
}
public class Node {
private int StdNum;
private int Avarage;
private String Name;
private String FamillyName;
private Node Root;
public int devisionNumber;
private Node LeftChild;
private Node RightChild;
public Node(int stdNum,int Avarage,String Name,String FamillyName){
this.StdNum=stdNum;
this.Avarage=Avarage;
this.Name=Name;
this.FamillyName=FamillyName;
}
public boolean CheckForNull (Object a){
boolean check;
if(a==null){
check=true;
}
else
check=false;
return check;
}
public void ShowData(){
System.out.println("ID: "+ getStdNum());
System.out.println("Avg: " + getAvarage());
System.out.println("Name: "+ getName());
System.out.println("Familly: "+ getFamillyName()+"\n");
if(CheckForNull(this.getLeftChild())==false)
System.out.println("leftNode ID: "+ getLeftChild().getStdNum() );
else System.out.println("LeftChild : Null ");
if(CheckForNull(this.getRightChild())==false)
System.out.println("RightNode ID: "+ getRightChild().getStdNum());
else System.out.println("RightChild : Null ");
if(CheckForNull(this.getRoot())==false)
System.out.println("Root ID: "+ getRoot().getStdNum());
else System.out.println("Root : Null ");
}
public int getStdNum() {
return StdNum;
}
public void setStdNum(int stdNum) {
StdNum = stdNum;
}
public int getAvarage() {
return Avarage;
}
public void setAvarage(int avarage) {
Avarage = avarage;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getFamillyName() {
return FamillyName;
}
public void setFamillyName(String famillyName) {
FamillyName = famillyName;
}
public Node getRoot() {
return Root;
}
public void setRoot(Node root) {
Root = root;
}
public Node getLeftChild() {
return LeftChild;
}
public void setLeftChild(Node leftChild) {
LeftChild = leftChild;
}
public Node getRightChild() {
return RightChild;
}
public void setRightChild(Node rightChild) {
RightChild = rightChild;
}
}
you always set the node to root (line 2 of the method), so your recursion does not go down the subtrees, but restarts every time ... so the recursion never ends - resulting in a stack overflow.