用C#编写一个windows窗口程序,构建一个形如下图的完全二叉树,并1到10000整数存储在此树中,然后用TreeView控件按二叉树结构将树中的各节点值显示出来。
31
/ \
15 79
/ \ / \
10 23 67 99
/
1请以中序遍历此树的所有节点,并将值按序存储到一个双向链表中,并将各个节点值
显示在窗体中!
31
/ \
15 79
/ \ / \
10 23 67 99
/
1请以中序遍历此树的所有节点,并将值按序存储到一个双向链表中,并将各个节点值
显示在窗体中!
解决方案 »
- richtextbox控件中的内容剪切问题之如何判断是剪切内容包含图片
- Application.DoEvents()是什么意思?
- 改变文本框内部分字体颜色
- 转换到vs2005,hashtable出现了问题
- 求问:实现PC端连接手机,从PC端直接发短信到手机的!
- C#WINFORM程序怎么控制点击网页中超链接啊
- 一个小问题
- 我想自己开发一个制作微请柬的软件,可是没有思路,请各位大神支招
- c#winform程序怎么判断浏览器是否打开,如果打开则强制关闭
- 如何回到Form1?
- Microsoft Visual Studio .NET 2003的安装问题~急
- 在C#程序中向dbf文件插入时间类型数据问题
* Node.cs
* **************************************/using System;namespace binaryTree
{
/// <summary>
/// Summary description for Node.
/// </summary>
public class Node
{
public Node left;
public Node right;
public int currentValue; public Node(int i)
{
currentValue=i;
} }
}
* Form1.cs
* ********************************************************** */using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace binaryTree
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox tbNum;
private System.Windows.Forms.Button btnInsert;
private System.Windows.Forms.TreeView treeView;
private System.Windows.Forms.Button btnShowTree;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.TextBox tbTraverseResult;
private System.Windows.Forms.Button btnTraverse;
private Node Root;
private delegate void OperateNode(int i);
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent(); //
// TODO: Add any constructor code after InitializeComponent call
//
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.tbNum = new System.Windows.Forms.TextBox();
this.btnInsert = new System.Windows.Forms.Button();
this.treeView = new System.Windows.Forms.TreeView();
this.btnShowTree = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.tbTraverseResult = new System.Windows.Forms.TextBox();
this.btnTraverse = new System.Windows.Forms.Button();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 16);
this.label1.TabIndex = 0;
this.label1.Text = "请插入数字";
//
// tbNum
//
this.tbNum.Location = new System.Drawing.Point(104, 40);
this.tbNum.Name = "tbNum";
this.tbNum.TabIndex = 1;
this.tbNum.Text = "";
//
// btnInsert
//
this.btnInsert.Location = new System.Drawing.Point(216, 40);
this.btnInsert.Name = "btnInsert";
this.btnInsert.Size = new System.Drawing.Size(48, 23);
this.btnInsert.TabIndex = 2;
this.btnInsert.Text = "Insert";
this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
//
// treeView
//
this.treeView.ImageIndex = -1;
this.treeView.Location = new System.Drawing.Point(24, 120);
this.treeView.Name = "treeView";
this.treeView.SelectedImageIndex = -1;
this.treeView.Size = new System.Drawing.Size(248, 152);
this.treeView.TabIndex = 3;
//
// btnShowTree
//
this.btnShowTree.Location = new System.Drawing.Point(176, 280);
this.btnShowTree.Name = "btnShowTree";
this.btnShowTree.Size = new System.Drawing.Size(96, 23);
this.btnShowTree.TabIndex = 4;
this.btnShowTree.Text = "Show TreeView";
this.btnShowTree.Click += new System.EventHandler(this.btnShowTree_Click);
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(16, 16);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(264, 64);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Insert to BinaryTree";
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(16, 96);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(264, 216);
this.groupBox2.TabIndex = 6;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "TreeView";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.btnTraverse);
this.groupBox3.Controls.Add(this.tbTraverseResult);
this.groupBox3.Location = new System.Drawing.Point(16, 328);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(264, 128);
this.groupBox3.TabIndex = 7;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Traverse And Show";
//
// tbTraverseResult
//
this.tbTraverseResult.Location = new System.Drawing.Point(8, 24);
this.tbTraverseResult.Multiline = true;
this.tbTraverseResult.Name = "tbTraverseResult";
this.tbTraverseResult.Size = new System.Drawing.Size(240, 64);
this.tbTraverseResult.TabIndex = 0;
this.tbTraverseResult.Text = "";
//
// btnTraverse
//
this.btnTraverse.Location = new System.Drawing.Point(176, 96);
this.btnTraverse.Name = "btnTraverse";
this.btnTraverse.TabIndex = 1;
this.btnTraverse.Text = "Traverse";
this.btnTraverse.Click += new System.EventHandler(this.btnTraverse_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 462);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.btnShowTree);
this.Controls.Add(this.treeView);
this.Controls.Add(this.btnInsert);
this.Controls.Add(this.tbNum);
this.Controls.Add(this.label1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false); }
#endregion /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void InsertNode(int i,ref Node myRoot)
{
if(myRoot==null)
{
myRoot=new Node(i);
return;
}
else
{
if(i>myRoot.currentValue)
{
InsertNode(i,ref myRoot.right);
}
else if(i<myRoot.currentValue)
{
InsertNode(i,ref myRoot.left);
}
else
{
return;
}
}
} private void TraverseNode(Node myRoot,OperateNode op)
{
if(myRoot!=null)
{
if(myRoot.left!=null)
{
TraverseNode(myRoot.left,op);
}
op(myRoot.currentValue);
if(myRoot.right!=null)
{
TraverseNode(myRoot.right,op);
}
}
} private void btnInsert_Click(object sender, System.EventArgs e)
{
int i=0;
try
{
i=int.Parse(this.tbNum.Text);
}
catch
{
MessageBox.Show("错误的输入");
}
InsertNode(i,ref Root);
} private void btnShowTree_Click(object sender, System.EventArgs e)
{
TreeNode treeBeginRoot=new TreeNode("树型样式");
this.treeView.Nodes.Clear();
this.treeView.Nodes.Add(treeBeginRoot);
BulidTree(Root,treeBeginRoot); } private void BulidTree(Node myRoot,TreeNode parentNode)
{
if(myRoot!=null)
{
TreeNode treeNode=new TreeNode(myRoot.currentValue.ToString());
parentNode.Nodes.Add(treeNode);
BulidTree(myRoot.right,treeNode);
BulidTree(myRoot.left,treeNode);
}
} private void btnTraverse_Click(object sender, System.EventArgs e)
{
this.TraverseNode(Root,new OperateNode(this.ShowInTextBox));
} private void ShowInTextBox(int i)
{
this.tbTraverseResult.Text+=string.Format("{0} ",i.ToString());
} }
}