我写了一个简单的二叉树的算法,运行的时候出错了,我不知道现在要怎么样修改了,希望大家帮下忙,我非非常感谢每个帮忙的人!
namespace CS
{ public class Node<T>
{ private T data; //数据
private Node<T> lChild; //左孩子
private Node<T> rChild; //右孩子
public Node(T val, Node<T> lp, Node<T> rp)
{
data = val;
lChild = lp;
rChild = rp;
}
public Node(T val)
{
data = val;
lChild = null;
rChild = null;
}
public Node()
{
data = default(T);
lChild = null;
rChild = null;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> LChild
{
get { return lChild; }
set { lChild = value; }
}
public Node<T> RChild
{
get { return rChild; }
set { rChild = value; }
}
} public class BiTree<T> { private Node<T> head; public Node<T> Head
{
get { return head; }
set { head = value; }
}
public BiTree()
{ head = null; } public BiTree(T val)
{
Node<T> p = new Node<T>(val);
head = p;
} public BiTree(T val, Node<T> l, Node<T> r)
{
Node<T> p = new Node<T>(val,l,r);
head = p;
} // 是否为空 public bool IsEmpty()
{
if (head == null)
return true;
else
return false;
} //get root
public Node<T> root()
{ return head; } //get right child
public Node<T> GetLChild(Node<T> p)
{ return p.LChild; }
//get left child
public Node<T> GetRChild(Node<T> p)
{ return p.RChild; } /// <summary>
/// 将结点P的左子树插入值为val的新结点,
/// 原左子树成为新的结点的左子树
/// </summary>
public void InsertL(T val, Node<T> p)
{
Node<T> tmp = new Node<T>(val);
tmp.LChild = p.LChild;
p.LChild = tmp;
} /// <summary>
/// 将结点P的右子树插入值为val的新结点,
/// 原右子树成为新的结点的右子树
/// </summary>
public void InsertR(T val, Node<T> p)
{
Node<T> tmp = new Node<T>(val);
tmp.RChild = p.RChild;
p.RChild = tmp;
} // 若p非空,则删除其左子树
public Node<T> DeleteL(Node<T> p)
{
if (p==null||p.LChild==null)
return null;
Node<T> tmp = p.LChild;
p.LChild = null; return tmp;
} // 若p非空,则删除其右子树
public Node<T> DeleteR(Node<T> p)
{
if (p == null || p.RChild == null)
return null;
Node<T> tmp = p.RChild;
p.RChild = null; return tmp;
} //是否叶子结点
public bool IsLeaf(Node<T> p)
{
if((p!=null)&&(p.LChild==null)&&(p.RChild==null))
return true ;
else
return false;
} }
class Program
{
static void Main(string[] args)
{
int h = 0;
Node<int> l=null,r=null,root=new Node<int> (5); BiTree<int> bTree = new BiTree<int>();
if (bTree.IsEmpty())
{
for (int i = 0; i < 5; i++)
{ try
{
Console.WriteLine("请输入5组二个整数(左,右)");
Console.WriteLine("left:");
l.LChild.Data = Int32.Parse(Console.ReadLine());
Console.WriteLine("right:");
r.RChild.Data = Int32.Parse(Console.ReadLine()); }
catch (Exception) { }
finally
{
bTree.Head= root;
bTree.InsertL(root.Data,l);
bTree.InsertR(root.Data, r);
}
} for (int i = 0; i < 5; i++)
{
Console.WriteLine("root",bTree.Head);
Console.WriteLine("left", bTree.GetLChild(l));
Console.WriteLine("rigth", bTree.GetRChild(r));
}
Console.ReadLine(); }
}
}
}
namespace CS
{ public class Node<T>
{ private T data; //数据
private Node<T> lChild; //左孩子
private Node<T> rChild; //右孩子
public Node(T val, Node<T> lp, Node<T> rp)
{
data = val;
lChild = lp;
rChild = rp;
}
public Node(T val)
{
data = val;
lChild = null;
rChild = null;
}
public Node()
{
data = default(T);
lChild = null;
rChild = null;
}
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> LChild
{
get { return lChild; }
set { lChild = value; }
}
public Node<T> RChild
{
get { return rChild; }
set { rChild = value; }
}
} public class BiTree<T> { private Node<T> head; public Node<T> Head
{
get { return head; }
set { head = value; }
}
public BiTree()
{ head = null; } public BiTree(T val)
{
Node<T> p = new Node<T>(val);
head = p;
} public BiTree(T val, Node<T> l, Node<T> r)
{
Node<T> p = new Node<T>(val,l,r);
head = p;
} // 是否为空 public bool IsEmpty()
{
if (head == null)
return true;
else
return false;
} //get root
public Node<T> root()
{ return head; } //get right child
public Node<T> GetLChild(Node<T> p)
{ return p.LChild; }
//get left child
public Node<T> GetRChild(Node<T> p)
{ return p.RChild; } /// <summary>
/// 将结点P的左子树插入值为val的新结点,
/// 原左子树成为新的结点的左子树
/// </summary>
public void InsertL(T val, Node<T> p)
{
Node<T> tmp = new Node<T>(val);
tmp.LChild = p.LChild;
p.LChild = tmp;
} /// <summary>
/// 将结点P的右子树插入值为val的新结点,
/// 原右子树成为新的结点的右子树
/// </summary>
public void InsertR(T val, Node<T> p)
{
Node<T> tmp = new Node<T>(val);
tmp.RChild = p.RChild;
p.RChild = tmp;
} // 若p非空,则删除其左子树
public Node<T> DeleteL(Node<T> p)
{
if (p==null||p.LChild==null)
return null;
Node<T> tmp = p.LChild;
p.LChild = null; return tmp;
} // 若p非空,则删除其右子树
public Node<T> DeleteR(Node<T> p)
{
if (p == null || p.RChild == null)
return null;
Node<T> tmp = p.RChild;
p.RChild = null; return tmp;
} //是否叶子结点
public bool IsLeaf(Node<T> p)
{
if((p!=null)&&(p.LChild==null)&&(p.RChild==null))
return true ;
else
return false;
} }
class Program
{
static void Main(string[] args)
{
int h = 0;
Node<int> l=null,r=null,root=new Node<int> (5); BiTree<int> bTree = new BiTree<int>();
if (bTree.IsEmpty())
{
for (int i = 0; i < 5; i++)
{ try
{
Console.WriteLine("请输入5组二个整数(左,右)");
Console.WriteLine("left:");
l.LChild.Data = Int32.Parse(Console.ReadLine());
Console.WriteLine("right:");
r.RChild.Data = Int32.Parse(Console.ReadLine()); }
catch (Exception) { }
finally
{
bTree.Head= root;
bTree.InsertL(root.Data,l);
bTree.InsertR(root.Data, r);
}
} for (int i = 0; i < 5; i++)
{
Console.WriteLine("root",bTree.Head);
Console.WriteLine("left", bTree.GetLChild(l));
Console.WriteLine("rigth", bTree.GetRChild(r));
}
Console.ReadLine(); }
}
}
}
解决方案 »
- CheckBoxList 选择与取消的问题?
- 请教各位大虾一个问题,怎样把一个文本框里的多行数据插入数据库
- 关于数据库连接以及DATALIST的几个疑问
- SecondaryBuffer 构造函数提示“值不在预期范围内”
- C#字符串截取问题应该怎么截取
- 在vs2010 打包winform 应用程序出现的错误。。。求救
- 100分求解如何判断adsl是否在线?
- 关于用word做报表,请各位做过报表系统的大虾支招
- 请教一个基础的设计问题。
- Windows服务中应该用那种timer。System.Timers or System.Threading.Timer ? 请高手帮忙!
- 报表问题,急求解!!!!!
- [IE编程]WebBrowser控件和IE版本关系?
using System.Collections.Generic;
using System.Text;namespace Bintree
{
static class Program
{
//二叉树结点数据结构的定义
//二叉树结点数据结构包括数据域,左右结点以及父结点成员;
class nodes<T>
{
T data;
nodes<T> Lnode, Rnode, Pnode;
public T Data
{
set { data = value; }
get { return data; }
}
public nodes<T> LNode
{
set { Lnode = value; }
get { return Lnode; }
}
public nodes<T> RNode
{
set { Rnode = value; }
get { return Rnode; }
}
public nodes<T> PNode
{
set { Pnode = value; }
get { return Pnode; }
}
public nodes()
{ }
public nodes(T data)
{
this.data = data;
}
} //先序编历二叉树
static void PreOrder<T>(nodes<T> rootNode)
{
if (rootNode != null)
{
Console.WriteLine(rootNode.Data);
PreOrder<T>(rootNode.LNode);
PreOrder<T>(rootNode.RNode);
}
}
//构造一棵已知的二叉树
static nodes<string> BinTree()
{
nodes<string>[] binTree = new nodes<string>[8];
//创建结点
binTree[0] = new nodes<string>("A");
binTree[1] = new nodes<string>("B");
binTree[2] = new nodes<string>("C");
binTree[3] = new nodes<string>("D");
binTree[4] = new nodes<string>("E");
binTree[5] = new nodes<string>("F");
binTree[6] = new nodes<string>("G");
binTree[7] = new nodes<string>("H");
//使用层次遍历二叉树的思想,构造一个已知的二叉树
binTree[0].LNode = binTree[1];
binTree[0].RNode = binTree[2];
binTree[1].RNode = binTree[3];
binTree[2].LNode = binTree[4];
binTree[2].RNode = binTree[5];
binTree[3].LNode = binTree[6];
binTree[3].RNode = binTree[7];
//返回二叉树的根结点
return binTree[0];}//中序遍历二叉树
static void MidOrder<T>(nodes<T> rootNode)
{
if (rootNode != null)
{
MidOrder<T>(rootNode.LNode);
Console.WriteLine(rootNode.Data);
MidOrder<T>(rootNode.RNode);
}
} //后序遍历二叉树
static void AfterOrder<T>(nodes<T> rootNode)
{
if (rootNode != null)
{
AfterOrder<T>(rootNode.LNode);
AfterOrder<T>(rootNode.RNode);
Console.WriteLine(rootNode.Data);
}
} //层次遍历二叉树
static void LayerOrder<T>(nodes<T> rootNode)
{
nodes<T>[] Nodes = new nodes<T>[20];
int front = -1;
int rear = -1;
if (rootNode != null)
{
rear++;
Nodes[rear] = rootNode;
}
while (front != rear)
{
front++;
rootNode = Nodes[front];
Console.WriteLine(rootNode.Data);
if (rootNode.LNode != null)
{
rear++;
Nodes[rear] = rootNode.LNode;
}
if (rootNode.RNode != null)
{
rear++;
Nodes[rear] = rootNode.RNode;
}
}
}
//测试的主Methods
static void Main(string[] args)
{ Console.WriteLine("构造一客二叉树:结点分别为A,B,C,D,E,F,G,H");
Console.WriteLine(" A");
Console.WriteLine(" / \\");
Console.WriteLine(" B C");
Console.WriteLine(" / \\ / \\");
Console.WriteLine(" D E F G");
Console.WriteLine(" /");
Console.WriteLine(" H");
nodes<string> rootNode = BinTree();
Console.WriteLine("先序遍历Methods遍历二叉树:");
PreOrder<string>(rootNode);Console.WriteLine("中序遍历Methods遍历二叉树:");
MidOrder<string>(rootNode);Console.WriteLine("后序遍历Methods遍历二叉树:");
AfterOrder<string>(rootNode);Console.WriteLine("层次遍历Methods遍历二叉树:");
LayerOrder<string>(rootNode);Console.Read();
} }
}