I am learning to implement a binary tree , I am trying to get the level of each node , I tried traversing from my root nodes to leaf node but wasn't able to get the right answer. Here is my code
public class BinaryTree
{
private Node root;
/**
Constructs an empty tree.
*/
public BinaryTree() {
root=new Node();
root = null; }
/**
Constructs a tree with one node and no children.
@param rootData the data for the root
*/
public BinaryTree(Object rootData)
{ root=new Node();
root.data=rootData;
}
/**
Constructs a binary tree.
@param rootData the data for the root
@param left the left subtree
@param right the right subtree
*/
public BinaryTree(Object rootData, BinaryTree left, BinaryTree right)
{
root = new Node();
root.data = rootData;
root.left = left.root;
root.right = right.root;
}
class Node
{
public Object data;
public Node left;
public Node right;
public String printTree(int level)
{
return null;
}
}
int getLevelUtil(Node n, Object data, int level)
{
if (n == null)
return 0;
if (n.data == data)
return level;
int downlevel = getLevelUtil(n.left, data, level+1);
if (downlevel != 0)
return downlevel;
downlevel = getLevelUtil(n.right, data, level+1);
return downlevel;
}
/* Returns level of given data value */
int getLevel(Node n, int data)
{
return getLevelUtil(n,data,1);
}
/**
Returns the height of the subtree whose root is the given node.
@param n a node or null
@return the height of the subtree, or 0 if n is null
*/
private static int height(Node n)
{
if (n == null) { return 0; }
else { return 1 + Math.max(height(n.left), height(n.right)); }
}
/**
Returns the height of this tree.
@return the height
*/
public Object data()
{
return root.data;
}
/**
Gets the left subtree of this tree
@return the left child of the root
*/
public BinaryTree left()
{
return null;
}
/**
Gets the right subtree of this tree
@return the right child of the root
*/
public BinaryTree right()
{
return null;
}
/**
* Sets a new right child
* @param child the new right child
*/
public void setRight(BinaryTree child)
{
root.right=child.root;
}
/**
* Sets a new left child
* @param child the new left child
*/
public void setLeft(BinaryTree child)
{
root.left=child.root;
}
public void setData(Object data)
{
root.data=data;
}
public boolean isLeaf()
{
if(root.left==null&& root.right==null)
return true;
else
return false;
}
public String printTree()
{
String s=new String();
s= s+printTree(root);
return s;
}
public String printTree(Node parent)
{ String s=new String();
if (parent == null) { return ""+" "; }
s=s+parent.data+"(level:"+(height(parent))+")";
s=s+printTree(parent.left);
s=s+ printTree(parent.right);
return(s);
}
}
Any way in which I can do this...???
I think you need to add PARENT property for each Node. If you have Parent property, the below code help you get level of a Node:
class Node
{
public Object data;
public Node left;
public Node right;
public Node parent;
public int getLevel() {
int level = 1;
Node n = this;
while (n != null && n.parent != null) {
level++;
n = n.parent;
}
return level;
}
}
NOTE:
I assume ROOT node has Level = 1
You should not use PUBLIC for data member, you can use Private, Protected and use Getter/setter.
Other note: Should not use RECURSIVE, it may cause StackOverflowException.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.