今天开博了2007-7-16

开博没有太监的客。。。

 

c#数据结构———二叉查找树

using System;

namespace BinaryTreeLibrary

{

     ///创建的是二叉查找树,没有重复的结点值

     ///特点:左支树中任何值都小于父结点值,右结点任何值大于父结点值

     public class TreeNode

     {

         private TreeNode leftNode;

         private TreeNode rightNode;

         private int data;

         public TreeNode(int nodeData)

          {

              data = nodeData;

              leftNode = rightNode = null;//没有子树

         }

         public TreeNode LeftNode

         {

              get

              {

                   return leftNode;

              }

              set

              {

                   leftNode = value;

              }

         }

         public TreeNode RightNode

         {

              get

              {

                   return rightNode;

              }

              set

              {

                   rightNode = value;

              }

         }

         public int Data

         {

              get

              {

                   return data;

              }

              set

              {

                   data = value;

              }

         }

         public void Insert(int insertValue)//创建结点

         {

              if(insertValue<data)

              {

                   if(leftNode == null)

                       leftNode = new TreeNode(insertValue);

                   else

                       leftNode.Insert(insertValue);

              }

              else if(insertValue >data)

              {

                   if(rightNode == null)

                       rightNode = new TreeNode(insertValue);

                   else

                       rightNode.Insert(insertValue);

              }

         }

     }

     public class Tree

     {

         private TreeNode root;

         public Tree()

         {

              root = null;

         }

         public void InsertNode(int insertValue)//创建树

         {

              lock(this)

              {

                   if(root == null)

                       root = new TreeNode(insertValue);

                   else

                       root.Insert(insertValue);

              }

         }

         public void PreorderTraversal()//前序遍历

         {

              lock(this)

              {

                   PreorderHelper(root);

              }

         }

         private void PreorderHelper(TreeNode node)//采用了递归

         {

              if(node == null)

                   return;

              Console.Write(node.Data+" ");

              PreorderHelper(node.LeftNode);

              PreorderHelper(node.RightNode);

         }

         public void InorderTraversal()//中序遍历

         {

              lock(this)

              {

                   InorderHelper(root);

              }

         }

         public void  InorderHelper(TreeNode node)

         {

              if(node == null)

                   return;

              InorderHelper(node.LeftNode);

              Console.Write(node.Data +" ");

              InorderHelper(node.RightNode);

         }

         public void PostorderTraversal()//后序遍历

         {

              lock(this)

              {

                   PostorderHelper(root);

              }

         }

         public void PostorderHelper(TreeNode node)

         {

              if(node == null)

                   return;

              PostorderHelper(node.LeftNode);

              PostorderHelper(node.RightNode);

              Console.Write(node.Data +" ");

         }

     }

}

posted on 2005-04-25 22:58  胡敏  阅读(1050)  评论(1编辑  收藏  举报

导航