Java 自平衡avl树

Java 自平衡avl树,java,Java,我正在尝试做一个avl树,每当树不平衡时,它都会自动更新。旋转正在工作,但我有一个错误,例如,如果树节点7、leftChild 6、leftChild 5的leftChild变为节点6、leftChild 5、rightchild 7,在平衡后我添加了一个新节点,则首先将该节点与7进行比较,而不是与6进行比较。如何解决此问题 这是主要课程: import java.io.*; import javax.swing.*; import java.util.*; import java.lang.*

我正在尝试做一个avl树,每当树不平衡时,它都会自动更新。旋转正在工作,但我有一个错误,例如,如果树节点7、leftChild 6、leftChild 5的leftChild变为节点6、leftChild 5、rightchild 7,在平衡后我添加了一个新节点,则首先将该节点与7进行比较,而不是与6进行比较。如何解决此问题

这是主要课程:

import java.io.*;
import javax.swing.*;
import java.util.*;
import java.lang.*;

public class NewMain  implements Cloneable{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {

        File file = new File ("AVLTree.txt");
        ArrayList <TreeNode> array = new ArrayList ();
        Scanner kb = new Scanner (System.in);
        int num = 0;
        TreeNode root = new TreeNode ();
        do {
            System.out.print("              AVL Tree            \n\n\n\n");
            System.out.println("1. Create a new binary tree");
            System.out.println("2. Save Tree");
            System.out.println("3. Load Tree");
            System.out.println("4. Enter a new node in the tree");
            System.out.println("5. Show current AVL tree");
            System.out.println("6. Show inorder traversal");
            System.out.println("7. Search");
            System.out.println("8. Quit  \n\n\n\n\n\n\n");
            System.out.print("Enter a number: ");
            num = kb.nextInt ();
            if (num == 1){
                if (array.isEmpty ())
                {
                    System.out.print ("Enter the root value: ");
                    int value = kb.nextInt ();
                    root = new TreeNode();
                    root.setValue(value);
                    array.add(root);
                }
                else
                {
                    array.clear();
                    System.out.print ("Enter the root value: ");
                    int value = kb.nextInt ();
                    root = new TreeNode();
                    root.setValue(value);
                    array.add(root);
                }
            }
            if (num == 2) 
            {
                    FileOutputStream outFile = null;
                    ObjectOutputStream oos = null;
                    try 
                    {
                        outFile = new FileOutputStream(file);             
                        oos = new ObjectOutputStream(outFile);
                        for (TreeNode list : array) 
                        {                 
                            oos.writeObject(list);                                      
                        }
                        oos.close();
                    }
                    catch (Exception e) 
                    {
                        System.out.print("Save Not Successful!");
                    }
               }
            if (num == 3) 
            {
                if (file.exists()) 
                {
                    FileInputStream inFile = null;
                    ObjectInputStream ios = null;
                    try 
                    {
                        Object obj = null;
                        inFile = new FileInputStream(file); 
                        ios = new ObjectInputStream(inFile); 
                        while ((obj = ios.readObject()) != null) {              
                            if (obj instanceof TreeNode) 
                            { 
                                array.add((TreeNode) obj);
                            }
                        }
                        ios.close(); 
                    } 
                    catch(EOFException e)
                    {   
                    }
                    catch (Exception e) 
                    {
                        System.out.print("File was not found while loading");
                    }
                }
            }
            if (num == 4)
            {
                System.out.print ("Enter a new child node: ");
                int value = kb.nextInt ();              
                try 
                {
                    array.add(root.insert(value));  
                    root.balance();
                }
                catch (Exception e)
                {
                    System.out.print (e.getMessage());
                }

            }
            if (num == 5){
                System.out.print ("Pointer Number\t\tLeft\t\tNode\t\tRight\t\tLeft Height\t\tRight Height\n");
                for (int i=0; i<array.size();i++)
                {                     
                      System.out.print (i+"\t\t\t"+array.indexOf(array.get(i).getLeftChild())+"\t\t"+array.get(i).getValue()+"\t\t"+array.indexOf(array.get(i).getRightChild())+"\t\t"+array.get(i).getLeftHeight()+"\t\t\t"+array.get(i).getRightHeight()+"\n");
                }
            }
            if (num == 6)
            {
                System.out.print("Inorder traversal: ");
                System.out.println(root.InOrderTraversal());
                System.out.print("Postorder traversal: ");
                System.out.println(root.PostOrderTraversal());
                System.out.print("Preorder traversal: ");
                System.out.println(root.PreOrderTraversal());
            }
            if (num == 7)
            {
                System.out.print("Enter node to be searched: ");
                int node = kb.nextInt ();
                for (int i = 0; i<array.size();i++)
                {
                    if (node == array.get(i).getValue())
                    {
                        System.out.print ("Node is in index "+i+"\n");
                        break;
                    }
                    if (i == array.size()-1 && node != array.get(i).getValue())
                    {
                        System.out.print ("Node not found in the tree!"+"\n");
                        break;
                    }
                }

            }            
        }while (num != 8);
    }
}
import java.io.*;
导入javax.swing.*;
导入java.util.*;
导入java.lang.*;
公共类NewMain实现可克隆{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args)
{
File File=新文件(“AVLTree.txt”);
ArrayList数组=新的ArrayList();
扫描仪kb=新扫描仪(System.in);
int num=0;
TreeNode root=新的TreeNode();
做{
System.out.print(“AVL树\n\n\n”);
System.out.println(“1.创建一个新的二叉树”);
System.out.println(“2.保存树”);
System.out.println(“3.加载树”);
System.out.println(“4.在树中输入一个新节点”);
System.out.println(“5.显示当前AVL树”);
System.out.println(“6.Show-inoder遍历”);
System.out.println(“7.Search”);
System.out.println(“8.Quit\n\n\n\n\n”);
System.out.print(“输入一个数字:”);
num=kb.nextInt();
如果(num==1){
if(array.isEmpty())
{
System.out.print(“输入根值:”);
int value=kb.nextInt();
根=新树节点();
root.setValue(值);
add(root);
}
其他的
{
array.clear();
System.out.print(“输入根值:”);
int value=kb.nextInt();
根=新树节点();
root.setValue(值);
add(root);
}
}
如果(num==2)
{
FileOutputStream输出文件=null;
ObjectOutputStream oos=null;
尝试
{
outFile=新文件OutputStream(文件);
oos=新对象输出流(输出文件);
用于(树节点列表:数组)
{                 
oos.writeObject(列表);
}
oos.close();
}
捕获(例外e)
{
System.out.print(“保存不成功!”);
}
}
如果(num==3)
{
if(file.exists())
{
FileInputStream infle=null;
ObjectInputStream ios=null;
尝试
{
objectobj=null;
infle=新文件输入流(文件);
ios=新的ObjectInputStream(填充);
而((obj=ios.readObject())!=null){
if(树节点的obj实例)
{ 
add((TreeNode)obj);
}
}
ios.close();
} 
捕获(EOFEException e)
{   
}
捕获(例外e)
{
System.out.print(“加载时未找到文件”);
}
}
}
如果(num==4)
{
System.out.print(“输入新的子节点:”);
int value=kb.nextInt();
尝试
{
add(root.insert(value));
根平衡();
}
捕获(例外e)
{
System.out.print(如getMessage());
}
}
如果(num==5){
System.out.print(“指针编号\t\tLeft\t\tNode\t\tRight\t\tLeft高度\t\tRight高度\n”);

对于(int i=0;i创建一个简短的单元测试,以再现问题,并在必要时启动调试器。针对问题进行单元测试至少有两个好处:它会自动运行,您不必一直输入数字,并且可以确保问题不会在以后再次出现

if (num == 4)
        {
            System.out.print ("Enter a new child node: ");
            int value = kb.nextInt ();              
            try 
            {
                array.add(root.insert(value));  
                root.balance();
            }
            catch (Exception e)
            {
                System.out.print (e.getMessage());
            }

       }

问题是,您从未使用balance方法的返回值。实际上,balance将返回树的新根。

您是否将本科作业张贴在此处?:)
if (num == 4)
        {
            System.out.print ("Enter a new child node: ");
            int value = kb.nextInt ();              
            try 
            {
                array.add(root.insert(value));  
                root.balance();
            }
            catch (Exception e)
            {
                System.out.print (e.getMessage());
            }

       }