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树
平衡树是计算机科学中的一类数据结构. 平衡树是计算机科学中的一类改进的二叉查找树.一般的二叉查找树的查询复杂度是跟目标结点到树根的距离(即深度)有关,因此当结点的深度普遍较大时,查询的均摊复杂度会上升 ...
随机推荐
- MySQL数据备份和恢复
1.数据备份 mysqldump -uroot -p databasename > file.sql 2.数据还原 mysql -u root -p databasename < file ...
- myeclipse 之 快捷键
简单记录一下,新装了个机器,win7系统,想设置一下自己习惯的快捷键 如:ctrl+alt+方向键,复制行,发现设置不上,原先的自带的也失效,设置一下ctrl+alt+其它键,ok可以使用,这说明某些 ...
- Clock Pictures
Clock Pictures 题目描述 You have two pictures of an unusual kind of clock. The clock has n hands, each h ...
- Dense Subsequence
Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Timewarp 一种生成当中帧技术,异步时间扭曲(Asynchronous Timewarp)
翻译: https://www.oculus.com/blog/asynchronous-timewarp/ 异步时间扭曲(Asynchronous Timewarp 时间扭曲,即调整时长) 关 ...
- ReactiveX序列——RxSwift 浅析
ReactiveX序列——RxSwift Swift是苹果公司新推出的一门现代化的编程语言,并且将其开源出来了,Swift具有很多的优点,这也使得这门语言推出的短时间引起了很大反应的原因,在最近的 ...
- Java中常见的5种WEB服务器介绍
这篇文章主要介绍了Java中常见的5种WEB服务器介绍,它们分别是Tomcat.Resin.JBoss.WebSphere.WebLogic,需要的朋友可以参考下 Web服务器是运行及发布Web应用的 ...
- php下载文件的一种方式
<?php ob_start(); // $file_name="cookie.jpg"; $file_name="abc.jpg"; //用以解决中文不 ...
- python 函数部分
#初始化 def init(data): data['first']={} data['middle']={} data['last']={} #查看条件 def lookup(data,label, ...
- ubuntu apache 安装awstats 流量分析工具(命令方式)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...