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

None.gif using  System;
None.gif
None.gif
namespace  BinaryTreeLibrary
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif     
/**////创建的是二叉查找树,没有重复的结点值
InBlock.gif
ExpandedSubBlockEnd.gif     
///特点:左支树中任何值都小于父结点值,右结点任何值大于父结点值

InBlock.gif
InBlock.gif     
public class TreeNode
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif
InBlock.gif         
private TreeNode leftNode;
InBlock.gif
InBlock.gif         
private TreeNode rightNode;
InBlock.gif
InBlock.gif         
private int data;
InBlock.gif
InBlock.gif         
public TreeNode(int nodeData)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif
InBlock.gif              data 
= nodeData;
InBlock.gif
InBlock.gif              leftNode 
= rightNode = null;//没有子树
InBlock.gif

ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public TreeNode LeftNode
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
get
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   
return leftNode;
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
InBlock.gif              
set
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   leftNode 
= value;
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public TreeNode RightNode
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
get
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   
return rightNode;
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
InBlock.gif              
set
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   rightNode 
= value;
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public int Data
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
get
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   
return data;
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
InBlock.gif              
set
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   data 
= value;
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

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

ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
if(insertValue<data)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   
if(leftNode == null)
InBlock.gif
InBlock.gif                       leftNode 
= new TreeNode(insertValue);
InBlock.gif
InBlock.gif                   
else
InBlock.gif
InBlock.gif                       leftNode.Insert(insertValue);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
InBlock.gif              
else if(insertValue >data)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   
if(rightNode == null)
InBlock.gif
InBlock.gif                       rightNode 
= new TreeNode(insertValue);
InBlock.gif
InBlock.gif                   
else
InBlock.gif
InBlock.gif                       rightNode.Insert(insertValue);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
ExpandedSubBlockEnd.gif     }

InBlock.gif
InBlock.gif     
public class Tree
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif
InBlock.gif         
private TreeNode root;
InBlock.gif
InBlock.gif         
public Tree()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              root 
= null;
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public void InsertNode(int insertValue)//创建树
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
lock(this)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   
if(root == null)
InBlock.gif
InBlock.gif                       root 
= new TreeNode(insertValue);
InBlock.gif
InBlock.gif                   
else
InBlock.gif
InBlock.gif                       root.Insert(insertValue);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public void PreorderTraversal()//前序遍历
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
lock(this)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   PreorderHelper(root);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

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

ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
if(node == null)
InBlock.gif
InBlock.gif                   
return;
InBlock.gif
InBlock.gif              Console.Write(node.Data
+" ");
InBlock.gif
InBlock.gif              PreorderHelper(node.LeftNode);
InBlock.gif
InBlock.gif              PreorderHelper(node.RightNode);
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public void InorderTraversal()//中序遍历
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
lock(this)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   InorderHelper(root);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public void  InorderHelper(TreeNode node)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
if(node == null)
InBlock.gif
InBlock.gif                   
return;
InBlock.gif
InBlock.gif              InorderHelper(node.LeftNode);
InBlock.gif
InBlock.gif              Console.Write(node.Data 
+" ");
InBlock.gif
InBlock.gif              InorderHelper(node.RightNode);
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public void PostorderTraversal()//后序遍历
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
lock(this)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   PostorderHelper(root);
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
public void PostorderHelper(TreeNode node)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
if(node == null)
InBlock.gif
InBlock.gif                   
return;
InBlock.gif
InBlock.gif              PostorderHelper(node.LeftNode);
InBlock.gif
InBlock.gif              PostorderHelper(node.RightNode);
InBlock.gif
InBlock.gif              Console.Write(node.Data 
+" ");
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
ExpandedSubBlockEnd.gif     }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
posted on 2005-08-30 18:41 海盗 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Grisson/archive/2005/08/30/226404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值