namespace ConsoleApp49 { internal class Program { static void Main(string[] args) { BTreeDemo(); Console.WriteLine("BTree!"); } static void BTreeDemo() { BTree<int> tree = new BTree<int>(1); tree.Root.Left = new BTreeNode<int>(2); tree.Root.Right= new BTreeNode<int>(3); tree.Root.Left.Left = new BTreeNode<int>(4); tree.Root.Left.Right = new BTreeNode<int>(5); Console.WriteLine("PreOrder:"); tree.PreOrderTraversal(tree.Root); Console.WriteLine("InOrder:"); tree.InOrderTraversal(tree.Root); Console.WriteLine("PostOrder:"); tree.PostOrderTraversal(tree.Root); } } public class BTreeNode<T> { public T Value { get; set; } public BTreeNode<T> Left { get; set; } public BTreeNode<T> Right { get; set; } public BTreeNode(T value) { Value = value; Left = null; Right = null; } } public class BTree<T> { public BTreeNode<T> Root { get; set; } public BTree(T rootValue) { Root=new BTreeNode<T>(rootValue); } public void PreOrderTraversal(BTreeNode<T> node) { if(node!=null) { Console.WriteLine(node.Value); PreOrderTraversal(node.Left); PreOrderTraversal(node.Right); } } public void InOrderTraversal(BTreeNode<T> node) { if(node!=null) { InOrderTraversal(node.Left); Console.WriteLine(node.Value); InOrderTraversal(node.Right); } } public void PostOrderTraversal(BTreeNode<T> node) { if(node!=null) { PostOrderTraversal(node.Left); PostOrderTraversal(node.Right); Console.WriteLine(node.Value); } } } }
标签:node,PreOrder,BTreeNode,C#,tree,BTree,Root,public,Left From: https://www.cnblogs.com/Fred1987/p/18370256