标题: C#线索二叉树
问天
元帅
Rank: 1


元帅勋章 终身成就勋章
UID 11493
精华 187
积分 34199
帖子 33353
威望 91
金币 13148
热心 2617
阅读权限 100
注册 2006-4-7
状态 离线
C#线索二叉树

using System;

namespace BiThrTree
{
/// <summary>
/// 定义结点类:
/// </summary>
class BTNode
{
public char data;
public int ltag,rtag;//0表示线索,1表示结点
public BTNode lchild,rchild;
}
class BiThrTree
{
/// <summary>
/// 建立一棵新二叉树:
/// </summary>
/// <param name="T"></param>
static public void CreateBiThrTree(ref BTNode T)
{
char ch;
ch=(char)Console.Read();
if(ch=='#')
{
T=null;
}
else
{
T=new BTNode();
T.data=ch;
CreateBiThrTree(ref T.lchild);
CreateBiThrTree(ref T.rchild);
}
}
/// <summary>
/// 线索化二叉树:
/// </summary>
/// <param name="T"></param>
static BTNode pre,H;
static public void Threading(ref BTNode T)
{
H=pre=new BTNode();
pre.rchild=pre.lchild=null;
pre.rtag=pre.ltag=0;
Thread(ref T);
}
static public void Thread(ref BTNode T)   
{
if(T!=null)
{
if(T.lchild==null){T.lchild=pre;T.ltag=0;}
else{T.ltag=1;}
if(T.rchild==null){T.rtag=0;}
else{T.rtag=1;}
if(pre.rchild==null&;amp;&;amp;pre.rtag==0)pre.rchild=T;
pre=T;
if(T.ltag==1)Thread(ref T.lchild);
if(T.rtag==1)Thread(ref T.rchild);
}
}
/// <summary>
/// 先序输出:
/// </summary>
static public void PrePrint(BTNode T)
{
if(T!=null)
{
Console.Write(T.data "\t");
if(T.ltag==1)PrePrint(T.lchild);
if(T.rtag==1)PrePrint(T.rchild);
}
}
/// <summary>
/// 先序线索遍历输出:
/// </summary>
static public void PreThrPrint(BTNode T)
{
T=H.rchild;
//Console.WriteLine("H.rchild.date::" H.rchild.data);
while(T!=null)
{
Console.Write(T.data "\t");
if(T.rtag==0)T=T.rchild;
else{
if(T.ltag==1)T=T.lchild;
else{
T=T.rchild;
}
}
}
}
/// <summary>
/// Deepth of a BiThrTree:
/// </summary>
static public int Deepth(BTNode T)     
{
int a,b;
if(T!=null)
{
if(T.ltag==1)a=Deepth(T.lchild);else a=0;
if(T.rtag==1)b=Deepth(T.rchild);else b=0;
return (1 max(a,b));
}
else
{
return 0;
}
}
static public int max(params int[] w)
{
int max;
max=w[0];
for(int i=0;i<w.Length;i  )
if(max<w)max=w;
return max;
}
/// <summary>
/// 复制线索二叉树:
/// </summary>
static public void DulplicateBiThrTree(BTNode T1,ref BTNode T2)
{
if(T1!=null)
{
T2=new BTNode();
T2.data=T1.data;
T2.ltag=T1.ltag;T2.rtag=T1.rtag;
if(T2.ltag==1)DulplicateBiThrTree(T1.lchild,ref T2.lchild);else T2.lchild=T1.lchild;
if(T2.rtag==1)DulplicateBiThrTree(T1.rchild,ref T2.rchild);else T2.rchild=T1.rchild;
}
}

static void Main()
{
BTNode mytree=null;
Console.WriteLine("Please input a tree(for example:abc##d##ed###):");
CreateBiThrTree(ref mytree);
Threading(ref mytree);
PrePrint(mytree);
Console.WriteLine("\n按先序输出:\n");     
PreThrPrint(mytree);
Console.WriteLine("\n该树的深度为:{0}",Deepth(mytree));
BTNode mytree2=null;
Console.WriteLine("调用复制函数得到的新树为:");
DulplicateBiThrTree(mytree,ref mytree2);
PrePrint(mytree2);
Console.ReadLine();
Console.ReadLine();
}

}

}




网友 问天 签名 - 网友社区 请您回个帖。谢谢
PR查询 免费域名 免费空间
顶部
[广告] 免费域名(Free Subdomain) 免费空间(Free hosting) PR查询(Google Pagerank)



当前时区 GMT+8, 现在时间是 2008-10-7 14:12
信产部ICP备案:京ICP备05066424号 北京市公安局网监备案:1101050648号

Powered by Discuz! 5.5.0
清除 Cookies - 联系我们 - 网友俱乐部 - Archiver - WAP