博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#平衡树(AVLTree)
阅读量:5014 次
发布时间:2019-06-12

本文共 5971 字,大约阅读时间需要 19 分钟。

参考: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 = { 3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 };            AVLTree
avlTree = new AVLTree
(); for (int i = 0; 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(15); avlTree.LevelOrder(); Console.WriteLine(); Console.Write("删除节点16:"); avlTree.Remove(16); avlTree.LevelOrder(); Console.WriteLine(); Console.Write("删除节点7:"); avlTree.Remove(7); avlTree.LevelOrder(); Console.WriteLine(); Console.Read(); } } public class AVLTreeNote
where TKey : IComparable { public AVLTreeNote(TKey key, AVLTreeNote
leftNote, AVLTreeNote
rightNote) { Key = key; LeftNote = leftNote; RightNote = rightNote; } public TKey Key { get; set; } public int Height { get; set; } public AVLTreeNote
LeftNote { get; set; } public AVLTreeNote
RightNote { get; set; } } public class AVLTree
where TKey : IComparable { private AVLTreeNote
RootNote { get; set; } public AVLTree() { } private int GetHeight() { return 0; } private int GetHeight(AVLTreeNote
note) { return note == null ? 0 : note.Height; } private AVLTreeNote
LeftLeftRotation(AVLTreeNote
note) { AVLTreeNote
temp = note.LeftNote; note.LeftNote = temp.RightNote; temp.RightNote = note; note.Height = Math.Max(GetHeight(note.LeftNote), GetHeight(note.RightNote)) + 1; temp.Height = Math.Max(GetHeight(temp.LeftNote), GetHeight(temp.RightNote)) + 1; return temp; } private AVLTreeNote
RightRightRotation(AVLTreeNote
note) { AVLTreeNote
temp = note.RightNote; note.RightNote = temp.LeftNote; temp.LeftNote = note; note.Height = Math.Max(GetHeight(note.LeftNote), GetHeight(note.RightNote)) + 1; temp.Height = Math.Max(GetHeight(temp.LeftNote), GetHeight(temp.RightNote)) + 1; return temp; } private AVLTreeNote
LeftRightRotation(AVLTreeNote
note) { note.LeftNote = RightRightRotation(note.LeftNote); return LeftLeftRotation(note); } private AVLTreeNote
RightLeftRotation(AVLTreeNote
note) { note.RightNote = LeftLeftRotation(note.RightNote); return RightRightRotation(note); } public void Insert(TKey key) { RootNote = Insert(key, RootNote); } private AVLTreeNote
Insert(TKey key, AVLTreeNote
note) { if (note == null) { note = new AVLTreeNote
(key, null, null); } else { if (key.CompareTo(note.Key) < 0) { note.LeftNote = Insert(key, note.LeftNote); if (Math.Abs(GetHeight(note.LeftNote) - GetHeight(note.RightNote)) == 2) { if (key.CompareTo(note.LeftNote.Key) < 0)//其实这里判断就像知道新增加的子节点属于左节点还是右节点 画图的话 一目了然 { note = LeftLeftRotation(note); } else { note = LeftRightRotation(note); } } } if (key.CompareTo(note.Key) > 0) { note.RightNote = Insert(key, note.RightNote); if (Math.Abs(GetHeight(note.RightNote) - GetHeight(note.LeftNote)) == 2) { if (key.CompareTo(note.RightNote.Key) > 0)//其实这里判断就像知道新增加的子节点属于左节点还是右节点 画图的话 一目了然 { note = RightRightRotation(note); } else { note = RightLeftRotation(note); } } } } note.Height = Math.Max(GetHeight(note.LeftNote), GetHeight(note.RightNote)) + 1; return note; } public void Remove(TKey key) { Remove(key, RootNote); } private AVLTreeNote
Remove(TKey key, AVLTreeNote
note) { if (note == null) { return null; } if (key.CompareTo(note.Key) < 0) { note.LeftNote = Remove(key, note.LeftNote); if (Math.Abs(GetHeight(note.RightNote) - GetHeight(note.LeftNote)) == 2) { AVLTreeNote
rightNote = note.RightNote; if (GetHeight(rightNote.LeftNote) > GetHeight(rightNote.RightNote)) { note = RightLeftRotation(note); } else { note = RightRightRotation(note); } } } if (key.CompareTo(note.Key) > 0) { note.RightNote = Remove(key, note.RightNote); if (Math.Abs(GetHeight(note.RightNote) - GetHeight(note.LeftNote)) == 2) { AVLTreeNote
leftNote = note.LeftNote; if (GetHeight(leftNote.RightNote) > GetHeight(leftNote.LeftNote)) { note = LeftRightRotation(note); } else { note = LeftLeftRotation(note); } } } if (note.Key.CompareTo(key) == 0) { if (note.LeftNote != null && note.RightNote != null) { if (GetHeight(note.LeftNote) > GetHeight(note.RightNote)) { AVLTreeNote
max = FindMax(note.LeftNote); note.Key = max.Key; note.LeftNote = Remove(max.Key, note.LeftNote); } else { AVLTreeNote
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
note) { Queue
> queue = new Queue
>(); queue.Enqueue(note); while (queue.Count > 0) { 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
FindMin() { return FindMin(RootNote); } private AVLTreeNote
FindMin(AVLTreeNote
note) { if (note.LeftNote == null) { return note; } return FindMin(note.LeftNote); } public AVLTreeNote
FindMax() { return FindMax(RootNote); } private AVLTreeNote
FindMax(AVLTreeNote
note) { if (note.RightNote == null) { return note; } return FindMax(note.RightNote); } }}

 

转载于:https://www.cnblogs.com/bbvi/p/5066580.html

你可能感兴趣的文章
JavaWeb学习——JSP基础
查看>>
Eclipse tomcat server 无法添加项目
查看>>
黑寡妇黄飞鸿
查看>>
leetcode 217 Contains Duplicate 数组中是否有重复的数字
查看>>
The Ctrl & CapsLock `problem'
查看>>
MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
查看>>
linux故障判断
查看>>
Leetcode 23. Merge k Sorted Lists(python)
查看>>
Java进阶知识点6:并发容器背后的设计理念 - 锁分段、写时复制和弱一致性
查看>>
Makefile ===> Makefile 快速学习
查看>>
face detection[HR]
查看>>
java性能调优工具
查看>>
C# 其他的Url 文件的路径转化为二进制流
查看>>
cmake使用
查看>>
ios7上隐藏status bar
查看>>
构造方法和全局变量的关系
查看>>
python3基础05(有关日期的使用1)
查看>>
ArrayList的使用方法
查看>>
面向对象高级
查看>>
Bitwise And Queries
查看>>