AVL tree tostring in java code example
Example: AVL TREE JAVA to store string
public AVLNode search(String key)
{
if (root == null)
{
return null;
}
AVLNode p = root;
try
{while (true)
{
if(p == null && p.getData().equals(key))
{
break;
}
else if (key.compareTo(p.getData()) < 0)
{
p = p.getLeftChild();
}
else
{
p = p.getRightChild();
}
}
}
catch(NullPointerException e)
{
System.out.println(e);
}
return p;
}
public void insert(String data)
{
if (root == null)
{
root = new AVLNode(data);
return;
}
AVLNode p = root;
while(true)
{
if (data.equals(p.getData()) && p.getLeftChild() != null)
{
p = p.getLeftChild();
}
else if(data.compareTo(p.getData()) < 0 && p.getLeftChild() != null)
{
p = p.getLeftChild();
}
else if (data.compareTo(p.getData()) > 0 && p.getRightChild() != null)
{
p = p.getRightChild();
}
else
{
break;
}
}
AVLNode newNode= new AVLNode(data);
if(data.equals(p.getData()))
{
p.setLeftChild(newNode);
}
else if (data.compareTo(p.getData()) < 0)
{
p.setLeftChild(newNode);
}
else
{
p.setRightChild(newNode);
}
rebalanceInsertionPath(newNode);
}