| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 676 人关注过本帖
标题:求助:急,高手进呀!!
只看楼主 加入收藏
royijing
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-5-28
收藏
 问题点数:0 回复次数:1 
求助:急,高手进呀!!
本人接到的是一个关于用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
搜索更多相关主题的帖子: 二叉树 邮箱 TreeNode class 
2008-05-28 02:01
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
A:不是高手能进吗?
B:不能!
A:可我进啦......
B:那请你出去,并擦干净...
A......

msdn == 葵花宝典!!!
QQ:122768959
2008-05-28 09:03
快速回复:求助:急,高手进呀!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047484 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved