C#实现平衡二叉查找树

using System;
using System.Collections;

namespace DataStructure
{
    
/// <summary>
    
/// AVLTree 的摘要说明。-----平衡二叉查找树
    
/// </summary>

    public class AVLTree:BST
    
{
        
protected int height;//空树的高定义为-1;
        
//构造一棵空的二叉查找树

        
public AVLTree():base()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
            height=-1;
        }


        
public AVLTree(object _obj):base(_obj)
        
{
            height
=0;
        }


        
//------------------------------------------------------------------
        protected override object GetEmptyInstance(uint _degree)
        
{    return new AVLTree(); }

        
//------------------------------------------------------------------
        protected int BalanceFactor()
        
{
            
if (this.IsEmpty() )
                
return 0;
            
return ((AVLTree)this.Left).height-((AVLTree)this.Right).height;
        }


        
//调整高度
        protected void AdjustHeight()
        
{
            
this.height=Math.Max( 
                ((AVLTree)
this.Left).height, ((AVLTree)this.Right).height)+1;     
        }


        
//平衡时的四种旋转方式
        protected void LLRotation()
        
{
            
ifthis.IsEmpty() )
                
throw new Exception("My:invalid operation!");
            AVLTree avlB
=new AVLTree(this.key);
            avlB.AttachSubtree(
1,(AVLTree)this[0][1]);
            avlB.AttachSubtree(
2,(AVLTree)this[1]);
        
            
this.key=this[0].Key;
            
this[0]=this[0][0];
            
this[1]=avlB;

            
//调整两个节点的高度
            ((AVLTree)this.Right).AdjustHeight();
            
this.AdjustHeight();
        }


        
protected void LRRotation()
        
{
            
ifthis.IsEmpty() )
                
throw new Exception("My:invalid operation!");
            ((AVLTree)
this.Left).RRRotation();
            
this.LLRotation();
        }


        
protected void RRRotation()
        
{
            
ifthis.IsEmpty() )
                
throw new Exception("My:invalid operation!");
            AVLTree avlB
=new AVLTree(this.key);
             
            avlB.AttachSubtree(
1,(AVLTree)this[0]);
            avlB.AttachSubtree(
2,(AVLTree)this[1][0]);

            
this.key=this[1].Key;
            
this[0]=avlB;
            
this[1]=this[1][1];

            
//调整两个节点的高度
            ((AVLTree)this.Left).AdjustHeight();
            
this.AdjustHeight();
        }


        
protected void RLRotation()
        
{
            
ifthis.IsEmpty() )
                
throw new Exception("My:invalid operation!");
            ((AVLTree)
this.Right).LLRotation();
            
this.RRRotation();
        }


        
//---------------override--------------------

        
public override void AttachKey(object _obj)
        
{
            
if(!IsEmpty())
                
throw new Exception("My:this node must be a empty tree node!");
            
this.key=_obj;
            
//产生一个degree长的数组,并将其初始化为空树
            this.treeList=new ArrayList();
            
this.treeList.Capacity=(int)this.degree;
            
for(int i=0;i<this.degree;i++)
            
{
                treeList.Add(
new AVLTree());
            }

            
//
            this.height=0;
        }


        
//在改动树的结构后平衡树
        public override void Balance()
        
{
            
this.AdjustHeight();
            
//大于1则说明不平衡
            if( Math.Abs(this.BalanceFactor())>1)
            
{
                
if(this.BalanceFactor()>0)
                
{
                    
if (((AVLTree)this.Left).BalanceFactor()>0)
                        
this.LLRotation();
                    
else
                        
this.LRRotation();
                }
                  
                
else
                
{
                    
if (((AVLTree)this.Right).BalanceFactor()<0)
                        
this.RRRotation();
                    
else
                        
this.RLRotation();
                }

            }

        }


        
public int Height{get{return this.height;}}
    }

}

C#实现二叉树

using System;
using System.Collections;

namespace DataStructure
{
    
/// &lt;summary&gt;
    
/// BinaryTree 的摘要说明。
    
/// &lt;/summary&gt;

    public class BinaryTree:NaryTree
    
{
        
//构造二叉空树
        public BinaryTree():base(2)
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

         
        
public BinaryTree(object _obj):base(2,_obj)
        
{
  
        }


        
//------------------------------------------------------------------
        protected override object GetEmptyInstance(uint _degree)
        
{
            
return new BinaryTree(_degree);
        }

        
//------------------------------------------------------------------
 
        
//重写深度遍历
        public override void DepthFirstTraversal(IPrePostVisitor _vis)
        
{
            
if ( !IsEmpty() )
            
{
                _vis.PreVisit(
this.Key);
                
this[0].DepthFirstTraversal(_vis);
                _vis.Visit(
this.Key);
                
this[1].DepthFirstTraversal(_vis);
                _vis.PostVisit(
this.Key);
            }

        }



        
//二叉树大小的比较
        
//先比较关键字,如果相等,再比较左子树,如果再相等,则比较右子树----如此递归
        #region IComparable 成员

        
public override int CompareTo(object obj)
        
{
            
// TODO: 添加 BinaryTree.CompareTo 实现
            
//因为Comare()中已经进行了类型断定,故不会出现转型错误
            BinaryTree tmpTree=(BinaryTree)obj;
 
            
ifthis.IsEmpty() )
                
return tmpTree.IsEmpty()?0:-1;
            
if( tmpTree.IsEmpty() )
                
return 1;
            
int result=Comparer.Default.Compare(this,tmpTree);
            
if(result==0)
                result
=this[0].CompareTo(tmpTree[0]);
            
if(result==0)
                result
=this[1].CompareTo(tmpTree[1]);
 
            
return result;
        }


        
#endregion

    }

}

C#希尔排序

using System; 
namespace ShellSorter 

    
public class ShellSorter 
    

        
public void Sort(int [] list) 
        

            
int inc; 
            
for(inc=1;inc<=list.Length/9;inc=3*inc+1); 
            
for(;inc>0;inc/=3
            

                
for(int i=inc+1;i<=list.Length;i+=inc) 
                

                    
int t=list[i-1]; 
                    
int j=i; 
                    
while((j>inc)&&(list[j-inc-1]>t)) 
                    

                        list[j
-1]=list[j-inc-1]; 
                        j
-=inc; 
                    }
 
                    list[j
-1]=t; 
                }
 
            }
 
        }
 
    }
 
    
public class MainClass 
    

        
public static void Main() 
        

            
int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47}
            ShellSorter sh
=new ShellSorter(); 
            sh.Sort(iArrary); 
            
for(int m=0;m<iArrary.Length;m++
                Console.Write(
"{0} ",iArrary[m]); 
            Console.WriteLine(); 
        }
 
    }
 
}
 

C#实现插入排序

using System; 
namespace InsertionSorter 

    
public class InsertionSorter 
    

        
public void Sort(int [] list) 
        

            
for(int i=1;i<list.Length;i++
            

                
int t=list[i]; 
                
int j=i; 
                
while((j>0)&&(list[j-1]>t)) 
                

                    list[j]
=list[j-1]; 
                    
--j; 
                }
 
                list[j]
=t; 
            }
 
        }
 
    }
 
    
public class MainClass 
    

        
public static void Main() 
        

            
int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47}
            InsertionSorter ii
=new InsertionSorter(); 
            ii.Sort(iArrary); 
            
for(int m=0;m<iArrary.Length;m++
                Console.Write(
"{0}",iArrary[m]); 
            Console.WriteLine(); 
        }
 
    }
 
}
 

C#实现选择排序

using System; 
namespace SelectionSorter 

    
public class SelectionSorter 
    

        
private int min; 
        
public void Sort(int [] list) 
        

            
for(int i=0;i<list.Length-1;i++
            

                min
=i; 
                
for(int j=i+1;j<list.Length;j++
                

                    
if(list[j]<list[min]) 
                        min
=j; 
                }
 
                
int t=list[min]; 
                list[min]
=list[i]; 
                list[i]
=t; 
            }
 
   
   
        }
 
    }
 
    
public class MainClass 
    

        
public static void Main() 
        

            
int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47}
            SelectionSorter ss
=new SelectionSorter(); 
            ss.Sort(iArrary); 
            
for(int m=0;m<iArrary.Length;m++
                Console.Write(
"{0} ",iArrary[m]); 
            Console.WriteLine(); 
        }
 
    }
 
}
 

C#实现冒泡排序

using System; 

   
namespace BubbleSorter 

    
public class BubbleSorter 
    

        
public void Sort(int [] list) 
        

            
int i,j,temp; 
            
bool done=false
            j
=1
            
while((j<list.Length)&&(!done)) 
            

                done
=true
                
for(i=0;i<list.Length-j;i++
                

                    
if(list[i]>list[i+1]) 
                    

                        done
=false
                        temp
=list[i]; 
                        list[i]
=list[i+1]; 
                        list[i
+1]=temp; 
                    }
 
                }
 
                j
++
            }
 
   
   
        }
 
    }
 
    
public class MainClass 
    

        
public static void Main() 
        

            
int[] iArrary=new int[ ]{1,5,13,6,10,55,99,2,87,12,34,75,33,47}
            BubbleSorter sh
=new BubbleSorter(); 
            sh.Sort(iArrary); 
            
for(int m=0;m<iArrary.Length;m++
                Console.Write(
"{0} ",iArrary[m]); 
            Console.WriteLine(); 
        }
 
    }
 
}