AVL Tree中的元素比较,急~~~谢谢各位先~~~

caocao024 2009-10-14 07:20:27
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;
}

}
...全文
98 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
caocao024 2009-10-15
  • 打赏
  • 举报
回复
我绝望了~~~~
caocao024 2009-10-14
  • 打赏
  • 举报
回复
都没思路??还是我说的不清楚?

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧