二叉树的定义与前序、中序、后序遍历

二叉树定义(递归方式):

    class Node
    {
        public int Key;
        public Node Left, Right;

        public Node(int item)
        {
            Key = item;
            Left = Right = null;
        }
    }

    class BinaryTree
    {
        // Root of Binary Tree
        public Node Root;

        public BinaryTree()
        {
            Root = null;
        }

        /* Given a binary tree, print its nodes according to the "bottom-up" postorder traversal. */
        public void printPostorder(Node node)
        {
            if (node == null)
                return;

            // first recur on left subtree
            printPostorder(node.Left);

            // then recur on right subtree
            printPostorder(node.Right);

            // now deal with the node
            Console.WriteLine(node.Key + " ");
        }

        /* Given a binary tree, print its nodes in inorder*/
        public void printInorder(Node node)
        {
            if (node == null)
                return;

            /* first recur on left child */
            printInorder(node.Left);

            /* then print the data of node */
            Console.WriteLine(node.Key + " ");

            /* now recur on right child */
            printInorder(node.Right);
        }

        /* Given a binary tree, print its nodes in preorder*/
        public void printPreorder(Node node)
        {
            if (node == null)
                return;

            /* first print data of node */
            Console.WriteLine(node.Key + " ");

            /* then recur on left sutree */
            printPreorder(node.Left);

            /* now recur on right subtree */
            printPreorder(node.Right);
        }

        // Wrappers over above recursive functions
        public void printPostorder() { printPostorder(Root); }
        public void printInorder() { printInorder(Root); }
        public void printPreorder() { printPreorder(Root); }
    }

前、中、后序的遍历:

        /**
         * 二叉树
         */
        static void Main(string[] args)
        {
            /**
             *             1
             *            / \
             *          2    3
             *         / \
             *        4   5
             * **/
            BinaryTree tree = new BinaryTree();
            tree.Root = new Node(1);
            tree.Root.Left = new Node(2);
            tree.Root.Right = new Node(3);
            tree.Root.Left.Left = new Node(4);
            tree.Root.Left.Right = new Node(5);

            Console.WriteLine("Preorder traversal of binary tree is ");
            tree.printPreorder();

            Console.WriteLine("\nInorder traversal of binary tree is ");
            tree.printInorder();

            Console.WriteLine("\nPostorder traversal of binary tree is ");
            tree.printPostorder();


            Console.ReadKey();
        }

输出结果:

还有使用非递归的方式实现遍历的方式,以及删除节点等处理请参考:https://blog.csdn.net/sinat_36246371/article/details/53351204

后序再研究,目前记不清。。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值