C#如何使该程序运行?数组问题

时间:2018-10-19 10:54:17

标签: c# arrays binary-search-tree traversal

我应该获得遍历顺序二元搜索树的遍历顺序InOrder,PreOrder和PostOrder遍历的输出。 我只是不知道如何使用Array来运行该程序。

class Node
{
    public int[] item = new int[11];
    public Node left;
    public Node right;
    public void display()
    {
        Console.Write("[");
        Console.Write(item);
        Console.Write("]");
    }
}

class Tree
{
    public Node root;

    public Tree()
    {
        root = null;
    }

    public Node Returnroot()
    {
        return root;
    }

    public void Insert(int id)
    {
        Node newnode = new Node();
        newnode.item[11] = id;
        if (root == null)
            root = newnode;
        else
        {
            Node current = root;
            Node parent;
            while (true)
            {
                parent = current;
                if (id < current.item[11])
                {
                    current = current.left;
                        if (current == null)
                        {
                            parent.left = newnode;
                            return;
                        }
                }
                else
                {
                    current = current.right;
                    if (current == null)
                    {
                        parent.right = newnode;
                        return;
                    }
                }
            }
        }
    }

    public void Inorder(Node Root)
    {
        if (Root != null)
        {
            Inorder(Root.left);
            Console.WriteLine(Root.item[11] + " ");
            Inorder(Root.right);
        }
    }

    public void Preorder(Node Root)
    {
        if (Root != null)
        {
            Console.WriteLine(Root.item[11] + " ");
            Preorder(Root.left);
            Preorder(Root.right);
        }
    }

    public void Postorder(Node Root)
    {
        if (Root != null)
        {
            Postorder(Root.left);
            Postorder(Root.right);
            Console.WriteLine(Root.item[11] + " ");
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Tree BST = new Tree();
        BST.Insert(30);
        BST.Insert(35);
        BST.Insert(57);
        BST.Insert(15);
        BST.Insert(63);
        BST.Insert(49);
        BST.Insert(89);
        BST.Insert(77);
        BST.Insert(67);
        BST.Insert(98);
        BST.Insert(91);
        Console.WriteLine("inOrder Traversal :  ");
        BST.Inorder(BST.Returnroot());
        Console.WriteLine("  ");
        Console.WriteLine();
        Console.WriteLine("PreOrder Traversal :  ");
        BST.Preorder(BST.Returnroot());
        Console.WriteLine("  ");
        Console.WriteLine();
        Console.WriteLine("PostOrder Traversal :  ");
        BST.Postorder(BST.Returnroot());
        Console.WriteLine("  ");
        Console.WriteLine();

        Console.ReadKey();
    }
}

我的问题是如何使用数组运行此代码,这对我来说非常复杂,我尝试了多种方法来使此运行仍未运行。我通常会跳出数组错误。所以也许我怎么把数组放错了?我应该如何在此程序中声明数组?

2 个答案:

答案 0 :(得分:1)

System.IndexOutOfRangeException类的Tree方法中,您遇到了Insert错误(第39行)。您试图将id放在newnode.item数组(newnode.item[11] = id;)的第12个元素中,但是newnode.item数组的大小仅是第11个元素的大小。如果要将此id放置为第11个元素,则必须放置newnode.item[10] = id;,因为数组元素的索引是从0到n,而不是从1到n。

您必须在所有代码上将someArray.item[11]更改为someArray.item[10]

但是,如果您想加入newnode.item[11],则必须将Node类从public int[] item = new int[11];更改为public int[] item = new int[12];

答案 1 :(得分:0)

正如我在评论中已经说过的那样,C#数组基于0。
这意味着当您将数组声明为

int[] item = new int[11];

11th 元素是item[10],而item[11]超出范围。

item[11]替换所有达到item[10]的尝试会导致:

using System.IO;
using System;

class Node
{
    public int[] item = new int[11];
    public Node left;
    public Node right;
    public void display()
    {
        Console.Write("[");
        Console.Write(item);
        Console.Write("]");
    }
}

class Tree
{
    public Node root;

    public Tree()
    {
        root = null;
    }

    public Node Returnroot()
    {
        return root;
    }

    public void Insert(int id)
    {
        Node newnode = new Node();
        newnode.item[10] = id; // HERE (1/5)
        if (root == null)
            root = newnode;
        else
        {
            Node current = root;
            Node parent;
            while (true)
            {
                parent = current;
                if (id < current.item[10]) // HERE (2/5)
                {
                    current = current.left;
                        if (current == null)
                        {
                            parent.left = newnode;
                            return;
                        }
                }
                else
                {
                    current = current.right;
                    if (current == null)
                    {
                        parent.right = newnode;
                        return;
                    }
                }
            }
        }
    }

    public void Inorder(Node Root)
    {
        if (Root != null)
        {
            Inorder(Root.left);
            Console.WriteLine(Root.item[10] + " "); // HERE (3/5)
            Inorder(Root.right);
        }
    }

    public void Preorder(Node Root)
    {
        if (Root != null)
        {
            Console.WriteLine(Root.item[10] + " "); // HERE (4/5)
            Preorder(Root.left);
            Preorder(Root.right);
        }
    }

    public void Postorder(Node Root)
    {
        if (Root != null)
        {
            Postorder(Root.left);
            Postorder(Root.right);
            Console.WriteLine(Root.item[10] + " "); // HERE (5/5)
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Tree BST = new Tree();
        BST.Insert(30);
        BST.Insert(35);
        BST.Insert(57);
        BST.Insert(15);
        BST.Insert(63);
        BST.Insert(49);
        BST.Insert(89);
        BST.Insert(77);
        BST.Insert(67);
        BST.Insert(98);
        BST.Insert(91);
        Console.WriteLine("inOrder Traversal :  ");
        BST.Inorder(BST.Returnroot());
        Console.WriteLine("  ");
        Console.WriteLine();
        Console.WriteLine("PreOrder Traversal :  ");
        BST.Preorder(BST.Returnroot());
        Console.WriteLine("  ");
        Console.WriteLine();
        Console.WriteLine("PostOrder Traversal :  ");
        BST.Postorder(BST.Returnroot());
        Console.WriteLine("  ");
        Console.WriteLine();

        Console.ReadKey();
    }
}

通往输出:

inOrder Traversal :  
15 
30 
35 
49 
57 
63 
67 
77 
89 
91 
98 


PreOrder Traversal :  
30 
15 
35 
57 
49 
63 
89 
77 
67 
98 
91 


PostOrder Traversal :  
15 
49 
67 
77 
91 
98 
89 
63 
57 
35 
30 

这里也不例外。