基礎的なもの、二叉木のいくつかのアルゴリズムC#について
5661 ワード
書くのは比較的簡単で,間に合わせに読む
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
using System;
using System.Collections.Generic;
using System.Text;
namespace structure
{
class Program
{
#region Define tree node
#Inclde data, left node, right node and parent node
class nodes<T>
{
T data;
nodes<T> Lnode, Rnode, Pnode;
public T Data
{
set
{
data = value;
}
get
{
return data;
}
}
public nodes<T> RNode
{
set
{
Rnode = value;
}
get
{
return RNode;
}
}
public nodes<T> LNode
{
set
{
Lnode = value;
}
get
{
return LNode;
}
}
public nodes<T> PNode
{
set
{
Pnode = value;
}
get
{
return PNode;
}
}
public nodes()
{}
public nodes(T data)
{
this.data = data;
}
#endregion
}
static void PreOrder<T>(nodes<T> rootNode)
{
if(rootNode != null)
{
Console.WriteLine(rootNode.Data);
if(rootNode.LNode != null)
PreOrder<T>(rootNode.LNode);
if(root.RNode != null)
PreOrder<T>(rootNode.RNode);
}
}
static void MidOrder<T>(nodes<T> rootNode)
{
if(rootNode != null)
{
if(rootNode.LNode != null)
MidOrder<T>(rootNode.LNode);
Console.WriteLine(rootNode.Data);
if(rootNode.RNode != null)
MidOrder<T>(rootNode.RNode);
}
}
static void AfterOrder<T>(nodes<T> rootNode)
{
if(rootNode != null)
{
if(rootNode.LNode != null)
AfterOrder<T>(rootNode.LNode);
if(rootNode.RNode != null)
AfterOrder<T>(rootNode.RNode);
Console.WriteLine(rootNode.Data);
}
}
static void LayerOrder<T>(nodes<T> rootNode)
{
Queue<T> q = new Queue<T>();
if(rootNode != null)
q.Enque(rootNode);
while(q != null)
{
Node tmpNode = q.Deque();
Console.WriteLine(tmpNode.Data);
if(tmpNode.LNode)
q.Enqueue(tmpNode.LNode);
if(tmpNode.RNode)
q.Enqueue(tmpNode.RNode)
}
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }