空异常错误

时间:2015-03-04 00:12:54

标签: c# argumentnullexception

这是我为算法类​​所做的二叉树程序的代码。我不断抛出一个null异常错误,但我不知道是什么导致它。

namespace Tree
{
    class CallTree
    {
        static void Main(string[] args)
        {

            BNode HE = new BNode(0, "House Entrance");

            BNode LowerHallway = new BNode(0, "Lower Hallway");
            BNode UpperHallway = new BNode(0, "Upper Hallway");
            HE.setLeft(LowerHallway);
            HE.setRight(UpperHallway);

            BNode Lounge = new BNode(0, "Lounge");
            BNode Kitchen = new BNode(0, "Kitchen");
            LowerHallway.setLeft(Lounge);
            LowerHallway.setRight(Kitchen);

            BNode Balcony = new BNode(0, "Balcony");
            Kitchen.setRight(Balcony);

            BNode Study = new BNode(0, "Study");
            BNode MasterBedroom = new BNode(0, "Master Bedroom");
            UpperHallway.setLeft(Study);
            UpperHallway.setRight(MasterBedroom);

            BNode GuestBath = new BNode(0, "Guest Bath");
            BNode GuestBedroom = new BNode(0, "Guest Bedroom");
            Study.setLeft(GuestBath);
            Study.setRight(GuestBedroom);

            BNode PrivateBath = new BNode(0, "Private Bath");
            BNode Closet = new BNode(0, "Closet");
            MasterBedroom.setLeft(PrivateBath);
            MasterBedroom.setRight(Closet);

            HBinaryTree HBinaryTree = new HBinaryTree(HE);
            BNode rootNode = HBinaryTree.GetRoot();

我在这里得到'rootNode'的例外V

            HBinaryTree.preOrder(rootNode);
            //HBinaryTree.inOrder(rootNode);
            //HBinaryTree.postOrder(rootNode);

            Console.ReadKey();
        }
    }
    //definition of node in a binary tree
    public class BNode
    {
        public string room;
        public int treasure;

        public BNode left, right;//left child and right child

        public BNode(int item, string room)
        {
            treasure = item;
            left = null;
            right = null;
        }

        public BNode(int item, string room, BNode leftNode, BNode rightNode)
        {
            treasure = item;
            left = leftNode;
            right = rightNode;
        }

        public void show()
        {
            Console.Write(treasure);
        }
        //Is it interial node?
        public bool isInner()
        {
            return left != null || right != null;
        }
        //Is it a leaf node?

        public bool isLeaf()
        {

            return left == null && right == null;
        }

        //Does it have a left child?
        public bool hasLeft()
        {
            return left != null;
        }

        //Does it have a right child?
        public bool hasRight()
        {
            return right != null;
        }
        //Set its left child to be newLeft
        public void setLeft(BNode newLeft)
        {
            left = newLeft;
        }

        //Set its right child to be newRight
        public void setRight(BNode newRight)
        {
            right = newRight;
        }

        //return data value
        public int getValue()
        {
            return treasure;
        }

        //set data value
        public void setValue(int newValue)
        {
        treasure = newValue;
        }

    }

    //definition of a proper binary tree
    class HBinaryTree
    {

        public BNode root;  //root of the tree

        public HBinaryTree()
        {
            root = null;
        }

        public BNode GetRoot()
        {
            return root;
        }

        public HBinaryTree(BNode rootNode) // constructor
        {
            root = rootNode;
        }
        // PreOrder traversal
        public void preOrder(BNode root)
        {

这4行中的'root'有更多异常错误V

            root.show();
            if (root.isInner())
            {
                preOrder(root.left);
                preOrder(root.right);
            }

        }

        //// InOrder traversal
        //public void inOrder(BNode root)
        //{

        //    if (root.isInner())
        //    {
        //        inOrder(root.left);
        //    }
        //    root.show();
        //    if (root.isInner())
        //    {
        //        inOrder(root.right);
        //    }
        //}

        //PostOrder traversal
        //public void postOrder(BNode root)
        //{

        //    if (root.isInner())
        //    {
        //        postOrder(root.left);
        //        postOrder(root.right);
        //    }
        //    root.show();
        //}
   }
}

如果有人可以帮我推断出导致'root'和'rootNode'异常的原因,我希望如此。它声明它们是空的,但我不明白为什么它说明了这一点。

1 个答案:

答案 0 :(得分:3)

如果preOrder返回true,则isInner函数以递归方式调用树中每个节点的左右节点。如果其任一节点非空,则isInner返回true,但您的厨房只有一个右侧分支,而其左侧为空。因此,当该函数到达厨房时,它会看到isInner为真,并且它将自己的左分支从厨房调用为空。您需要单独检查左和右的空值。