空异常错误

Null Exception Errors

这是我为算法 class 编写的二叉树程序的代码。我不断收到空异常错误,但我不知道是什么原因造成的。

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' 抛出异常。它说它们是空的,但我不明白为什么它这么说。

如果 isInner returns 为真,您的 preOrder 函数会递归地为树中每个节点的左右节点调用自身。 isInner returns 如果其中一个节点不为空,则为真,但是您的厨房只有一个右分支,而它的左分支为空。因此,当该函数到达厨房时,它发现 isInner 为真,并且它使用厨房的左侧分支调用自身,该分支为空。您需要分别检查左侧和右侧是否为 null。