Creating binary search tree - java

So far I have the algorithm figured out to add to my binary search tree, but I'm having a bit of difficulty translating it into code. The algorithm is as follows:
public void add(int v) {
Create a new node n to hold value v.
If tree is empty
Set root to n.
Else
Create temporary node reference m, initialized to root.
Loop looking for a gap (a null left or right pointer) that is on the
correct side of m for v
If v < m.value, look at the left pointer
If v >= m.value, look at the right pointer
If pointer on correct side is not null, set m to that child node and
continue looking
m = m.left or m = m.right
The search for insertion position stops when node m has a null pointer on
the correct side.
Insert the new node n at that position
m.left = n or m.right = n
}
So far I have:
public void add(int v) {
Node n = new Node(v);
if(root==null)
root = n;
else {
Node m = root;
while(...) {
if(...)
m = m.left;
else
m = m.right;
}
if(...)
m.left = m;
else
m.right = n;
}
}
I believe most of that is correct, but I don't know what needs to be done at places marked as "..."

first of all a binary search tree should not have any duplicate values, an important requirement you have not implemented in your code. I have implemented the binary search tree recently while learning datastructures in java. Here is the code i wrote:
public class OrderedBinaryTree
{
private int _elementsPresent = 0;
private Node _root = null;
private int [] _values = null;
private class Node
{
Node _left = null;
Node _right = null;
Node _parent = null;
int _value = 0;
public Node(int value,Node parent)
{
_value = value;
_parent = parent;
}
}
public void put(int value)
{
boolean valueInserted = false;
Node temp = _root;
while(!valueInserted)
{
if(_root == null)
{
_root = new Node(value,null);
break;
}
else if(value == temp._value)
{
System.out.println("the entered value is already present");
return;
}
else if(value<=temp._value)
{
if(temp._left == null)
{
temp._left = new Node(value,temp);
break;
}
else
{
temp = temp._left;
}
}
else
{
if(temp._right == null)
{
temp._right = new Node(value,temp);
break;
}
else
{
temp = temp._right;
}
}
}
_elementsPresent++;
}

Related

Tree traversing doesn't print desired solution

(java)
I have class called Node, which has following fields:
value (integer)
connectedNodes (array of Node objects, always has same size = 2)
combination (object of Combination class)
Combination class has one field called messageContext, let's just say that it's a message which needs to be shown on the screen when something happens (described later).
Also, we have one Tree object, which has only one field: root (Node object)
Now, let's say that we have one String called combinationStr = "1121". Now, we use Tree's method called addCombination:
public void addCombination(Combination finalCombination, Node current, String combination, int counter) {
if(counter==combination.length()) {
return;
}
int value = combination.charAt(counter)-48;
if(current.connectedNodes[value-1]==null) {
current.connectedNodes[value-1] = new Node(value);
}
if(counter==combination.length()-1) {
current.combination = finalCombination;
return;
}
addCombination(finalCombination,current.connectedNodes[value-1],combination,counter+1);
}
finalCombination object is an object that is going to be assigned to the last Node's combination field, added to the Tree for one combinationStr. So, we use this function to create the Tree-like structure that has path: -1 (root) -> 1 -> 1 -> 2 -> 1
When we come to the last one, traversing the Tree, we should see message appear. This is the messageContext of finalCombination.
Okay so, now let's use while(true) loop that will let us input a number, which will be like a path-chooser. If we input 1, we will go to node 1 and have other options to choose.
While loop looks like this:
Scanner scanner = new Scanner(System.in);
Node currentNode = tree.root;
while(true) {
for(Node node: currentNode.connectedNodes) {
if(node!=null) {
System.out.print(node.value + " ");
continue;
}
System.out.print("nullnode ");
}
System.out.println("");
if(currentNode.combination!=null) {
System.out.println(currentNode.combination.messageContext);
}
if(currentNode.connectedNodes[0]==null && currentNode.connectedNodes[1]==null) {
currentNode = tree.root;
System.out.println("root");
}
int x = scanner.nextInt();
currentNode = tree.takeStep(currentNode,x);
}
So, what are we doing here is actually printing the value of current Node, then printing values of Node's we can go to. If Node doesn't exist, we print nullnode.
The takeStep() method looks like this:
public Node takeStep(Node current, int value) {
if(current.connectedNodes[value-1]!=null) {
return current.connectedNodes[value-1];
}
return this.root;
}
It just checks if there is a node we want to go to and returns that node, if it does. If it doesn't exist, it will return us to root.
But, what's the problem with this code ?
Well, look at the whole main class:
Tree tree = new Tree(new Node(-1));
String[] combination = {"1121","11","2212"};
for(String s: combination) {
Combination tempCombination = new Combination();
tempCombination.messageContext = s + " ova poruka";
tree.addCombination(tempCombination,tree.root,s,0);
tree.traverse(tree.root);
System.out.println("END");
}
Scanner scanner = new Scanner(System.in);
Node currentNode = tree.root;
while(true) {
System.out.println(currentNode.value);
for(Node node: currentNode.connectedNodes) {
if(node!=null) {
System.out.print(node.value + " ");
}
else {
System.out.print("nullnode ");
}
}
int x = scanner.nextInt();
if(currentNode.combination!=null) {
System.out.println(currentNode.combination.messageContext);
if(currentNode.connectedNodes[0]==null && currentNode.connectedNodes[1]==null) {
currentNode = tree.root;
break;
}
}
currentNode = tree.takeStep(currentNode,x);
}
When we enter number x, we will call takeStep and check if that node exists connected to current one. But the problem is: When we input 1, it prints everything normally, when we input 1 again, it prints everything normally, when we input 2, it prints everything normally... but when we input 1 again, it says there are 2 nullnodes, and for some reason it doesn't change to root. Can anyone help me please? Here are the full classes:
NODE:
public class Node {
int value;
Node[] connectedNodes = {null,null};
Combination combination;
public Node(int value) {
this.value = value;
this.combination = null;
}
}
TREE:
public class Tree {
Node root;
public Tree(Node root) {
this.root = root;
}
public void addCombination(Combination finalCombination, Node current, String combination, int counter) {
if(counter==combination.length()) {
return;
}
int value = combination.charAt(counter)-48;
if(current.connectedNodes[value-1]==null) {
current.connectedNodes[value-1] = new Node(value);
}
if(counter==combination.length()-1) {
current.combination = finalCombination;
return;
}
addCombination(finalCombination,current.connectedNodes[value-1],combination,counter+1);
}
public void traverse(Node current) {
System.out.print(current.value+ " ");
for(Node node: current.connectedNodes) {
if(node!=null) {
traverse(node);
}
}
}
public Node takeStep(Node current, int value) {
if(current.connectedNodes[value-1]!=null) {
return current.connectedNodes[value-1];
}
return this.root;
}}
COMBINATION:
public class Combination {
String messageContext;
}
Can you please help me ? I just want to reset to root when it hasn't anywhere to go else ? Thank you in advance!
I ran your code and found out that you are storing the message context in the parent node instead of the actual node which marks the end of the combination. So I changed this piece of code in addCombination.
public void addCombination(Combination finalCombination, Node current, String combination, int counter) {
if (counter == combination.length()) {
//Storing at the original node.
current.combination = finalCombination;
return;
}
int value = combination.charAt(counter) - 48;
if (current.connectedNodes[value - 1] == null) {
current.connectedNodes[value - 1] = new Node(value);
}
addCombination(finalCombination, current.connectedNodes[value - 1], combination, counter + 1);
}
And changed following in the main code.
while (true) {
System.out.println(currentNode.value);
//Moved it up now as the node it self has the message context.
if (currentNode.combination != null) {
System.out.println(currentNode.combination.messageContext);
if (currentNode.connectedNodes[0] == null && currentNode.connectedNodes[1] == null) {
currentNode = tree.root;
continue;
}
}
for (Node node : currentNode.connectedNodes) {
if (node != null) {
System.out.print(node.value + " ");
} else {
System.out.print("nullnode ");
}
}
int x = scanner.nextInt();
currentNode = tree.takeStep(currentNode, x);
}
Now try the code it is resetting to root as expected.

partition in a singly linked list

I was doing this exercice:
Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. Example input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
I found it hard to find a solution for Singly linked list (that created by my own, not using library), I would like to know if there is uncessary code blocks in my code and is there a way to avoid putting in two lists and then merge? because it seems to have very slow performance like that.
public CustomLinkedList partition(CustomLinkedList list, int x) {
CustomLinkedList beforeL = new CustomLinkedList();
CustomLinkedList afterL = new CustomLinkedList();
LinkedListNode current = list.getHead();
while (current != null) {
if (current.getData() < x) {
addToLinkedList(beforeL, current.getData());
} else {
addToLinkedList(afterL, current.getData());
}
// increment current
current = current.getNext();
}
if (beforeL.getHead() == null)
return afterL;
mergeLinkedLists(beforeL, afterL);
return beforeL;
}
public void addToLinkedList(CustomLinkedList list, int value) {
LinkedListNode newEnd = new LinkedListNode(value);
LinkedListNode cur = list.getHead();
if (cur == null)
list.setHead(newEnd);
else {
while (cur.getNext() != null) {
cur = cur.getNext();
}
cur.setNext(newEnd);
cur = newEnd;
}
}
public void mergeLinkedLists(CustomLinkedList list1, CustomLinkedList list2) {
LinkedListNode start = list1.getHead();
LinkedListNode prev = null;
while (start != null) {
prev = start;
start = start.getNext();
}
prev.setNext(list2.getHead());
}
CustumLinkedList contains two attributes: -LinkedListNode which is the head and an int which is the size.
LinkedListNode contains two attributes: One of type LinkedListNode pointing to next node and one of type int: data value
Thank you.
The problem of your code is not merging two lists as you mentioned. It's wrong to use the word merge here because you're only linking up the tail of the left list with head of right list which is a constant time operation.
The real problem is - on inserting a new element on the left or right list, you are iterating from head to tail every time which yields in-total O(n^2) operation and is definitely slow.
Here I've wrote a simpler version and avoid iterating every time from head to insert a new item by keeping track of the current tail.
The code is very simple and is definitely faster than yours(O(n)). Let me know if you need explanation on any part.
// I don't know how your CustomLinkedList is implemented. Here I wrote a simple LinkedList node
public class ListNode {
private int val;
private ListNode next;
public ListNode(int x) {
val = x;
}
public int getValue() {
return this.val;
}
public ListNode getNext() {
return this.next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
public ListNode partition(ListNode head, int x) {
if(head == null) return null;
ListNode left = null;
ListNode right = null;
ListNode iterL = left;
ListNode iterR = right;
while(iter != null) {
if(iter.getValue() < x) {
iterL = addNode(iterL, iter.getValue());
}
else {
iterR = addNode(iterR, iter.getValue());
}
iter = iter.getNext();
}
// link up the left and right list
iterL.setNext(iterR);
return left;
}
public ListNode addNode(ListNode curr, int value) {
ListNode* newNode = new ListNode(value);
if(curr == null) {
curr = newNode;
} else {
curr.setNext(newNode);
curr = curr.getNext();
}
return curr;
}
Hope it helps!
If you have any list of data, access orderByX Method.
Hope it would help you.
public class OrderByX {
Nodes root = null;
OrderByX() {
root = null;
}
void create(int[] array, int k) {
for (int i = 0; i < array.length; ++i) {
root = insert(root, array[i]);
}
}
Nodes insert(Nodes root, int data) {
if (root == null) {
root = new Nodes(data);
} else {
Nodes tempNew = new Nodes(data);
tempNew.setNext(root);
root = tempNew;
}
return root;
}
void display() {
Nodes tempNode = root;
while (tempNode != null) {
System.out.print(tempNode.getData() + ", ");
tempNode = tempNode.getNext();
}
}
void displayOrder(Nodes root) {
if (root == null) {
return;
} else {
displayOrder(root.getNext());
System.out.print(root.getData() + ", ");
}
}
Nodes orderByX(Nodes root, int x) {
Nodes resultNode = null;
Nodes lessNode = null;
Nodes greatNode = null;
Nodes midNode = null;
while (root != null) {
if (root.getData() < x) {
if (lessNode == null) {
lessNode = root;
root = root.getNext();
lessNode.setNext(null);
} else {
Nodes temp = root.getNext();
root.setNext(lessNode);
lessNode = root;
root = temp;
}
} else if (root.getData() > x) {
if (greatNode == null) {
greatNode = root;
root = root.getNext();
greatNode.setNext(null);
} else {
Nodes temp = root.getNext();
root.setNext(greatNode);
greatNode = root;
root = temp;
}
} else {
if (midNode == null) {
midNode = root;
root = root.getNext();
midNode.setNext(null);
} else {
Nodes temp = root.getNext();
root.setNext(midNode);
midNode = root;
root = temp;
}
}
}
resultNode = lessNode;
while (lessNode.getNext() != null) {
lessNode = lessNode.getNext();
}
lessNode.setNext(midNode);
while (midNode.getNext() != null) {
midNode = midNode.getNext();
}
midNode.setNext(greatNode);
return resultNode;
}
public static void main(String... args) {
int[] array = { 7, 1, 6, 2, 8 };
OrderByX obj = new OrderByX();
obj.create(array, 0);
obj.display();
System.out.println();
obj.displayOrder(obj.root);
System.out.println();
obj.root = obj.orderByX(obj.root, 2);
obj.display();
}
}
class Nodes {
private int data;
private Nodes next;
Nodes(int data) {
this.data = data;
this.next = null;
}
public Nodes getNext() {
return next;
}
public void setNext(Nodes next) {
this.next = next;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
I think that maintaining two lists is not an issue. It is possible to use a single list, but at the cost of loosing some of the simplicity.
The principal problem seems to be the addToLinkedList(CustomLinkedList list, int value) method.
It iterates throughout the entire list in order to add a new element.
One alternative is to always add elements at the front of the list. This would also produce a valid solution, and would run faster.

Adding two numbers given (digits in Linked List) using Strings

I am trying to add two non negative numbers, the digits of which are stored in reverse order in two separate linked lists. The answer should also be a linked list with digits reversed and no trailing zeros.
I understand that there is a way to solve this question by adding digits and maintaining a carry each time, but I am trying to solve it by using addition operation on numbers.
Here's my code:
/**
* Definition for singly-linked list.
* class ListNode {
* public int val;
* public ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode a, ListNode b) {
if(a==null || b==null){
return null;
}
String num1 = "";
String num2 = "";
ListNode temp1 = a;
ListNode temp2 = b;
while(temp1!=null){
num1 = num1+Integer.toString(temp1.val);
temp1 = temp1.next;
}
new StringBuilder(num1).reverse().toString();
double value1 = Double.parseDouble(num1);
while(temp2!=null){
num2 = num2+Integer.toString(temp2.val);
temp2 = temp2.next;
}
new StringBuilder(num2).reverse().toString();
double value2 = Double.parseDouble(num2);
double result = value1+value2;
String res = String.format("%.0f",result);
ListNode first_node = new ListNode(Character.getNumericValue(res.charAt(0)));
ListNode ans = first_node;
for(int j=1;j<res.length();j++){
ListNode node = new ListNode(Character.getNumericValue(res.charAt(j)));
add(node,ans);
}
return ans;
}
public void add(ListNode node, ListNode ans){
ListNode temp;
temp = ans;
ans = node;
ans.next = temp;
}
}
My code has been giving wrong answers. Can anyone point out the errors?
Your approach is not correct and indirect.
You are trying to do big numbers arithmetics using floating point operations.
As a result - computing errors.
We have:
List<Integer> firstNumber;
List<Integer> secondNumber;
Lets assume firstNumber > secondNumber.
Try this alogrithm:
List<Integer> result = new ArrayList<>();
int i = 0;
int appendix = 0;
for (; i < secondNumber.size(); i++) {
int sum = firstNumber.get(i) + secondNumber.get(i) + appendix;
result.append(sum % 10);
appendix = sum / 10;
}
for (; i < firstNumber.size(); i++) {
int sum = firstNumber.get(i) + appendix;
result.append(sum % 10);
appendix = sum / 10;
}
if (appendix != 0)
result.append(appendix);
return result;
Your add function looks incorrect. You will get you number in the reverse order than what is expected.
Also, your solution is missing the point of the question. Your approach will fail if you have a number with a lot of digits (even double has its limits ~ 2^1024 I think). A linked list representation allows for numbers even bigger.
The correct solution would just iterate through both the lists simultaneously with a carry digit while creating the solution list. If this is a question in an assignment or coding competition, your solution would be judged wrong.
Your add method is wrong, it doesn't build correctly the list. Here is how the final part of your method should look, without the add method:
for(int j=1;j<res.length();j++){
ans.next = new ListNode(Character.getNumericValue(res.charAt(j)));;
ans = ans.next;
}
return first_node;
In your approach, the variable ans is not updating. You can try this:
ans = add(node,ans);
and in your add method, change the method to return ListNode ans
Your approach is not straightforward and wouldn't give you expected results.
Here is a simple approach which wouldn't require much explanation as the addition is simple integer by integer.
Do note that i carry forward when the sum of two integers is greater than 9 else continue with the sum of next integers from both the list.
class Node {
private Object data;
private Node next;
public Object getData() { return data; }
public void setData(Object data) { this.data = data; }
public Node getNext() { return next; }
public void setNext(Node next) { this.next = next; }
public Node(final Object data, final Node next) {
this.data = data;
this.next = next;
}
#Override
public String toString() { return "Node:[Data=" + data + "]"; }
}
class SinglyLinkedList {
Node start;
public SinglyLinkedList() { start = null; }
public void addFront(final Object data) {
// create a reference to the start node with new data
Node node = new Node(data, start);
// assign our start to a new node
start = node;
}
public void addRear(final Object data) {
Node node = new Node(data, null);
Node current = start;
if (current != null) {
while (current.getNext() != null) {
current = current.getNext();
}
current.setNext(node);
} else {
addFront(data);
}
}
public void deleteNode(final Object data) {
Node previous = start;
if (previous == null) {
return;
}
Node current = previous.getNext();
if (previous != null && previous.getData().equals(data)) {
start = previous.getNext();
previous = current;
current = previous.getNext();
return;
}
while (current != null) {
if (current.getData().equals(data)) {
previous.setNext(current.getNext());
current = previous.getNext();
} else {
previous = previous.getNext();
current = previous.getNext();
}
}
}
public Object getFront() {
if (start != null) {
return start.getData();
} else {
return null;
}
}
public void print() {
Node current = start;
if (current == null) {
System.out.println("SingleLinkedList is Empty");
}
while (current != null) {
System.out.print(current);
current = current.getNext();
if (current != null) {
System.out.print(", ");
}
}
}
public int size() {
int size = 0;
Node current = start;
while (current != null) {
current = current.getNext();
size++;
}
return size;
}
public Node getStart() {
return this.start;
}
public Node getRear() {
Node current = start;
Node previous = current;
while (current != null) {
previous = current;
current = current.getNext();
}
return previous;
}
}
public class AddNumbersInSinglyLinkedList {
public static void main(String[] args) {
SinglyLinkedList listOne = new SinglyLinkedList();
SinglyLinkedList listTwo = new SinglyLinkedList();
listOne.addFront(5);
listOne.addFront(1);
listOne.addFront(3);
listOne.print();
System.out.println();
listTwo.addFront(2);
listTwo.addFront(9);
listTwo.addFront(5);
listTwo.print();
SinglyLinkedList listThree = add(listOne, listTwo);
System.out.println();
listThree.print();
}
private static SinglyLinkedList add(SinglyLinkedList listOne, SinglyLinkedList listTwo) {
SinglyLinkedList result = new SinglyLinkedList();
Node startOne = listOne.getStart();
Node startTwo = listTwo.getStart();
int carry = 0;
while (startOne != null || startTwo != null) {
int one = 0;
int two = 0;
if (startOne != null) {
one = (Integer) startOne.getData();
startOne = startOne.getNext();
}
if (startTwo != null) {
two = (Integer) startTwo.getData();
startTwo = startTwo.getNext();
}
int sum = carry + one + two;
carry = 0;
if (sum > 9) {
carry = sum / 10;
result.addRear(sum % 10);
} else {
result.addRear(sum);
}
}
return result;
}
}
Sample Run
Node:[Data=3], Node:[Data=1], Node:[Data=5]
Node:[Data=5], Node:[Data=9], Node:[Data=2]
Node:[Data=8], Node:[Data=0], Node:[Data=8]

How to find size of the largest BST in a Binary Tree

import java.util.*;
class Node
{
int data;
Node left , right;
public Node (int item)
{
data = item;
left = right = null;
}
}
class BinaryTree
{
public static Node root;
BinaryTree()
{
root = null;
}
public int largestBST(Node root)
{
MinMax m = largest(root);
return m.size;
}
public MinMax largest(Node root)
{
if(root == null)
{
return new MinMax();
}
MinMax leftMinMax = largest(root.left);
MinMax rightMinMax = largest(root.right);
MinMax m = new MinMax();
if(leftMinMax.isBST == false || rightMinMax.isBST == false || (leftMinMax.max > root.data) || (rightMinMax.min <= root.data))
{
m.isBST = false;
m.size = Math.max(leftMinMax.size , rightMinMax.size);
}
m.isBST = true;
m.size = leftMinMax.size + rightMinMax.size + 1;
m.min = root.left != null ? leftMinMax.min : root.data;//if left node is null take node as min or min of left
m.max = root.right != null ? rightMinMax.max : root.data;//if right node is null take node as max or max of right
return m;
}
class MinMax
{
int max,min;
boolean isBST;
int size;
MinMax()
{
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
isBST = false;
size = 0;
}
}
public static void main(String args[])
{
BinaryTree bt = new BinaryTree();
bt.root = new Node(25);
bt.root.left = new Node(18);
bt.root.right = new Node(50);
bt.root.left.left = new Node(19);
bt.root.left.right = new Node(20);
bt.root.right.left = new Node(35);
bt.root.right.right = new Node(60);
bt.root.right.left.right = new Node(40);
bt.root.right.left.left = new Node(20);
bt.root.right.right.left = new Node(55);
bt.root.right.right.right = new Node(70);
int size = bt.largestBST(root);
System.out.println("The size of largest BST is " + size);
}
}
Here the output should be 7 but instead it is showing the total number of nodes in the tree i.e. 11.
What's wrong with the code?
If you wish to find the maximum depth of the tree, then rewriting largestBST(Node root) like this will do:
public int largestBST(Node root)
{
if (root == null) {
return 0;
}
return 1 + Math.max(largestBST(root.left), largestBST(root.right));
}
What is happening here is that we return 1 + max(left subtree depth, right subtree depth). If the node doesn't exist, we return 0 to stop the recursion. This will return 4.
If by largest, you mean the subtree with the most nodes starting from the given root, regardless of the depth, then we can modify this solution to get the following:
public int largestBST(Node root)
{
if (root == null) {
return 0;
}
return Math.max(countNodes(root.left), countNodes(root.right));
}
public int countNodes(Node root)
{
if (root == null) {
return 0;
}
return 1 + countNodes(root.left) + countNodes(root.right);
}
As you can see, the recursion now happens in the countNodes() method, and largestBST() simply returns the max from left and right branches. The recursion follows the same principle as before. 0 is returned if a node doesn't exist, and we add 1 for each node that does exist. The result will be 7 now.

How to get the level of a node in a generic tree?

Given a generic tree implemented as a root node with a list of sons, which sons are nodes and again each node has a list of its sons.
__A__
/ | \
B C D
| / \
E F G
The node A has a list of its sons: B, C, D
B, C, D also have a list of their sons: B --> E ; C --> F, G ; D --> null ;
I will explain my idea of the algorithm, you can fix it or give me another completely new idea.
public Integer level(T dato) {...}
Traverse the tree adding to the queue each node of the tree or adding a "null" if the last node added is the last node of the level. Null is an identifier in the queue to know where the level has ended.
My problem is that I don't know exactly where to put the identifier after the first time.
Here is some of the code:
public Integer level(T data){
int inclu= this.include(data);
if (inclu==-1) { // if the tree doesn't include the data
return -1;
} else {
return inclu; // returns the level
}
}
public Integer include( T data ) { // returns the level where the data is
Integer inclu = -1; // -1 if the data is not included
if (this.getDataRoot()==data){
return 0; // The root of the tree has the data
}
else {
LinkedList<GenericNode<T>> queue = new LinkedList<GenericNode<T>>();
GenericNode<T> tree = new GenericNode<T>();
int level=1;
queue.addAtBeginning(this.getRoot());
queue.addAtBeginning(null);
while (queue.size()>0 && inclu==-1) {
if(queue.element(queue.size())!=null) { // if it is not the end of the level then dequeue
tree.setData(queue.element(queue.size()).getData()); //queue.element(position) returns the element in that position
tree.setListOfSons(queue.element(queue.size()).getSons());
if (tree.getSons()!=null) { // if the tree has sons
int i=1;
while(i<=tree.getSons().size() && inclu==-1) {
queue.addAtBeginning(tree.getSons().element(i));
if (tree.getSons().element(i).getData()==data) // if I found the data I'm looking for
inclu=level;
i++; // counter
}
}
} else { // if it is the end of the level (means the queue gave me a null)
level++;
}
queue.delete(queue.size()); //ending the dequeue process
} //end while
} // end main else
return inclu; //returns the summation of the levels or 0 if it was found at the root of the tree or -1 if the data was not found
}
I wrote a class that returns the level of target node in specific tree.
import java.util.LinkedList;
import java.util.List;
public class TreeLevel {
public static class Node {
public Node(String data) { this.data = data ; };
public String data;
public List<Node> childs = new LinkedList<Node>();
}
public static Integer level(Node tree, Node target){
return level(tree, target, 0);
}
private static Integer level(Node tree, Node target, int currentLevel) {
Integer returnLevel = -1;
if(tree.data.equals(target.data)) {
returnLevel = currentLevel;
} else {
for(Node child : tree.childs) {
if((returnLevel = level(child, target, currentLevel + 1)) != -1){
break;
}
}
}
return returnLevel;
}
public static void main(String[] args) {
Node a = new Node("A");
Node b = new Node("B");
Node c = new Node("C");
Node d = new Node("D");
Node e = new Node("E");
Node f = new Node("F");
Node g = new Node("G");
// childs of a:
a.childs.add(b);
a.childs.add(c);
a.childs.add(d);
// childs of b:
b.childs.add(e);
// childs of c:
c.childs.add(f);
c.childs.add(g);
// childs of d:
// d.childs = null or simply d.childs.length() is 0
Node target = new Node("G");
Integer level = level(a, target);
System.out.println("level [" + level + "]");
}
}
I think I can give you a simple code for this question. You can change the it according to your code.
public Integer include( T data ) { // returns the level where the data is
Integer inclu = -1; // -1 if the data is not included
if (this.getDataRoot() == data){
return 0; // The root of the tree has the data
}
return level(this.getRoot(), data, 1);
}
//Find data in a tree whose root is Node
//If not found, return -1
public int level(T node, T data, int level) {
if (!node.hasChildren()) {
return -1;
}
for (T child : node.getChildren()) {
if (child.getData == data) {
return level; //Aha!!! found it
} else {
int l = level(child, data, level + 1); /// find in this sub-tree
if (l != -1) {
return l;
}
}
}
return -1; /// Not found in this sub-tree.
}
P.S : == is used to compare, which is not good. .equals() should be used.

Categories

Resources