数据结构学习(1):搜索二叉树

本文介绍了一种二叉搜索树的实现方式,包括插入、查找等基本操作,并提供了完整的C#代码示例。
None.gifusing System;
None.gif
using System.IO;
None.gif
None.gif
namespace BinaryTreeSearch
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Class1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    class BinaryTreeSearch
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加代码以启动应用程序
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
public void init()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//构造一个空的二叉搜索树开始
InBlock.gif
            BinarySearchTree T=new BinarySearchTree();//T初始为空
InBlock.gif

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class BinarySearchTree
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//BinarySearchTree中有一个存储指向二叉搜索树根结点指针(或存有证明为空树
InBlock.gif        
//的null)的私有数据域
InBlock.gif
        private TreeNode rootNode;
InBlock.gif        
//BinarySearchTree的各种方法
InBlock.gif        
//Insert方法所用的辅助方法
InBlock.gif
        private TreeNode insertKey(TreeNode T,ComparisonKey K)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(T==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TreeNode N
=new TreeNode();//构建一个新的TreeNode
InBlock.gif
                N.key=K;
InBlock.gif                
return N;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(K.compareTo(T.key)<0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    T.llink
=insertKey(T.llink,K);
InBlock.gif                    
return T;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    T.rlink
=insertKey(T.rlink,K);
InBlock.gif                    
return T;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
//end insertKey
InBlock.gif

InBlock.gif
InBlock.gif        
void insert(string K)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            rootNode
=insertKey(rootNode,new StringKey(K));
ExpandedSubBlockEnd.gif        }
//end insert
InBlock.gif

InBlock.gif        TreeNode find(ComparisonKey K)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TreeNode T
=rootNode;
InBlock.gif            
int result;
InBlock.gif
InBlock.gif            
while(T!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if((result=K.compareTo(T.key))<0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    T
=T.llink;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if(result==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return T;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    T
=T.rlink;
ExpandedSubBlockEnd.gif                }
//end if
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
return T;//如果搜索失败 返回null
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        TreeNode find(String K)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return find(new StringKey(K));
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class TreeNode
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ComparisonKey key;
InBlock.gif        TreeNode llink;
InBlock.gif        TreeNode rlink;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public interface ComparisonKey
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//        如果K1和K2均为ComparisonKey,那么K1.compareTo(k2)就会有三个值0,1,-1,
InBlock.gif        
//        就是K1==K2,K1>K2,K1<K2顺序是compareTo方法定义的优先级别顺序
InBlock.gif
        int compareTo(ComparisonKey value);
InBlock.gif        
//将ComparisonKey转换成可以打印的字符串
InBlock.gif
        string toOurString();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class PQItem:ComparisonKey
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
private int key;//key数据包括给出元素优先级别的整数关键字
InBlock.gif
        public PQItem(int value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            key
=value;
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
ComparisonKey 成员#region ComparisonKey 成员
InBlock.gif
InBlock.gif        
public int compareTo(ComparisonKey value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int a=this.key;
InBlock.gif            
int b=((PQItem)value).key;
InBlock.gif            
return ((a==b)?0:((a>b)?1:-1));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string toOurString()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return this.key.ToString();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/tyrael007/archive/2006/12/14/592450.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值