二叉树遍历
很久没研究算法了,都陌生得很了,现在工作了,觉得要提高自己的水平,光是完成工作,学习新的知识是不够的,要多研究算法,要多磨练自己的思维能力逻辑能力,多做题,不能让自己的脑袋生锈……
从今天开始,坚持写写一些算法的C#实现,先从二叉树的遍历开始:

1 //======================================================================
2 //
3 // copyright (C) 2010 All rights reserved
4 // framework : 3.5
5 // filename : Program
6 // description :
7 // author : marvin(马非马)
8 // company : Sysu.im.06imis
9 // create time : 2010-8-10 19:01:14
10 //
11 //======================================================================
12 using System;
13 using System.Collections.Generic;
14 using System.Linq;
15 using System.Text;
16
17 namespace AlgorithmTest
18 {
19 class Program
20 {
21 static void Main(string[] args)
22 {
23 //Hanoi h = new Hanoi();
24 //h.HanoiMove(5);
25
26 First(CreateBinaryTree());
27 Console.WriteLine("=====================");
28 Middle(CreateBinaryTree());
29 Console.WriteLine("=====================");
30 After(CreateBinaryTree());
31 Console.WriteLine("=====================");
32 Layer(CreateBinaryTree());
33 }
34
35 /// <summary>
36 /// 构建一棵二叉树
37 /// </summary>
38 /// <typeparam name="T"></typeparam>
39 class BTreeNode<T>
40 {
41 T data;
42 BTreeNode<T> lChild, rChild, parent;
43
44 public T Data
45 {
46 get { return data; }
47 set { data = value; }
48 }
49
50 public BTreeNode<T> LChild
51 {
52 get { return lChild; }
53 set { lChild = value; }
54 }
55
56 public BTreeNode<T> RChild
57 {
58 get { return rChild; }
59 set { rChild = value; }
60 }
61
62 public BTreeNode<T> Parent
63 {
64 get { return parent; }
65 set { parent = value; }
66 }
67
68 public BTreeNode() { }
69
70 public BTreeNode(T data)
71 {
72 this.data = data;
73 }
74 }
75
76 /// <summary>
77 /// 创建如下的二叉树
78 /// A
79 /// / \
80 /// B C
81 /// / \ /\
82 /// D EF G
83 /// /
84 /// H
85 /// </summary>
86 /// <returns>返回根节点</returns>
87 static BTreeNode<string> CreateBinaryTree()
88 {
89 BTreeNode<string>[] nodes = new BTreeNode<string>[8];
90
91 nodes[0] = new BTreeNode<string>("A");
92 nodes[1] = new BTreeNode<string>("B");
93 nodes[2] = new BTreeNode<string>("C");
94 nodes[3] = new BTreeNode<string>("D");
95 nodes[4] = new BTreeNode<string>("E");
96 nodes[5] = new BTreeNode<string>("F");
97 nodes[6] = new BTreeNode<string>("G");
98 nodes[7] = new BTreeNode<string>("H");
99
100 nodes[0].LChild = nodes[1];
101 nodes[0].RChild = nodes[2];
102
103 nodes[1].LChild = nodes[3];
104 nodes[1].RChild = nodes[4];
105
106 nodes[2].LChild = nodes[5];
107 nodes[2].RChild = nodes[6];
108
109 nodes[3].LChild = nodes[7];
110
111 return nodes[0];
112 }
113
114 /// <summary>
115 /// 先(根)序遍历
116 /// </summary>
117 /// <param name="node">根节点</param>
118 static void First(BTreeNode<string> node)
119 {
120 Console.WriteLine(node.Data);
121 if (node.LChild != null)
122 {
123 First(node.LChild);
124 }
125 if (node.RChild != null)
126 {
127 First(node.RChild);
128 }
129 }
130
131 /// <summary>
132 /// 中(根)序遍历
133 /// </summary>
134 /// <param name="node">根节点</param>
135 static void Middle(BTreeNode<string> node)
136 {
137 if (node.LChild != null)
138 Middle(node.LChild);
139 Console.WriteLine(node.Data);
140 if (node.RChild != null)
141 Middle(node.RChild);
142 }
143
144 /// <summary>
145 /// 后(根)序遍历
146 /// </summary>
147 /// <param name="node">根节点</param>
148 static void After(BTreeNode<string> node)
149 {
150 if (node.LChild != null)
151 After(node.LChild);
152 if (node.RChild != null)
153 After(node.RChild);
154 Console.WriteLine(node.Data);
155 }
156
157 /// <summary>
158 /// 层次遍历
159 /// </summary>
160 /// <param name="rootNode">根节点</param>
161 static void Layer(BTreeNode<string> rootNode)
162 {
163 int front = -1;
164 int rear = -1;
165 BTreeNode<string>[] nodes = new BTreeNode<string>[20];
166
167 if (rootNode != null)
168 {
169 rear++;
170 nodes[rear] = rootNode;
171 }
172
173 while (front != rear)
174 {
175 front++;
176 // 逐层把左右几点添加进来
177 rootNode = nodes[front];
178 Console.WriteLine(rootNode.Data);
179
180 if (rootNode.LChild != null)
181 {
182 rear++;
183 nodes[rear] = rootNode.LChild;
184 }
185 if (rootNode.RChild != null)
186 {
187 rear++;
188 nodes[rear] = rootNode.RChild;
189 }
190 }
191 }
192 }
193 }
194
2 //
3 // copyright (C) 2010 All rights reserved
4 // framework : 3.5
5 // filename : Program
6 // description :
7 // author : marvin(马非马)
8 // company : Sysu.im.06imis
9 // create time : 2010-8-10 19:01:14
10 //
11 //======================================================================
12 using System;
13 using System.Collections.Generic;
14 using System.Linq;
15 using System.Text;
16
17 namespace AlgorithmTest
18 {
19 class Program
20 {
21 static void Main(string[] args)
22 {
23 //Hanoi h = new Hanoi();
24 //h.HanoiMove(5);
25
26 First(CreateBinaryTree());
27 Console.WriteLine("=====================");
28 Middle(CreateBinaryTree());
29 Console.WriteLine("=====================");
30 After(CreateBinaryTree());
31 Console.WriteLine("=====================");
32 Layer(CreateBinaryTree());
33 }
34
35 /// <summary>
36 /// 构建一棵二叉树
37 /// </summary>
38 /// <typeparam name="T"></typeparam>
39 class BTreeNode<T>
40 {
41 T data;
42 BTreeNode<T> lChild, rChild, parent;
43
44 public T Data
45 {
46 get { return data; }
47 set { data = value; }
48 }
49
50 public BTreeNode<T> LChild
51 {
52 get { return lChild; }
53 set { lChild = value; }
54 }
55
56 public BTreeNode<T> RChild
57 {
58 get { return rChild; }
59 set { rChild = value; }
60 }
61
62 public BTreeNode<T> Parent
63 {
64 get { return parent; }
65 set { parent = value; }
66 }
67
68 public BTreeNode() { }
69
70 public BTreeNode(T data)
71 {
72 this.data = data;
73 }
74 }
75
76 /// <summary>
77 /// 创建如下的二叉树
78 /// A
79 /// / \
80 /// B C
81 /// / \ /\
82 /// D EF G
83 /// /
84 /// H
85 /// </summary>
86 /// <returns>返回根节点</returns>
87 static BTreeNode<string> CreateBinaryTree()
88 {
89 BTreeNode<string>[] nodes = new BTreeNode<string>[8];
90
91 nodes[0] = new BTreeNode<string>("A");
92 nodes[1] = new BTreeNode<string>("B");
93 nodes[2] = new BTreeNode<string>("C");
94 nodes[3] = new BTreeNode<string>("D");
95 nodes[4] = new BTreeNode<string>("E");
96 nodes[5] = new BTreeNode<string>("F");
97 nodes[6] = new BTreeNode<string>("G");
98 nodes[7] = new BTreeNode<string>("H");
99
100 nodes[0].LChild = nodes[1];
101 nodes[0].RChild = nodes[2];
102
103 nodes[1].LChild = nodes[3];
104 nodes[1].RChild = nodes[4];
105
106 nodes[2].LChild = nodes[5];
107 nodes[2].RChild = nodes[6];
108
109 nodes[3].LChild = nodes[7];
110
111 return nodes[0];
112 }
113
114 /// <summary>
115 /// 先(根)序遍历
116 /// </summary>
117 /// <param name="node">根节点</param>
118 static void First(BTreeNode<string> node)
119 {
120 Console.WriteLine(node.Data);
121 if (node.LChild != null)
122 {
123 First(node.LChild);
124 }
125 if (node.RChild != null)
126 {
127 First(node.RChild);
128 }
129 }
130
131 /// <summary>
132 /// 中(根)序遍历
133 /// </summary>
134 /// <param name="node">根节点</param>
135 static void Middle(BTreeNode<string> node)
136 {
137 if (node.LChild != null)
138 Middle(node.LChild);
139 Console.WriteLine(node.Data);
140 if (node.RChild != null)
141 Middle(node.RChild);
142 }
143
144 /// <summary>
145 /// 后(根)序遍历
146 /// </summary>
147 /// <param name="node">根节点</param>
148 static void After(BTreeNode<string> node)
149 {
150 if (node.LChild != null)
151 After(node.LChild);
152 if (node.RChild != null)
153 After(node.RChild);
154 Console.WriteLine(node.Data);
155 }
156
157 /// <summary>
158 /// 层次遍历
159 /// </summary>
160 /// <param name="rootNode">根节点</param>
161 static void Layer(BTreeNode<string> rootNode)
162 {
163 int front = -1;
164 int rear = -1;
165 BTreeNode<string>[] nodes = new BTreeNode<string>[20];
166
167 if (rootNode != null)
168 {
169 rear++;
170 nodes[rear] = rootNode;
171 }
172
173 while (front != rear)
174 {
175 front++;
176 // 逐层把左右几点添加进来
177 rootNode = nodes[front];
178 Console.WriteLine(rootNode.Data);
179
180 if (rootNode.LChild != null)
181 {
182 rear++;
183 nodes[rear] = rootNode.LChild;
184 }
185 if (rootNode.RChild != null)
186 {
187 rear++;
188 nodes[rear] = rootNode.RChild;
189 }
190 }
191 }
192 }
193 }
194
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!如果您想持续关注我的文章,请扫描二维码,关注马非码的微信公众号,我会将我的文章推送给您,并和您一起分享我日常阅读过的优质文章。
本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/marvin/欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南