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


#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 { get; set; }
public sbyte Position {
get;
private set;
}
public T t;
internal List<Node> Childs { get; set; }
public string Key { get; set; }
public dynamic Data { get; set; }
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 = 1; int 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 { get; private 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,
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 一般树学习 {
class Tree : ITreeManage {
internal class Node {
public Node Parent { get; set; }
public sbyte Position {
get;
private set;
}
public T t;
internal List<Node> Childs { get; set; }
public string Key { get; set; }
public dynamic Data { get; set; }
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 = 1; int 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 { get; private 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,
}
}


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();
}
}
}
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();
}
}
}
//
看下这个代码 怎么样,当然最重要的,遍历还没写,下篇补上