AVL Tree中的元素比较,急~~~谢谢各位先~~~
AVLTree<Student>中,AVLTree是按照学生英文姓名按字母顺序拍好的,有userName属性,如何在AVL 树中查找相同的UserName,并在相同UserName中插入数字以作区别,如有三个人User Name都为murin,则这三人userName变为 m1urin,m2urin,m3urin。
注:user name为学生姓加名字首字母,如 XU WEN,userName变为xwen
昨天是把tree放在list对比查找的,速度太慢了~~~,想求助各位大侠有没有好的思路。。。。
这里是AVLTree的代码
import java.io.*;
import java.util.*;
public class AVLTree<T extends Comparable<T>>
{
private AVLNode<T> root;
public AVLTree()
{
root = null;
}
public void inOrder(AVLNode<T> root, PrintStream newfile)
{
if(root != null)
{
inOrder(root.getLeftChild(), newfile);
newfile.println(root.getData());
inOrder(root.getRightChild(), newfile);
}
}
public boolean insert(T data)
{
root = insert(root,data);
return true;
}
public void display(PrintStream newfile)
{
inOrder(root,newfile);
}
private AVLNode<T> insert(AVLNode<T> localRoot, T data)
{
if(localRoot == null)
{
localRoot = new AVLNode<T>(data);
}
else if(data.compareTo(localRoot.getData())<0)
{
AVLNode<T> LeftChild = localRoot.getLeftChild();
AVLNode<T> subtree = insert(LeftChild,data);
localRoot.setLeftChild(subtree);
localRoot = rebalance(localRoot);
}
else
{
AVLNode<T> RightChild = localRoot.getRightChild();
AVLNode<T> subtree = insert(RightChild, data);
localRoot.setRightChild(subtree);
localRoot = rebalance(localRoot);
}
setHeight(localRoot);
return localRoot;
}
private int height(AVLNode<T> localRoot)
{
if(localRoot == null)
{
return -1;
}
else
{
return localRoot.getHeight();
}
}
private void setHeight(AVLNode<T> localRoot)
{
if(height(localRoot.getLeftChild()) > height(localRoot.getRightChild()))
{
localRoot.setHeight(height(localRoot.getLeftChild())+1);
}
else
{
localRoot.setHeight(height(localRoot.getRightChild())+1);
}
}
private AVLNode<T> rightRotation(AVLNode<T> node)
{
AVLNode<T> temp = node.getLeftChild();
node.setLeftChild(temp.getRightChild());
temp.setRightChild(node);
setHeight(node);
return temp;
}
private AVLNode<T> leftRotation(AVLNode node)
{
AVLNode<T> temp = node.getRightChild();
node.setRightChild(temp.getLeftChild());
temp.setLeftChild(node);
setHeight(node);
return temp;
}
private AVLNode<T> rightLeftRotation(AVLNode<T> node)
{
AVLNode<T> temp = node.getRightChild();
node.setRightChild(rightRotation(temp));
return leftRotation(node);
}
private AVLNode<T> leftRighrRotation(AVLNode<T> node)
{
AVLNode<T> temp = node.getLeftChild();
node.setLeftChild(leftRotation(temp));
return rightRotation(node);
}
private int getHeightDifference(AVLNode<T> node)
{
int leftHeight = -1, rightHeight = -1;
if(node.getLeftChild()!=null)
{
leftHeight = node.getLeftChild().getHeight();
}
if(node.getRightChild()!=null)
{
rightHeight = node.getRightChild().getHeight();
}
return leftHeight - rightHeight;
}
private AVLNode<T> rebalance(AVLNode<T> rootNode)
{
int diff = getHeightDifference(rootNode);
if(diff < -1)
{
if(getHeightDifference(rootNode.getRightChild())<0)
{
rootNode = leftRotation(rootNode);
}
else
{
rootNode = rightLeftRotation(rootNode);
}
}
else if(diff > 1)
{
if(getHeightDifference(rootNode.getLeftChild())>0)
{
rootNode = rightRotation(rootNode);
}
else
{
rootNode = leftRighrRotation(rootNode);
}
}
return rootNode;
}
}