打印一个splay树但出了什么问题?

时间:2015-04-04 09:13:25

标签: java splay-tree

我正在尝试打印一棵树,但我无法弄清楚我错在哪里。 displayTree方法适用于BST和AVL,但不适用于splay树。任何人都可以帮我这个吗?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;


public class TreeTester<AnyType> {

    private ArrayList<Integer> RandomAL;
    private int[] randomArray;
    private Random R;

    int emptyLeaf;
    public TreeTester(int x)
    {
        RandomAL = new ArrayList<Integer>();

        while(RandomAL.size() < (int)Math.pow(2, x)-1)
        {
            RandomAL = new ArrayList<Integer>();
            R  = new Random();
            randomArray = new int[(int)Math.pow(2, x)-1];

            int i = 0;
            while(RandomAL.size() < (int)Math.pow(2, x)-1)
            {
                randomArray[i] = R.nextInt(89) + 10;
                if(!RandomAL.contains(randomArray[i]))
                {
                    //randomArray.add(randomArray[i]);
                    RandomAL.add(randomArray[i]);
                    //System.out.println( "R: " + BST.get(i));
                    i++;
                    //System.out.println( "y: " + i + "BST.size()" + BST.size());
                }
            }
        }
    }

    public ArrayList<Integer> getRandom()
    {
        return RandomAL;
    }

    public void displayTree(int depth, TreeNode<AnyType> root)
    {
        Stack stack = new Stack();
        stack.push(root);

        int emptyLeaf = (int)Math.pow(2, depth);
        boolean isRowEmpty = false;

        while(isRowEmpty==false)
        {
            Stack innerstack = new Stack();
            isRowEmpty = true;
            for(int j=0; j<emptyLeaf; j++)
                System.out.print(" ");
            while(stack.isEmpty()==false)
            {
                TreeNode<AnyType> temp = (TreeNode<AnyType>) stack.pop();
                if(temp != null)
                {
                    System.out.print(temp.element);
                    innerstack.push(temp.left);
                    innerstack.push(temp.right);
                    if(temp.left != null ||temp.right != null)
                        isRowEmpty = false;
                }
                else
                {
                    System.out.print("--");
                    innerstack.push(null);
                    innerstack.push(null);
                }
                for(int j=0; j<emptyLeaf*2-2; j++)
                    System.out.print(' ');
            }
            System.out.println();
            emptyLeaf /= 2;
            while(innerstack.isEmpty()==false)
                stack.push( innerstack.pop() );                 

        }


    }

    public static void main(String[] args) throws FileNotFoundException
    {
        String UserChioce = "";
        String ItemName = "";
        int ItemQuantity = 0;
        String space = "";
        int ItemPrice = 0;

        TreeTester t = new TreeTester(4);
        //pa1 x = new pa1();

        File inputtext = new File("input.txt");
        Scanner input = new Scanner(inputtext);

        ArrayList<Integer> Random = t.getRandom();

        /*
        for(int i=0; i<Random.size(); i++)
        {
            System.out.println(Random.get(i));
        }
         */

        BinarySearchTree BST = new BinarySearchTree();
        System.out.println( BST.isEmpty() );

        for(int i=0; i<Random.size(); i++)
        {
            BST.insert(Random.get(i));
        }
        System.out.println( BST.isEmpty() );
        System.out.println( BST.getcounter() );


        t.displayTree(BST.counter, BST.getroot());

        AvlTree AVL = new AvlTree();
        System.out.println( AVL.isEmpty() );

        for(int i=0; i<Random.size(); i++)
        {
            AVL.insert(Random.get(i));
        }
        System.out.println( AVL.isEmpty() );
        System.out.println( AVL.getcounter() );


        t.displayTree(AVL.counter, AVL.getroot());

        SplayTree Splay = new SplayTree();
        System.out.println( Splay.isEmpty() );

        for(int i=0; i<Random.size(); i++)
        {
            Splay.insert(Random.get(i));
            System.out.println( Random.get(i) );

        }
        System.out.println( Splay.isEmpty() );
        System.out.println( Splay.getcounter() );

        System.out.println( Random.size() );
        System.out.println( Splay.countNodes() );
        t.displayTree(3, Splay.getroot());
        System.out.println( Splay.getroot().element );
    }
}

这是我的SplayTree:

/**
 *  Java Program to Implement SplayTree
 **/

 import java.util.Scanner;

 /** Class Node **/
 class SplayNode<AnyType> extends TreeNode
 {    
     SplayNode<AnyType> left, right, parent;
     int element;

     /** Constructor **/
     public SplayNode()
     {
         this(0, null, null, null);
     }          
     /** Constructor **/
     public SplayNode( int theElement )
     {
         this( theElement, null, null, null); 
     } 
     /** Constructor **/
     public SplayNode( int i, SplayNode<AnyType> left, SplayNode<AnyType> right, SplayNode<AnyType> parent)
     {
         super( i, left, right );
         this.left = left;
         this.right = right;
         this.parent = parent;
         this.element = i;         
     }    
 }

 /** Class SplayTree 
 * @param <AnyType>**/
 class SplayTree<AnyType>
 {
     public int counter;
     public int localcounter;

     private SplayNode<AnyType> root;
     private int count = 0;

     /** Constructor **/
     public SplayTree()
     {
         counter = 0;
         localcounter = 0;
         root = null;
     }

     public SplayNode<AnyType> getroot()
     {
        return root;
     }

     public int getcounter()
     {
        return counter;
     }
     /** Function to check if tree is empty **/
     public boolean isEmpty()
     {
         return root == null;
     }

     /** clear tree **/
     public void clear()
     {
         root = null;
     }

     /** function to insert element */
     public void insert(int ele)
     {
         localcounter = 0;
         SplayNode z = root;
         SplayNode p = null;
         while (z != null)
         {
             p = z;
             if (ele < p.element)
             {
                 z = z.right;
             }
             else
             {
                 z = z.left;
             }
         }
         z = new SplayNode();
         z.element = ele;
         z.parent = p;
         if (p == null)
         {
             root = z;
             localcounter += 1;
            if(counter < localcounter)
            {
                counter = localcounter;
                System.out.println("counter: " + counter);
            }
         }
         else if (ele < p.element)
         {
             p.right = z;
             localcounter += 1;
            if(counter < localcounter)
            {
                counter = localcounter;
                System.out.println("counter: " + counter);
            }
         }
         else
         {
             p.left = z;
             localcounter += 1;
            if(counter < localcounter)
            {
                counter = localcounter;
                System.out.println("counter: " + counter);
            }
         }
         Splay(z);
         count++;
     }
     /** rotate **/
     public void makeLeftChildParent(SplayNode c, SplayNode p)
     {
         if ((c == null) || (p == null) || (p.left != c) || (c.parent != p))
             throw new RuntimeException("WRONG");

         if (p.parent != null)
         {
             if (p == p.parent.left)
                 p.parent.left = c;
             else 
                 p.parent.right = c;
         }
         if (c.right != null)
             c.right.parent = p;

         c.parent = p.parent;
         p.parent = c;
         p.left = c.right;
         c.right = p;
     }

     /** rotate **/
     public void makeRightChildParent(SplayNode c, SplayNode p)
     {
         if ((c == null) || (p == null) || (p.right != c) || (c.parent != p))
             throw new RuntimeException("WRONG");
         if (p.parent != null)
         {
             if (p == p.parent.left)
                 p.parent.left = c;
             else
                 p.parent.right = c;
         }
         if (c.left != null)
             c.left.parent = p;
         c.parent = p.parent;
         p.parent = c;
         p.right = c.left;
         c.left = p;
     }

     /** function splay **/
     private void Splay(SplayNode x)
     {
         while (x.parent != null)
         {
             SplayNode Parent = x.parent;
             SplayNode GrandParent = Parent.parent;
             if (GrandParent == null)
             {
                 if (x == Parent.left)
                     makeLeftChildParent(x, Parent);
                 else
                     makeRightChildParent(x, Parent);                 
             } 
             else
             {
                 if (x == Parent.left)
                 {
                     if (Parent == GrandParent.left)
                     {
                         makeLeftChildParent(Parent, GrandParent);
                         makeLeftChildParent(x, Parent);
                     }
                     else 
                     {
                         makeLeftChildParent(x, x.parent);
                         makeRightChildParent(x, x.parent);
                     }
                 }
                 else 
                 {
                     if (Parent == GrandParent.left)
                     {
                         makeRightChildParent(x, x.parent);
                         makeLeftChildParent(x, x.parent);
                     } 
                     else 
                     {
                         makeRightChildParent(Parent, GrandParent);
                         makeRightChildParent(x, Parent);
                     }
                 }
             }
         }
         root = x;
     }

     /** function to remove element **/
     public void remove(int ele)
     {
         SplayNode node = findNode(ele);
        remove(node);
     }

     /** function to remove node **/
     private void remove(SplayNode node)
     {
         if (node == null)
             return;

         Splay(node);
         if( (node.left != null) && (node.right !=null))
         { 
             SplayNode min = node.left;
             while(min.right!=null)
                 min = min.right;

             min.right = node.right;
             node.right.parent = min;
             node.left.parent = null;
             root = node.left;
         }
         else if (node.right != null)
         {
             node.right.parent = null;
             root = node.right;
         } 
         else if( node.left !=null)
         {
             node.left.parent = null;
             root = node.left;
         }
         else
         {
             root = null;
         }
         node.parent = null;
         node.left = null;
         node.right = null;
         node = null;
         count--;
     }

     /** Functions to count number of nodes **/
     public int countNodes()
     {
         return count;
     }

     /** Functions to search for an element **/
     public boolean search(int val)
     {
         return findNode(val) != null;
     }
     private SplayNode findNode(int ele)
     {
         SplayNode z = root;
         while (z != null)
         {
             if (ele < z.element)
                 z = z.right;
             else if (ele > z.element)
                 z = z.left;
             else
                 return z;
         }
         return null;
     }

     /** Function for inorder traversal **/ 
     public void inorder()
     {
         inorder(root);
     }
     private void inorder(SplayNode r)
     {
         if (r != null)
         {
             inorder(r.left);
             System.out.print(r.element +" ");
             inorder(r.right);
         }
     }

     /** Function for preorder traversal **/
     public void preorder()
     {
         preorder(root);
     }
     private void preorder(SplayNode r)
     {
         if (r != null)
         {
             System.out.print(r.element +" ");
             preorder(r.left);             
             preorder(r.right);
         }
     }

     /** Function for postorder traversal **/
     public void postorder()
     {
         postorder(root);
     }
     private void postorder(SplayNode r)
     {
         if (r != null)
         {
             postorder(r.left);             
             postorder(r.right);
             System.out.print(r.element +" ");
         }
     }     
 }

这是我的TreeNode:

package TreeTest;

public class TreeNode<AnyType>
{
    TreeNode(AnyType element)
    {
        this(element, null, null);
    }

    TreeNode(AnyType element, TreeNode<AnyType> lt, TreeNode<AnyType> rt)
    {
        this.element = element; 
        left = lt; 
        right = rt;
    }

    AnyType element; // The data in the node
    TreeNode<AnyType> left; // Left child
    TreeNode<AnyType> right; // Right child
}

这是我的结果:

true
counter: 1
counter: 2
counter: 3
counter: 4
counter: 5
counter: 6
false
6
                                                                45                                                                                                                              
                                21                                                              58                                                              
                --                              23                              54                              70                              
        --              --              --              40              50              --              62              85              
    --      --      --      --      --      --      35      --      --      --      --      --      61      --      72      --      
  --  --  --  --  --  --  --  --  --  --  --  --  27  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  76  --  --  
true
counter: 1
counter: 2
counter: 3
counter: 4
counter: 5
counter: 6
false
6
                                                                58                                                                                                                              
                                45                                                              70                                                              
                23                              54                              62                              76                              
        21              35              50              --              61              --              72              85              
    --      --      27      40      --      --      --      --      --      --      --      --      --      --      --      --      
true
counter: 1
45
58
70
62
21
23
54
85
40
50
61
72
35
76
27
false
1
15
15
        0              
27

0 个答案:

没有答案