数据结构:二叉树的建立和遍历(C#实现)
2008-12-12 10:08
796 查看
遍历只做了先序,递归实现的,中序和后序都类似.代码比较简单,就不写注释,直接贴出来了
代码:
TreeNode:结点类
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉树的建立和遍历
{
class TreeNode
{
public char data;
public TreeNode left, right;
public TreeNode(char c,TreeNode l,TreeNode r)
{
data = c;
left = l;
right = r;
}
public TreeNode() { left = right = null; }
}
}
Tree:树类
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉树的建立和遍历
{
class Tree
{
public TreeNode root;
Stack<TreeNode> stack = new Stack<TreeNode>();
public void CreateTree(String description)
{
bool left=true;
char[] descriptionarray = description.ToCharArray();
root = new TreeNode();
root.data = descriptionarray[0];
TreeNode temp=root;
for (int i = 1; i <=descriptionarray.Length-1; i++)
{
if (descriptionarray[i] == '(')
{
left = true; stack.Push(temp);
}
else if (descriptionarray[i] == ',')
{
left = false;
}
else if (descriptionarray[i] == ')') { stack.Pop(); }
else
{
temp = new TreeNode();
temp.data = descriptionarray[i];
if (left == true)
{
stack.Peek().left = temp;
}
else
{
stack.Peek().right = temp;
}
}
}
}
public void PreOrder(TreeNode t,String sign)
{
if (t!= null)
{
Console.WriteLine(sign+t.data);
sign += sign;
if (t.left != null)
{
PreOrder(t.left,sign);
}
if (t.right != null)
{
PreOrder(t.right,sign);
}
}
}
}
}
Program:调用者
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉树的建立和遍历
{
class Program
{
static void Main(string[] args)
{
//TreeNode t=null;
Tree tree = new Tree();
Console.WriteLine("请输入用字符串表示的二叉树");
string input =Console.ReadLine();
tree.CreateTree(input);
Console.WriteLine("遍历");
tree.PreOrder(tree.root,"--");
Console.Read();
}
}
}
代码:
TreeNode:结点类
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉树的建立和遍历
{
class TreeNode
{
public char data;
public TreeNode left, right;
public TreeNode(char c,TreeNode l,TreeNode r)
{
data = c;
left = l;
right = r;
}
public TreeNode() { left = right = null; }
}
}
Tree:树类
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉树的建立和遍历
{
class Tree
{
public TreeNode root;
Stack<TreeNode> stack = new Stack<TreeNode>();
public void CreateTree(String description)
{
bool left=true;
char[] descriptionarray = description.ToCharArray();
root = new TreeNode();
root.data = descriptionarray[0];
TreeNode temp=root;
for (int i = 1; i <=descriptionarray.Length-1; i++)
{
if (descriptionarray[i] == '(')
{
left = true; stack.Push(temp);
}
else if (descriptionarray[i] == ',')
{
left = false;
}
else if (descriptionarray[i] == ')') { stack.Pop(); }
else
{
temp = new TreeNode();
temp.data = descriptionarray[i];
if (left == true)
{
stack.Peek().left = temp;
}
else
{
stack.Peek().right = temp;
}
}
}
}
public void PreOrder(TreeNode t,String sign)
{
if (t!= null)
{
Console.WriteLine(sign+t.data);
sign += sign;
if (t.left != null)
{
PreOrder(t.left,sign);
}
if (t.right != null)
{
PreOrder(t.right,sign);
}
}
}
}
}
Program:调用者
using System;
using System.Collections.Generic;
using System.Text;
namespace 二叉树的建立和遍历
{
class Program
{
static void Main(string[] args)
{
//TreeNode t=null;
Tree tree = new Tree();
Console.WriteLine("请输入用字符串表示的二叉树");
string input =Console.ReadLine();
tree.CreateTree(input);
Console.WriteLine("遍历");
tree.PreOrder(tree.root,"--");
Console.Read();
}
}
}
相关文章推荐
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 数据结构_树_二叉树的建立、遍历、复制与移除_二叉链表存储_C++实现
- 二叉树建立和遍历,(数据结构清华大学版130页遍历实现)
- 数据结构--二叉树代码实现(建立,前序,中序,后续遍历,树的深度,销毁)
- 数据结构----二叉树----建立与遍历
- 数据结构-非递归实现后序遍历二叉树
- 二叉树的建立以及前序、中序、后序遍历的递归和非递归实现
- 数据结构-----后序遍历二叉树非递归算法(利用堆栈实现)
- 数据结构_二叉树的先序建立与先序,中序,后序(递归)遍历方式_C语言源代码
- 二叉树的先序建立、遍历、镜像、节点数、深度的具体代码实现
- 数据结构之二叉树(遍历、建立、深度)
- 数据结构 c语言实现 二叉树的层次遍历(linux下实现)
- 【数据结构】中序线索化二叉树后实现一个迭代器来遍历二叉树
- 二叉树的建立与遍历(一)(c++实现)
- 数据结构 二叉树的建立与遍历
- 【数据结构】二叉树前中后序遍历、层序遍历(递归实现)
- 数据结构二叉树——建立二叉树、中序递归遍历、非递归遍历、层次遍历
- Java实现二叉树建立以及三种遍历
- C++实现二叉树的建立和三种递归遍历
- 21-数据结构_链式二叉树-遍历实现