zoukankan      html  css  js  c++  java
  • 二叉树的定义与前序、中序、后序遍历

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

        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("
    Inorder traversal of binary tree is ");
                tree.printInorder();
    
                Console.WriteLine("
    Postorder traversal of binary tree is ");
                tree.printPostorder();
    
    
                Console.ReadKey();
            }

    输出结果:

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

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

  • 相关阅读:
    第五章 Python——字符编码与文件处理
    第六章 Python——函数与面向过程编程
    第七章 Python——模块与包
    第一章 计算机硬件基础与操作系统介绍
    luogu P1706 全排列问题
    luogu 2142 高精度减法
    luogu P1601 高精度加法
    luogu P1803 线段覆盖 贪心
    luogu P1031 均分纸牌 贪心
    luogu P2678 跳石头 二分答案
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/8860105.html
Copyright © 2011-2022 走看看