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

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

复制代码
    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

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

 



感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接
posted @   cctext  阅读(541)  评论(0编辑  收藏  举报
编辑推荐:
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!
点击右上角即可分享
微信分享提示