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树
平衡树是计算机科学中的一类数据结构. 平衡树是计算机科学中的一类改进的二叉查找树.一般的二叉查找树的查询复杂度是跟目标结点到树根的距离(即深度)有关,因此当结点的深度普遍较大时,查询的均摊复杂度会上升 ...
随机推荐
- device-mapper: multipath: Failing path recovery【转载】
digoal 2016-04-05 10:09:42 浏览180 评论0 摘要: 由于扇区损坏导致多路径设备failed. 现象如下 : # dmesg : device-mapper: mul ...
- PHP字符串函数试题
Ctrl+A查看答案 1.把ASCII字符的字符串转换为十六进制值的函数是什么?答:bin2hex($string),例如bin2hex('ab') = 6162 2.ASCII码转字符,字符转ASC ...
- 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D
http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...
- Qt多线程编程总结(一)
http://blog.csdn.net/mznewfacer/article/details/6965799 QMutex类 一个线程可以锁定互斥量,并且在它锁定之后,其它线程就不能再锁定这个互斥量 ...
- php 特定类型测试函数
is_array() 检查变量是否是数组 is_double().is_float().is_real() 检查变量是否是浮点数 is_long().is_int().is_integer()检查变量 ...
- 第一次安装ubuntu要设置的东西
1. 安装网卡驱动 lscpi 查看网卡型号 根据型号找到驱动源码 下载下来并编译 安装 2. 编译安卓源码的时候出现jdk型号不对的情况 把/usr/bin/java 删除,就可以了.
- THE ROAD TO PROGRAM
<The C Programming Language> <The Practice of Programming><The Art of Computer Progra ...
- js屏蔽浏览器右键菜单
<script type="text/javascript"> function doNothing(){ window.event.returnValue=false ...
- 自定义开关ToggleButton
package com.example.test;import android.os.Bundle;import android.app.Activity;import android.view.Me ...
- Lucene 简单手记http://www.cnblogs.com/hoojo/archive/2012/09/05/2671678.html
什么是全文检索与全文检索系统? 全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查 ...