C#平衡树(AVLTree)
参考: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)的更多相关文章
- AVLTree 平衡树
//测试数据//第一组:7个输入,测试LL型,40,36,44,32,38,28,24://第二组:7个输入,测试RR型,40,36,44,43,48,52,56://第三组:7个输入,测试LR型,4 ...
- 平衡树(AVL)详解
1. 为什么平衡树? 在二叉搜索树(BST,Binary Search Tree)中提到,BST树可能会退化成一个链表(整棵树中只有左子树,或者只有右子树),这将大大影响二叉树的性能. 前苏联科学家G ...
- 二叉排序树的创建删除中序输出&&平衡树
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...
- 实现Avl平衡树
实现Avl平衡树 一.介绍 AVL树是一种自平衡的二叉搜索树,它由Adelson-Velskii和 Landis于1962年发表在论文<An algorithm for the organi ...
- 二叉树,平衡树,红黑树,B~/B+树汇总
二叉查找树(BST),平衡二叉查找树(AVL),红黑树(RBT),B~/B+树(B-tree).这四种树都具备下面几个优势: (1) 都是动态结构.在删除,插入操作的时候,都不需要彻底重建原始的索引树 ...
- 平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】
平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是 ...
- java项目---用java实现二叉平衡树(AVL树)并打印结果(详)(3星)
package Demo; public class AVLtree { private Node root; //首先定义根节点 private static class Node{ //定义Nod ...
- Algorithms: 二叉平衡树(AVL)
二叉平衡树(AVL): 这个数据结构我在三月份学数据结构结构的时候遇到过.但当时没调通.也就没写下来.前几天要用的时候给调好了!详细AVL是什么,我就不介绍了,维基百科都有. 后面两月又要忙了. ...
- 平衡树以及AVL树
平衡树是计算机科学中的一类数据结构. 平衡树是计算机科学中的一类改进的二叉查找树.一般的二叉查找树的查询复杂度是跟目标结点到树根的距离(即深度)有关,因此当结点的深度普遍较大时,查询的均摊复杂度会上升 ...
随机推荐
- ios屏幕
设备 屏幕尺寸 分辨率(pt) Reader 分辨率(px) 渲染后 PPI iPhone 3GS 3.5吋 320x480 @1x 320x480 163 iPhone 4/4s 3.5吋 32 ...
- 关于spring的注解方式注入默认值(转) -- 首字母小写
1.是首字母小写 比如 UserAction对应的id是userAction 可以通过ApplicationContext 对象的act.getBean("userAction") ...
- html 页面视图中的资源文件(css/js/image)的路径问题。
说到html 页面视图中的资源文件的路径引用问题,这个问题以前一直没去弄明白.今天,我将公司新开发的一个项目完全移植到我本地搭建的php 环境中来,遇到了这个问题,想了一下,然后也不是很困难的就把它给 ...
- GDT、GDTR、LDT、LDTR的理解
GDT是全局描述附表,主要存放操作系统和各任务公用的描述符,如公用的数据和代码段描述符.各任务的TSS描述符和LDT描述符.(TSS是任务状态段,存放各个任务私有运行状态信息描述符)LDT是局部描述符 ...
- Ztree当节点没有下级时不显示下拉图标
select o.*,(select count(*) from sys_org t where t.orgsupid=o.orgid) isLeaf from sys_org o where 1=1
- st-Spanning Tree
st-Spanning Tree time limit per test 4 seconds memory limit per test 256 megabytes input standard in ...
- java synchronized 线程同步机制详解
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...
- 命令行从Android手机中导出已安装APK的方法调研
一.背景 二.步骤 一.背景 很多时候,APK文件只存在于应用市场,在PC上无法直接下载.用手机下载下来后就直接安装了,也不能保存原始的APK文件. APK安装到手机后,Android系统会保存一份和 ...
- 关于在jsp中的表达式
列子: <%List<F_dd_tourist_info_markup> tourists = (List<F_dd_tourist_info_markup>) requ ...
- android执行外部命令、检测文件是否存在、自动检测U盘路径
private final String UDiskFileName = "/2969_logo/bootfile.image"; private final String Loc ...