Java AVL树遍历,搜索问题
我在AVL树实现中遇到了一些问题。。所有旋转和加法的代码似乎都是正确的,我试运行程序以彻底检查它是否在逻辑上正确运行。我的树遍历(按顺序)似乎有问题,因为它只从假定的100中输出几个整数。此外,无论我输入什么,搜索总是失败。我似乎无法理解发生了什么,但我怀疑这与一些空指针有关。下面是AVL树的代码,我想知道AddNode方法或rotation方法中是否有任何不正确的代码,但它们似乎没有问题。。这些类包括节点类、AVL类和AVL树类,AVL树类是主类 节点类Java AVL树遍历,搜索问题,java,avl-tree,Java,Avl Tree,我在AVL树实现中遇到了一些问题。。所有旋转和加法的代码似乎都是正确的,我试运行程序以彻底检查它是否在逻辑上正确运行。我的树遍历(按顺序)似乎有问题,因为它只从假定的100中输出几个整数。此外,无论我输入什么,搜索总是失败。我似乎无法理解发生了什么,但我怀疑这与一些空指针有关。下面是AVL树的代码,我想知道AddNode方法或rotation方法中是否有任何不正确的代码,但它们似乎没有问题。。这些类包括节点类、AVL类和AVL树类,AVL树类是主类 节点类 private int data; p
private int data;
private Node left;
private Node right;
private int height;
public Node(int m) {
data = m;
left = null;
right = null;
height = 0;
}
public void setToleft(Node newleft) {
left = newleft;
}
public Node getleftNode() {
return left;
}
public void setToright(Node newright) {
right = newright;
}
public Node getrightNode() {
return right;
}
public int getData() {
return data;
}
public int getHeight(){
return height;
}
public void setHeight(int height){
this.height = height;
}
AVL类
public Node root;
public AVL(int root) {
this.root = new Node(root); // since root presently has no left or right children, height is currently 0
}
public int Height(Node n) {
if (n == null) { //basis step
return -1;
} else { //add one for every path
if (n.getleftNode() == null && n.getrightNode() == null) {
return 0;
}
return 1 + Math.max(Height(n.getleftNode()), Height(n.getrightNode()));
}
}
public void add(int data) {
addNode(data, root);
root.setHeight(Math.max(Height(root.getleftNode()), Height(root.getrightNode())) + 1);
}
public void addNode(int data, Node n) {
if (data < n.getData()) {
if (n.getleftNode() == null) {
n.setToleft(new Node(data));
} else {
addNode(data, n.getleftNode());
}
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
if ((Height(n.getleftNode()) + 1) - (Height(n.getrightNode()) + 1) == Math.abs(2)) {
if (data < n.getleftNode().getData()) {
n = LLRotation(n);
} else {
n = LRRotation(n);
}
}
} else if (data >= n.getData()) { //>= also caters for duplicates and inserts them infront of same value
if (n.getrightNode() == null) {
n.setToright(new Node(data));
} else {
addNode(data, n.getrightNode());
}
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
if ((Height(n.getrightNode()) + 1) - (Height(n.getleftNode()) + 1) == Math.abs(2)) {
if (data >= n.getrightNode().getData()) {
n = RRRotation(n);
} else {
n = RLRotation(n);
}
}
}
}
public Node LLRotation(Node n) { //single
Node n1 = n.getleftNode();
n.setToleft(n1.getrightNode());
n1.setToright(n);
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
n1.setHeight(Math.max(Height(n1.getleftNode()), Height(n)) + 1);
//compares heights of left and right subtrees and gets max
//the above source code is of course vital since the node height must be resetted after rotations
//adding 1 at the end of the last two code lines is important since
//initially the height is only calculated from subtrees onwards
//same for single right rotation below
return n1;
}
public Node RRRotation(Node n) { //single
Node n1 = n.getrightNode();
n.setToright(n1.getleftNode());
n1.setToleft(n);
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
n1.setHeight(Math.max(Height(n1.getrightNode()), Height(n)) + 1);
return n1;
}
public Node LRRotation(Node n) { //double
n.setToleft(RRRotation(n.getleftNode()));
return LLRotation(n);
}
public Node RLRotation(Node n) { //double
n.setToright(LLRotation(n.getrightNode()));
return RRRotation(n);
}
public void inOrderTraversal(Node n) {
if (n != null) {
inOrderTraversal(n.getleftNode()); //recursive call to the left subtree
System.out.println(n.getData()); //line which makes the actual node to display its data
inOrderTraversal(n.getrightNode()); //recursive call to the right subtree
}
}
public void traverse() {
inOrderTraversal(root); // can be called in main class to automatically traverse tree from its root
}
public int search(int x) {
try {
if (x == root.getData()) { //basis step
System.out.println("Item found!");
return x;
}
if (x < root.getData()) {
root = root.getleftNode();
return search(x);//recursive call
} else {
root = root.getrightNode();
return search(x);//recursive call
}
} catch (NullPointerException e) {
System.out.println ("Search failed!");
return 0;
}
}
公共节点根;
公共AVL(整数根){
this.root=新节点(root);//由于root当前没有左或右子节点,因此高度当前为0
}
公共整数高度(节点n){
如果(n==null){//基步
返回-1;
}else{//为每个路径添加一个
if(n.getleftNode()==null&&n.getrightNode()==null){
返回0;
}
返回1+Math.max(高度(n.getleftNode()),高度(n.getrightNode());
}
}
公共无效添加(整型数据){
addNode(数据,根);
setHeight(Math.max(Height(root.getleftNode())、Height(root.getrightNode())+1);
}
公共void addNode(int数据,节点n){
if(数据=n.getData()){/>=也会处理重复项并将它们插入到相同值的前面
如果(n.getrightNode()==null){
n、 setToright(新节点(数据));
}否则{
addNode(数据,n.getrightNode());
}
n、 setHeight(Math.max(Height(n.getleftNode())、Height(n.getrightNode())+1);
如果((高度(n.getrightNode())+1)-(高度(n.getleftNode())+1)==Math.abs(2)){
如果(数据>=n.getrightNode().getData()){
n=旋转(n);
}否则{
n=旋转(n);
}
}
}
}
公共节点(节点n){//single
节点n1=n.getleftNode();
n、 setToleft(n1.getrightNode());
n1.设置灯(n);
n、 setHeight(Math.max(Height(n.getleftNode())、Height(n.getrightNode())+1);
n1.setHeight(数学最大值(Height(n1.getleftNode()),Height(n))+1);
//比较左右子树的高度并获得最大值
//上述源代码当然至关重要,因为旋转后必须重置节点高度
//在最后两行代码末尾添加1非常重要,因为
//最初,仅从子树开始计算高度
//下面的单次右旋转相同
返回n1;
}
公共节点RRRotation(节点n){//single
节点n1=n.getrightNode();
n、 setToright(n1.getleftNode());
n1.setToleft(n);
n、 setHeight(Math.max(Height(n.getleftNode())、Height(n.getrightNode())+1);
n1.setHeight(数学最大值(Height(n1.getrightNode()),Height(n))+1);
返回n1;
}
公共节点(节点n){//double
n、 setToleft(RRRotation(n.getleftNode());
返回旋转(n);
}
公共节点旋转(节点n){//double
n、 setToright(LLRotation(n.getrightNode());
返回旋转(n);
}
OrderTraversal中的公共void(节点n){
如果(n!=null){
inOrderTraversal(n.getleftNode());//对左子树的递归调用
System.out.println(n.getData());//使实际节点显示其数据的行
inOrderTraversal(n.getrightNode());//对右子树的递归调用
}
}
公共空间遍历(){
inOrderTraversal(root);//可以在主类中调用,以从树的根自动遍历树
}
公共整数搜索(整数x){
试一试{
如果(x==root.getData()){//基本步骤
System.out.println(“找到项!”);
返回x;
}
if(x
主类
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
AVL tree = null;
int choice = 0;
System.out.println("AVL TREE");
System.out.println("\n Choose an option from the menu: ");
System.out.println("\n\t 1.) Create file of 100 integers");
System.out.println("\n\t 2.) Create the tree");
System.out.println("\n\t 3.) In-Order traverse and show tree");
System.out.println("\n\t 4.) Search for integer");
System.out.println("\n\t 5.) Quit");
while (choice != 5) {
System.out.print("\nChoice: ");
choice = s.nextInt();
switch (choice) {
case 1:
createFile();
break;
case 2:
try {
FileReader readto = new FileReader("Integers.txt");
BufferedReader br = new BufferedReader(readto);
String line = br.readLine(); //reads text at start of file
line = br.readLine(); // skipping empty lines
line = br.readLine();
line = br.readLine();
int root = Integer.parseInt(line); //extracts first integer from the line
System.out.println("Root: " + root);
tree = new AVL(root);
int x = 0;
while (x != 99) {
try {
line = br.readLine();
int next = Integer.parseInt(line);
tree.add(next);
System.out.println(next);
x++;
} catch (NumberFormatException e) {
};
}
System.out.println("Tree successfully populated!");
} catch (FileNotFoundException e) {
System.out.println("ERROR: File not found!");
}
break;
case 3:
System.out.println("In-Order traversel executed. The now balanced tree shall now be printed in");
System.out.println("ascending order and also the left and right children of each node shall be printed.\n");
System.out.println("Traversal: ");
tree.traverse();
break;
case 4:
System.out.print("Please enter the integer to be searched: ");
int x = s.nextInt();
System.out.println(tree.search(x));
break;
case 5:
System.exit(0);
break;
default:
System.out.println("ERROR: Choice out of bounds!");
}
}
}
static void createFile() throws IOException {
Random r = new Random();
File intfile = new File("Integers.txt");
FileWriter writeto = new FileWriter("Integers.txt");
BufferedWriter bw = new BufferedWriter(writeto);
if (!(intfile.exists())) {
System.out.println("ERROR: File not found!");
} else {
bw.write("The following integers are randomly generated");
bw.newLine();
bw.write("and will be used to construct the AVL tree:");
bw.newLine();
bw.newLine();
int x;
System.out.println("The following random numbers shall be used to build the AVL tree: \n");
for (int i = 0; i < 100; i++) {
x = r.nextInt(100) + 1;
bw.write(String.valueOf(x));
bw.newLine();
System.out.println(x);
}
bw.close();
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
扫描仪s=新的扫描仪(System.in);
AVL树=空;
int-choice=0;
System.out.println(“AVL树”);
System.out.println(“\n从菜单中选择一个选项:”);
System.out.println(“\n\t 1.)创建100个整数的文件”);
System.out.println(“\n\t 2.)创建树”);
System.out.println(“\n\t 3.)顺序遍历并显示树”);
System.out.println(“\n\t 4.)搜索整数”);
System.out.println(“\n\t 5.)Quit”);
while(选项!=5){
系统输出打印(“\n提示:”);
choice=s.nextInt();
开关(选择){
案例1:
createFile();
打破
案例2:
试一试{
FileReader readto=新文件阅读器(“Integers.txt”);
BufferedReader br=新的BufferedReader(readto);
String line=br.readLine();//读取文件开头的文本
line=br.readLine();//跳过空行
line=br.readLine();
线=
int search(Node node, int key) {
if (node == null) {
return 0; // missing from tree
} else if (key < node.getData()) {
return search(node.getLeft(), key);
} else if (key > node.getData()) {
return search(node.getRight(), key);
} else {
return node.getData(); // found it
}
}
public int search(int key) {
return searchNode(root, key);
}
private int searchNode(Node node, int key) {
// Perform the recursive search, as above
}