我操,水平 太低没事写个树学习下,

本文探讨了一种一般树结构的学习实现,通过定义树节点类和树管理接口,实现了树的添加节点功能,并预留了遍历方法待实现。文章还提供了一个简单的测试示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这段时间看了下xna里面的Model对象结构,树的确太常用了,需要没事 学习下,上次我也写过一次类似的来着,不过原码掉了也不怎么对这次重写一次,经过初步调试,虽然不完全对,但还是差不多有70%左右吧达到了预期,现在把它搞上来 期望那些过路的热心朋友不吝指正,帮助 ,我在此表示热烈的欢迎!

 

 

ExpandedBlockStart.gif代码
#define CONTRACTS_FULL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 一般树学习 {
    
class Tree : ITreeManage {
        
internal class Node {
            
public Node Parent { getset; }
            
public sbyte Position {
                
get;
                
private set;
            }
            
public T t;
            
internal List<Node> Childs { getset; }
            
public string Key { getset; }
            
public dynamic Data { getset; }
            
public Node() {
                
this.Position = default(sbyte);
            }
            
public Node GoFirst() {
                
//this.Childs = new List<Node>();
                
//this.Childs.Add(new Node());
                return this.Childs[0];
            }

            
public Node GoNext(Node current) {
                var index 
= this.Childs.FindIndex(n => n.Equals(current));
                
if (index != -1 && index < this.Childs.Count-1)
                    
return this.Childs[index + 1];
                
return null;
            }
            
internal void Add(Node n) {
                
if (this.Childs == null)
                    
this.Childs = new List<Node>();
                
this.Childs.Add(n);
                
this.Position = (sbyte)this.Childs.Count;
            }
        }
        Node root;
//根节点   
        Node current;
        
public enum T : sbyte {
            t0 
= 0, t1 = 1, t2 = 2, t3 = 3, te = 4
        }
        
sbyte[] ts;
        
const sbyte tie = 3;
        
public Tree(sbyte t1,sbyte t2,sbyte t3) {
            
this.ts = new sbyte[Tree.tie];
            
this.ts[0= t1; this.ts[1= t2; this.ts[2= t3;
            
this.root = new Node();
            
this.root.Childs = new List<Node>();
            
this.root.t = T.t0;
            
this.root.Key = "root";
            
this.current = this.root;
        }
        
sbyte GetTValue(T t) {
            
sbyte r = default(sbyte);
            
switch (t) {
                
case T.t0:
                    r 
= this.ts[0];
                    
break;
                
case T.t2:
                    r 
= this.ts[2];
                    
break;
                
case T.t1:
                    r 
= this.ts[1];
                    
break;
                
case T.t3:

                    
break;
                
default:
                    
break;
            }
            
return r;
        }
        
//tdd ftdd;
        
//class tdd {

        
//}

        
#region ITreeManage 成员

        
public void Add(string key,object data) {
            
if (this.RealCount < this.MaxCount) {
                Node n 
= new Node();
                n.Key 
= key; n.Data = data;
                n.t 
= this.current.t + 1;
            re1:
                
if (this.current.Position < this.GetTValue(this.current.t)) {
                    n.Parent 
= this.current;
                    
this.current.Add(n);
                    
this.RealCount++;
                    
return;
                } 
else if (this.current.t < T.te - 2) {
                    
this.current = this.current.GoFirst();
                    
goto re1;
                }
            re2:
                
if (this.current.Parent != null && this.current.Parent.GoNext(this.current) != null) {
                    
this.current = this.current.Parent.GoNext(this.current);
                    
goto re1;
                } 
else if (this.current.Parent != null) {
                    
this.current = this.current.Parent;
                    
goto re2;
                }
            }
            
//end: ;
        }

        
public void Remove(Tree.Node n) {
            
throw new NotImplementedException();
        }

        
public IEnumerable<object> Traversal(TraversalType tt) {
            
throw new NotImplementedException();
        }

        
public int Lenght {
            
get {
                
int len = default(int);
                len 
= 1;
                
foreach (var item in this.ts) {
                    len 
*= item;
                }
                
return len;
            }
        }
        
public int MaxCount {
            
get {
                
int mxc = default(int);
                mxc 
= 1int h,jj; h = 0; jj = 1;
                
for (int i = 0 ; i < Tree.tie - 1 ; i++) {
                    jj 
= this.ts[i];
                    
for (int j = 0 ; j < i ; j++) {
                        jj 
*= this.ts[j];
                    }
                    h 
+= jj;
                }
                mxc 
= h + this.Lenght;
                
return mxc;
            }
        }
        
public int RealCount { getprivate set; }

        
#endregion
    }
    
interface ITreeManage {
        
void Add(string key,object data);
        
void Remove(Tree.Node n);
        IEnumerable
<object> Traversal(TraversalType tt);
        
int Lenght { get; }
        
int MaxCount { get; }
        
int RealCount { get; }
    }
    
enum TraversalType {
        
/// <summary>
        
/// 前序
        
/// </summary>
        preorder = 0,
        
/// <summary>
        
/// 后序
        
/// </summary>
        lastorder = 1,
        
/// <summary>
        
/// 中序
        
/// </summary>
        middelorder = 2,
        
/// <summary>
        
/// 广度
        
/// </summary>
        breadth = 3,
    }
}

 

 

ExpandedBlockStart.gif代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 一般树学习 {
    
class Program {
        
static void Main(string[] args) {
            Console.WriteLine(
"一般树学习:");
            Tree t 
= new Tree(3,3,3);
            Console.WriteLine(
"Tree.Length={0}",t.Lenght);
            Console.WriteLine(
"Tree.MaxCount={0}",t.MaxCount);
            Console.WriteLine(
"Tree.RealCount={0}",t.RealCount);

            
for (int i = 0 ; i < t.MaxCount ; i++) {
                t.Add(i.ToString(),i);
            }
            Console.ReadKey();
        }
    }
}

 

 

//

看下这个代码 怎么样,当然最重要的,遍历还没写,下篇补上

转载于:https://www.cnblogs.com/ProjectDD/archive/2011/01/11/1932491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值