Namespace Bithrtree
{
<summary>
To define a node class:
</summary>
Class Btnode
{
public char data;
public int ltag,rtag;//0 Indicates clues, 1 indicates node
Public Btnode Lchild,rchild;
}
Class Bithrtree
{
<summary>
Build a new binary tree:
</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>
Cue binary tree:
</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&&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>
First Order output:
</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>
First sequence thread traversal output:
</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[i]) max=w[i];
return Max;
}
<summary>
Replication leads two fork tree:
</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 by First order output: \ n");
Prethrprint (mytree);
Console.WriteLine ("\ n the depth of the tree: {0}", Deepth (mytree));
Btnode Mytree2=null;
Console.WriteLine ("The new tree called the copy function is:");
Dulplicatebithrtree (Mytree,ref mytree2);
Preprint (MYTREE2);
Console.ReadLine ();
Console.ReadLine ();
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.