参考:http://www.cnblogs.com/skywang12345/p/3577479.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Collections; namespace ConsoleApplication2
{
public class Program
{
public static void Main()
{
int[] arr = { , , , , , , , , , , , , , , , }; AVLTree<int> avlTree = new AVLTree<int>();
for (int i = ; i < arr.Length; i++)
{
avlTree.Insert(arr[i]);
Console.Write(arr[i] + " ");
}
Console.WriteLine(); Console.Write("层遍历:");
avlTree.LevelOrder();
Console.WriteLine(); Console.Write("删除节点15:");
avlTree.Remove();
avlTree.LevelOrder();
Console.WriteLine(); Console.Write("删除节点16:");
avlTree.Remove();
avlTree.LevelOrder();
Console.WriteLine(); Console.Write("删除节点7:");
avlTree.Remove();
avlTree.LevelOrder();
Console.WriteLine(); Console.Read();
}
} public class AVLTreeNote<TKey> where TKey : IComparable
{
public AVLTreeNote(TKey key, AVLTreeNote<TKey> leftNote, AVLTreeNote<TKey> rightNote)
{
Key = key;
LeftNote = leftNote;
RightNote = rightNote;
} public TKey Key { get; set; }
public int Height { get; set; }
public AVLTreeNote<TKey> LeftNote { get; set; }
public AVLTreeNote<TKey> RightNote { get; set; } } public class AVLTree<TKey> where TKey : IComparable
{
private AVLTreeNote<TKey> RootNote { get; set; } public AVLTree()
{ } private int GetHeight()
{
return ;
} private int GetHeight(AVLTreeNote<TKey> note)
{
return note == null ? : note.Height;
} private AVLTreeNote<TKey> LeftLeftRotation(AVLTreeNote<TKey> note)
{
AVLTreeNote<TKey> temp = note.LeftNote;
note.LeftNote = temp.RightNote;
temp.RightNote = note; note.Height = Math.Max(GetHeight(note.LeftNote), GetHeight(note.RightNote)) + ;
temp.Height = Math.Max(GetHeight(temp.LeftNote), GetHeight(temp.RightNote)) + ; return temp;
} private AVLTreeNote<TKey> RightRightRotation(AVLTreeNote<TKey> note)
{
AVLTreeNote<TKey> temp = note.RightNote;
note.RightNote = temp.LeftNote;
temp.LeftNote = note; note.Height = Math.Max(GetHeight(note.LeftNote), GetHeight(note.RightNote)) + ;
temp.Height = Math.Max(GetHeight(temp.LeftNote), GetHeight(temp.RightNote)) + ; return temp;
} private AVLTreeNote<TKey> LeftRightRotation(AVLTreeNote<TKey> note)
{
note.LeftNote = RightRightRotation(note.LeftNote);
return LeftLeftRotation(note);
} private AVLTreeNote<TKey> RightLeftRotation(AVLTreeNote<TKey> note)
{
note.RightNote = LeftLeftRotation(note.RightNote);
return RightRightRotation(note);
} public void Insert(TKey key)
{
RootNote = Insert(key, RootNote);
} private AVLTreeNote<TKey> Insert(TKey key, AVLTreeNote<TKey> note)
{
if (note == null)
{
note = new AVLTreeNote<TKey>(key, null, null);
}
else
{
if (key.CompareTo(note.Key) < )
{
note.LeftNote = Insert(key, note.LeftNote); if (Math.Abs(GetHeight(note.LeftNote) - GetHeight(note.RightNote)) == )
{
if (key.CompareTo(note.LeftNote.Key) < )//其实这里判断就像知道新增加的子节点属于左节点还是右节点 画图的话 一目了然
{
note = LeftLeftRotation(note);
}
else
{
note = LeftRightRotation(note);
}
}
} if (key.CompareTo(note.Key) > )
{
note.RightNote = Insert(key, note.RightNote); if (Math.Abs(GetHeight(note.RightNote) - GetHeight(note.LeftNote)) == )
{
if (key.CompareTo(note.RightNote.Key) > )//其实这里判断就像知道新增加的子节点属于左节点还是右节点 画图的话 一目了然
{
note = RightRightRotation(note);
}
else
{
note = RightLeftRotation(note);
}
}
}
}
note.Height = Math.Max(GetHeight(note.LeftNote), GetHeight(note.RightNote)) + ;
return note;
} public void Remove(TKey key)
{
Remove(key, RootNote);
} private AVLTreeNote<TKey> Remove(TKey key, AVLTreeNote<TKey> note)
{
if (note == null)
{
return null;
} if (key.CompareTo(note.Key) < )
{
note.LeftNote = Remove(key, note.LeftNote); if (Math.Abs(GetHeight(note.RightNote) - GetHeight(note.LeftNote)) == )
{
AVLTreeNote<TKey> rightNote = note.RightNote; if (GetHeight(rightNote.LeftNote) > GetHeight(rightNote.RightNote))
{
note = RightLeftRotation(note);
}
else
{
note = RightRightRotation(note);
} }
} if (key.CompareTo(note.Key) > )
{
note.RightNote = Remove(key, note.RightNote); if (Math.Abs(GetHeight(note.RightNote) - GetHeight(note.LeftNote)) == )
{
AVLTreeNote<TKey> leftNote = note.LeftNote;
if (GetHeight(leftNote.RightNote) > GetHeight(leftNote.LeftNote))
{
note = LeftRightRotation(note);
}
else
{
note = LeftLeftRotation(note);
}
}
} if (note.Key.CompareTo(key) == )
{
if (note.LeftNote != null && note.RightNote != null)
{
if (GetHeight(note.LeftNote) > GetHeight(note.RightNote))
{
AVLTreeNote<TKey> max = FindMax(note.LeftNote);
note.Key = max.Key;
note.LeftNote = Remove(max.Key, note.LeftNote);
}
else
{
AVLTreeNote<TKey> min = FindMin(note.RightNote);
note.Key = min.Key;
note.RightNote = Remove(min.Key, note.RightNote);
}
}
else
{
note = note.LeftNote == null ? note.RightNote : note.LeftNote;
}
}
return note;
} public void LevelOrder()
{
LevelOrder(RootNote);
} private void LevelOrder(AVLTreeNote<TKey> note)
{
Queue<AVLTreeNote<TKey>> queue = new Queue<AVLTreeNote<TKey>>();
queue.Enqueue(note); while (queue.Count > )
{
var temp = queue.Dequeue(); Console.Write(temp.Key + " "); if (temp.LeftNote != null)
{
queue.Enqueue(temp.LeftNote);
} if (temp.RightNote != null)
{
queue.Enqueue(temp.RightNote);
}
}
} public AVLTreeNote<TKey> FindMin()
{
return FindMin(RootNote);
} private AVLTreeNote<TKey> FindMin(AVLTreeNote<TKey> note)
{
if (note.LeftNote == null)
{
return note;
}
return FindMin(note.LeftNote);
} public AVLTreeNote<TKey> FindMax()
{
return FindMax(RootNote);
} private AVLTreeNote<TKey> FindMax(AVLTreeNote<TKey> note)
{
if (note.RightNote == null)
{
return note;
}
return FindMax(note.RightNote);
}
}
}

C#平衡树(AVLTree)的更多相关文章

  1. AVLTree 平衡树

    //测试数据//第一组:7个输入,测试LL型,40,36,44,32,38,28,24://第二组:7个输入,测试RR型,40,36,44,43,48,52,56://第三组:7个输入,测试LR型,4 ...

  2. 平衡树(AVL)详解

    1. 为什么平衡树? 在二叉搜索树(BST,Binary Search Tree)中提到,BST树可能会退化成一个链表(整棵树中只有左子树,或者只有右子树),这将大大影响二叉树的性能. 前苏联科学家G ...

  3. 二叉排序树的创建删除中序输出&&平衡树

    #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...

  4. 实现Avl平衡树

    实现Avl平衡树   一.介绍 AVL树是一种自平衡的二叉搜索树,它由Adelson-Velskii和 Landis于1962年发表在论文<An algorithm for the organi ...

  5. 二叉树,平衡树,红黑树,B~/B+树汇总

    二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操作的时候,都不需要彻底重建原始的索引树 ...

  6. 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】

    平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...

  7. java项目---用java实现二叉平衡树(AVL树)并打印结果(详)(3星)

    package Demo; public class AVLtree { private Node root; //首先定义根节点 private static class Node{ //定义Nod ...

  8. Algorithms: 二叉平衡树(AVL)

    二叉平衡树(AVL):   这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有.  后面两月又要忙了. ...

  9. 平衡树以及AVL树

    平衡树是计算机科学中的一类数据结构. 平衡树是计算机科学中的一类改进的二叉查找树.一般的二叉查找树的查询复杂度是跟目标结点到树根的距离(即深度)有关,因此当结点的深度普遍较大时,查询的均摊复杂度会上升 ...

随机推荐

  1. Bitmap和Drawable浅谈

    一.概念区别 Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565.RGB8888.作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低.我们理解为一 ...

  2. js css优化-- 合并和压缩

    在项目框架中,首先要引用很多css和js文件,80%的用户响应时间都是浪费在前端.而这些时间主要又是因为下载图片.样式表.JavaScript脚本.flash等文件造成的.减少这些资源文件的Reque ...

  3. File类与FileInfo类区别

    ile类是静态的,FileInfo不是静态的也没有静态的方法,仅可用于实例化的对象.FileInfo方法基本类似于File.关于二者,作何选择. ● 如果仅进行单一方法调用,则可以使用静态File类上 ...

  4. 详细的SQL中datediff用法

    DATEDIFF 函数 [日期和时间] 功能返回两个日期之间的间隔. 语法DATEDIFF ( date-part, date-expression-1, date-expression-2 ) da ...

  5. JSP简单标签标签库开发

    1.定制标签的实现类称为标签处理器,简单标签处理器指实现SimpleTag接口的类,如下图的SimpleTagSupport类,该类为SimpleTag接口的默认实现类. 注:不要直接实现Simple ...

  6. Mysql转化blob为可读

    -- info 为列名 SELECT convert(info using latin1) FROM drupal755.system; SELECT convert(info using utf8) ...

  7. [maven] 新建项目一直提示loading archetype list

    Maven's JRE is running out of memory. Under Build > Build Tools > Maven > Importing, set &q ...

  8. Sea Battle

    Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. iOS 多语言 浅析

    什么是本地化处理? 本地化处理就是我们的应用程序有可能发布到世界的很多国家去,因为每个国家应用的语言是不一样的,所以我们要把我们的应用程序的语言要进行本地化处理一下. 本地化处理需要处理那些文件? ( ...

  10. struts1,struts2,hibernate,spring的运行原理结构图

    一.struts1运行原理 1.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(s ...