二叉树的C#实现

二叉树的csharp实现,可用于数组排序
 
   
1 public class Tree < T > where T : IComparable < T >
2 {
3 private T data;
4 private Tree < T > left;
5 private Tree < T > right;
6
7 public Tree(T nodeValue)
8 {
9 this .data = nodeValue;
10 this .left = null ;
11 this .right = null ;
12 }
13
14 public T NodeData
15 {
16 get { return this .data; }
17 set { this .data = value; }
18 }
19 public Tree < T > LeftTree
20 {
21 get { return this .left; }
22 set { this .left = value; }
23 }
24 public Tree < T > RightTree
25 {
26 get { return this .right; }
27 set { this .right = value; }
28 }
29 /// <summary>
30 /// 插入节点
31 /// </summary>
32 /// <param name="newItem"></param>
33 public void Insert(T newItem)
34 {
35 T currentNodeValue = this .NodeData;
36 if (currentNodeValue.CompareTo(newItem) > 0 )
37 {
38 if ( this .LeftTree == null )
39 {
40 this .LeftTree = new Tree < T > (newItem);
41 }
42 else
43 {
44 this .LeftTree.Insert(newItem);
45 }
46 }
47 else
48 {
49 if ( this .RightTree == null )
50 {
51 this .RightTree = new Tree < T > (newItem);
52 }
53 else
54 {
55 this .RightTree.Insert(newItem);
56 }
57 }
58 }
59 /// <summary>
60 /// 遍历树
61 /// </summary>
62 public void WalkTree()
63 {
64 if ( this .LeftTree != null )
65 {
66 this .LeftTree.WalkTree();
67 }
68 Console.WriteLine( this .NodeData.ToString());
69 if ( this .RightTree != null )
70 {
71 this .RightTree.WalkTree();
72 }
73 }
74 }

转载于:https://www.cnblogs.com/michael110/archive/2011/03/25/1995735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值