博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二叉树的定义与前序、中序、后序遍历
阅读量:7010 次
发布时间:2019-06-28

本文共 2870 字,大约阅读时间需要 9 分钟。

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

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

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

 

你可能感兴趣的文章
Oracle 常用操作【02】数据库特性
查看>>
linux下C语言实现的内存池【转】
查看>>
理解 OpenStack 高可用(HA) (6): MySQL HA
查看>>
Linux系统的快速启动机制(内核切换) 【转】
查看>>
Python 判断文件是否存在,不存在则将名称写入指定文件
查看>>
Azkaban是什么?(一)
查看>>
AgileEAS.NET平台开发实例-药店系统-数据库还原
查看>>
android JNI 简单demo(2)它JNI demo 写
查看>>
Android 中PopupWindow使用
查看>>
你真的会玩SQL吗?EXISTS和IN之间的区别
查看>>
[Linux] 守护进程和守护线程
查看>>
PostgreSQL 语法分析中所使用的List
查看>>
颜色模式中8位,16位,24位,32位色彩是什么意思?会有什么区别?计算机颜色格式( 8位 16位 24位 32位色)【转】...
查看>>
oracle的rollup
查看>>
数据库反范式~有时候多点冗余是好事!
查看>>
PostgreSQL中的数组与Any
查看>>
如何给wordpress的编辑器添加一个自定义按钮,并且实现插入功能
查看>>
[外挂2] 鼠标单击事件
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource解决办法
查看>>
tp5自定义扩展类的使用extend
查看>>