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

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

  • 相关阅读:
    十分钟学会Java8:lambda表达式和Stream API
    史上最全的Spring Boot Cache使用与整合
    史上最全的Spring Boot Starter开发手册
    深入理解JAVA虚拟机(内存模型+GC算法+JVM调优)
    深入理解Mybatis技术与原理
    c++入门之命名空间存在的意义
    机器学习第一篇——最近邻kNN
    Python学习第十八篇——低耦合函数设计思想
    Python学习第十六篇——异常处理
    Python学习第十五篇——类继承和类实例化
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/8860105.html
Copyright © 2011-2022 走看看