求助:急,高手进呀!!
本人接到的是一个关于用c#编程做数据结构动态演示系统(二叉树)。下面这个是关于二叉树遍历的算法,我调试不出来,请帮帮忙,还有如何在窗体中实现呢?
本人邮箱号:[email]royijing@[/email]
//declaration of class TreeNode and class Tree.
using System;
namespace BinaryTreeLibrary
{
//class TreeNode declaration
class TreeNode
{
private TreeNode leftNode;//link to left child
private int data;//data stored in node
private TreeNode rightNode;//link to right child
//initialize data and make this a leaf node
public TreeNode( int nodeData )
{
data =nodeData;
leftNode =rightNode =null;//node has no children
}//end construtor
//leftNode property
public TreeNode leftNode
{
get{ return leftNode;}//end get
set{leftNode = value;}//end set
}//end property LeftNode
//Data property
public int data
{
get{return data;}//end get
set{data = value;}//end set
}//end property Data
//RightNode property
public TreeNode RightNode
{
get{return rightNode;}//end get
set{rightNode = value;}//end set
}//end property Right Node
//insert TreeNode into Tree that contains nodes;
//ignore duplicate values
public void Insert( IComparable insertValue )
{
if ( ( data ) <0)
{
//insert in left subtree
if(leftNode == null )
leftNode = new TreeNode( insertValue );
else // continue travesing left subtree
leftNode.Insert( insertValue );
} //end if
else if( (data)>0 )
{
// insert in right subtree
if(right == null)
rightNode == new TreeNode(insertValue );
else // continue traversing right subtree
rightNode.Insert( insertValue );
} // end else if
}//end method Insert
} //end class TreeNode
//class Tree declaration(书的类型声明)
public class Tree
{
private TreeNode root;
//construct an empty Tree of Integers
public Tree()
{
root = null;
} // end constructor
//insert a new node in the binary search tree.
//if the root node is null,creat the root node here.
//Otherwise,call the insert method of class TreeNode.
public void InsertNode( int insertValue )
{
if(root == null)
root = new TreeNode ( insertValue );
else
root.Insert( insertValue );
}//end method InsertNode
//begin preorder traversal
public void PreorderTraversal()
{
PreorderHelper( root );
}//end method PreorderTraversal
//recusive method to perform preorder traversal
private void PreorderHelper( TreeNode node )
{
if( node==null )
return;
//output node data
Console.Write( node.Data+" " );
//traverse left subtree
preorderHelper( node.LeftNode );
//traverse right subtree
preorderHelper( node.RightNode );
}//end method PreorderHelper
//begin inorder traversal
public void InorderTraversal()
{
InorderHelper( root );
}//end method InorderTraversal
//recursive method to perform inorder traversal
private void InorderHelper( TreeNode node )
{
if ( node==null )
return;
//traverse left subtree
InorderHelper( node.LeftNode );
//output node data
Console.Write( node.Data+" ");
//traverse right subtree
InorderHelper( node.RightNode );
}//end method InorderHelper
//begin postorder traversal
public void PostorderTraversal()
{
PostorderHelper( root );
}//end method PostorderTraversal
//recursive method to perform postorder traversal
private void PostorderHelper( TreeNode node)
{
if ( node==null )
return;
//traverse left subtree
postorderHelper( node.LeftNode );
//traverse right subtree
postorderHelper( node.RightNode );
//output node data
Console.Write( node.Data+" ");
}//end method PostorderHelper
}//end class Tree
}//end namespace BinaryTreeLibrary