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