参考: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. CentOS 7 rsync

    CentOS 7 rsync 1)软件简介 Rsync 是一个远程数据同步工具,可通过 LAN/WAN 快速同步多台主机间的文件.Rsync 本来是用以取代rcp 的一个工具,它当前由 Rsync.s ...

  2. git 使用整理

    git使用 Ubuntu 14.04 安装 apt-get install git 版本查看 git --version git version 配置(全局变量,默认值.可在具体仓库中设置改仓库使用的 ...

  3. 抛弃jQuery,拥抱原生JavaScript

    前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...

  4. HDU 5965 三维dp 或 递推

    题意:= =中文题 思路一:比赛时队友想的...然后我赛后想了一下想了个2维dp,但是在转移的时候,貌似出了点小问题...吧?然后就按照队友的思路又写了一遍. 定义dp[i][j][k],表示第i列, ...

  5. 【转】linux ls -l的详解

    原文:http://blog.csdn.net/sjzs5590/article/details/8254527 以root的家目录为例: 可以看到,用ls -l命令查看某一个目录会得到一个7个字段的 ...

  6. 使用jstl标签遍历双层的map(map下面的map)

    <c:forEach var="firstMap" items="${map}"> <c:forEach var="secondMa ...

  7. Spring Boot 系列教程8-EasyUI-edatagrid扩展

    edatagrid扩展组件 edatagrid组件是datagrid的扩展组件,增加了统一处理CRUD的功能,可以用在数据比较简单的页面. 使用的时候需要额外引入jquery.edatagrid.js ...

  8. HttpSession详解

    HttpSession详解   session的机制 http是无状态的协议,客户每次读取web页面时,服务器都打开新的会话,而且服务器也不会自动维护客户的上下文信息,那么要怎么才能实现会话跟踪呢?s ...

  9. Java获取来访者IP

    在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...

  10. 使用RPM包安装、配置和拆卸MySQL

    通过rpm包安装.配置及卸载mysql的详细过程. 以MySQL-server-4.0.14-0.i386.rpm为例,放在/usr/src目录下 cd /usr/src rpm -ivh MySQL ...