由于学习进度的原因,二叉树没有在3月12日植树节那天学习,现在乘者3月15日的日子,将学习的内容代码部分发布,网友可以在编程环境下运行。

  这里采用二叉链表,实现的代码见下:

 

  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using QueueStack;
  5
  6namespace 二叉链表
  7{
  8    class Program
  9    {
 10        public class Node<T>
 11        
 12             //数据域
 13            private T data;
 14            //左孩子
 15            private Node<T> Lchild;
 16            //右孩子
 17            private Node<T> Rchild;
 18            //构造函数
 19            public Node(T val, Node<T> lp, Node<T> rp)
 20            {
 21                data = val;
 22                Lchild = lp;
 23                Rchild = rp;
 24            }

 25            //构造函数
 26            public Node(Node<T> lp, Node<T> rp)
 27            {
 28                data=default(T);
 29                Lchild = lp;
 30                Rchild = rp;
 31            }

 32            //构造函数
 33            public Node(T val)
 34            {
 35                data = val;
 36                Lchild = null;
 37                Rchild = null;
 38            }

 39            //构造函数
 40            public Node()
 41            
 42              data=default(T);
 43              Lchild = null;
 44              Rchild = null;
 45            }

 46            //数据属性
 47            public T Data
 48            {
 49                get
 50                {
 51                    return data;
 52                }

 53                set
 54                {
 55                    data = value;
 56                }

 57            }

 58            //左孩子属性
 59            public Node<T> LChild
 60            {
 61                get
 62                {
 63                    return Lchild;
 64                }

 65                set
 66                {
 67                    Lchild = value;
 68                }

 69            }

 70            //右孩子属性
 71            public Node<T> RChild
 72            {
 73                get
 74                {
 75                    return Rchild;
 76                }

 77                set
 78                {
 79                    Rchild = value;
 80                }

 81            }

 82            
 83
 84        }

 85        public class BiTree<T>
 86        {
 87            //头引用
 88            private Node<T> head;
 89            //头属性
 90            public Node<T> Head
 91            {
 92                get
 93                {
 94                    return head;
 95                }

 96                set
 97                {
 98                    head = value;
 99                }

100
101            }

102            //构造函数
103            public BiTree(T val)
104            {
105                Node<T> p = new Node<T>(val);
106                head = p;
107            }

108            //构造函数
109            public BiTree(T val, Node<T> lp, Node<T> rp)
110            {
111                Node<T> p = new Node<T>(val, lp, rp);
112                head = p;
113            }

114            //构造函数
115            public BiTree()
116            {
117                head = null;
118            }

119            //判断是否为空
120            public bool IsEmpty()
121            {
122                if (head == null)
123                {
124                    return true;
125                }

126                else
127                {
128                    return false;
129                }

130            }

131            //获取根节点
132            public Node<T> Root()
133            {
134                return head;
135            }

136            //获取节点左孩子节点
137            public Node<T> GetChildL(Node<T> p)
138            {
139                return p.LChild;
140            }

141            //获取节点右孩子节点
142            public Node<T> GetChildR(Node<T> p)
143            {
144                return p.RChild;
145            }

146            //将节点P的左子树插入值为VAL的新节点,原来节点的左子树成为新节点的左子树
147            public void InsertL(T val, Node<T> p)
148            {
149                Node<T> tmp = new Node<T>(val);
150                tmp.LChild = p.LChild;
151                p.LChild = tmp;
152            }

153            //将节点P的右子树插入值为VAL的新节点,原来节点的左子树成为新节点的右子树
154            public void InsertR(T val, Node<T> p)
155            {
156                Node<T> tmp = new Node<T>(val);
157                tmp.RChild = p.RChild;
158                p.RChild = tmp;
159            }

160            //若p非空,删除P的左子树
161            public Node<T> DeleteL(Node<T> p)
162            {
163                if ((p == null&& (p.LChild == null))
164                {
165                    return null;
166                }

167                Node<T> tmp = p.LChild;
168                p.LChild = null;
169                return tmp;
170            }

171            //若p非空,删除P的右子树
172            public Node<T> DeleteR(Node<T> p)
173            {
174                if ((p == null&& (p.LChild == null))
175                {
176                    return null;
177                }

178                Node<T> tmp = p.RChild;
179                p.RChild = null;
180                return tmp;
181            }

182            //判断是否为叶子节点
183            public bool IsLeaf(Node<T> p)
184            {
185                if ((p != null&& (p.LChild != null&& (p.RChild != null))
186                {
187                    return true;
188                }

189                else
190                {
191                    return false;
192                }

193            }

194
195            //先序遍历DLR
196            public void PreOrder(Node<T> root)
197            {
198                //根节点为空,结束
199                if (root == null)
200                {
201                    return;
202                }

203                //处理根节点
204                Console.WriteLine(root.Data);
205                //遍历左子树
206                PreOrder(root.LChild);
207                //遍历右子树
208                PreOrder(root.RChild);
209            }

210            //中序遍历LDR
211            public void InOrder(Node<T> root)
212            {
213                //根节点为空
214                if (root == null)
215                {
216                    return;
217                }

218
219                //中序遍历左子树
220                InOrder(root.LChild);
221                Console.WriteLine(root.Data);
222                //中序遍历右子树
223                InOrder(root.RChild);
224            }

225            //后序遍历LRD
226            public void PostOrder(Node<T> root)
227            {
228                //根节点为空
229                if (root == null)
230                {
231                    return;
232                }

233
234                //后序遍历左子树
235                PostOrder(root.LChild);
236                //后序遍历右子树
237                PostOrder(root.RChild);
238                Console.WriteLine(root.Data);
239            }

240            //层序遍历Level Order
241            public void LevelOrder(Node<T> root)
242            {
243                //根节点为空
244                if (root == null)
245                {
246                    return;
247                }

248                //设置一个队列保存层序遍历的节点
249                QueueStack.queue.SeqQueue<T> queue = new queue.SeqQueue<T>(50);
250                //根队列入队
251                queue.In(root);
252                //队列非空,节点没有处理完成
253                while (!queue.IsEmpty())
254                {
255                    //节点出队
256                    Node<T> tmp = queue.Out();
257                    //处理当前节点
258                    Console.WriteLine("{0}", tmp);
259                    //左孩子入队
260                    if (root.LChild != null)
261                    {
262                        queue.In(root.LChild);
263                    }

264                    //右孩子入队
265                    if (root.RChild != null)
266                    {
267                        queue.In(root.RChild);
268                    }

269                }

270
271            }

272
273        }

274        
275        static void Main(string[] args)
276        {
277            Node<int> p = new Node<int>(12);
278            Node<int> q = new Node<int>(20);
279            BiTree<int> bt = new BiTree<int>(50,p,q);
280            //插入左子树
281            bt.InsertL(60,p);
282            //插入右子树
283            bt.InsertR(70,q);
284            //先序遍历
285            bt.PreOrder(bt.Root());
286            //中序遍历
287            bt.InOrder(bt.Root());
288            //后序遍历
289            bt.PostOrder(bt.Root());
290            //层序遍历
291            bt.LevelOrder(bt.Root());
292
293        }

294    }

295}

296

 

 

posted on 2009-03-15 16:35  甲乙丙丁  阅读(276)  评论(0编辑  收藏  举报