伸展二叉树树(C#)
参考过好几篇关于将伸展树的代码,发现看不懂。看图能看懂原理。就尝试自己实现了下。
自顶向上的算法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Collections;
using System.Threading.Tasks; namespace ConsoleApplication2
{
public class Program
{
public static void Main()
{
SplayTree<int> tree = new SplayTree<int>();
tree.Insert();
tree.Insert();
tree.Insert();
tree.Insert();
tree.Insert();
tree.Insert();
tree.Insert(); tree.Search(); Console.Read();
}
} public class SplayTreeNode<T> where T : IComparable
{
public T Key { get; set; }
public SplayTreeNode<T> LeftNode { get; set; }
public SplayTreeNode<T> RightNode { get; set; }
public SplayTreeNode(T _key, SplayTreeNode<T> _leftNode, SplayTreeNode<T> _rightNode)
{
this.Key = _key;
this.LeftNode = _leftNode;
this.RightNode = _rightNode;
} public SplayTreeNode()
{ } public override string ToString()
{
return Key + "";
}
} public class SplayTree<T> where T : IComparable
{
public SplayTreeNode<T> RootNode { get; set; } public void Splay(T key)
{
RootNode = Splay(RootNode, key);
} public SplayTreeNode<T> Splay(SplayTreeNode<T> node, T key)
{
if (node == null)
{
return node;
} return null;
} public void Insert(T key)
{
var node = RootNode;
if (node == null)
{
RootNode = new SplayTreeNode<T>(key, null, null);
return;
} while (true)
{
int result = node.Key.CompareTo(key);
if (result > )
{
if (node.LeftNode == null)
{
node.LeftNode = new SplayTreeNode<T>(key, null, null);
}
node = node.LeftNode;
}
else if (result < )
{
if (node.RightNode == null)
{
node.RightNode = new SplayTreeNode<T>(key, null, null);
}
node = node.RightNode;
}
else
{
break;
}
}
} public SplayTreeNode<T> Search(T key)
{
Search(RootNode, key);
return RootNode;
} private void Search(SplayTreeNode<T> node, T key)
{
int result = node.Key.CompareTo(key);
if (result < )
{
node= RightRotation(node);
if (node.RightNode != null)
{
RootNode = node;
}
else
{
Search(node, key);
} }
else if (result > )
{
node = LeftRotation(node);
if (node.LeftNode != null)
{
RootNode = node;
}else
{
Search(node, key);
}
}
else
{
RootNode = node;
}
} private SplayTreeNode<T> LeftRotation(SplayTreeNode<T> node)
{
SplayTreeNode<T> temp = node.LeftNode;
node.LeftNode = temp.RightNode;
temp.RightNode = node;
return temp;
} private SplayTreeNode<T> RightRotation(SplayTreeNode<T> node)
{
SplayTreeNode<T> temp = node.RightNode;
node.RightNode = temp.LeftNode;
temp.LeftNode = node;
return temp;
}
}
}
伸展二叉树树(C#)的更多相关文章
- 洛谷U4727小L的二叉树[树转序列 LIS]
题目背景 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣. 所以,小L当时卡在了二叉树. 题目描述 在计算机科学中,二叉树是每个结点最多有两个子结点的 ...
- NOIP2003加分二叉树[树 区间DP]
题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...
- 复习--二叉树&&树
树是一种很常用的数据结构,日后的学习中会经常碰到运用树的知识. //构造二叉树#include<cstdio> #include<iostream> #include<a ...
- BZOJ 1864 三色二叉树 - 树型dp
传送门 题目大意: 给一颗二叉树染色红绿蓝,父亲和儿子颜色必须不同,两个儿子颜色必须不同,问最多和最少能染多少个绿色的. 题目分析: 裸的树型dp:\(dp[u][col][type]\)表示u节点染 ...
- 二叉苹果树 - 二叉树树型DP
传送门 中文题面: 题目描述 有一棵苹果树,如果树枝有分叉,一定是分 2 叉(就是说没有只有 1 个儿子的结点,这棵树共有N 个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一 ...
- 6、二叉树树(java实现)
1.创建树的节点 public class Node { public Object data; //存储数据 public Node leftChild; //左子树指针 public Node r ...
- AVL排序二叉树树
AVL树第一部分,(插入) AVL树是一种自平衡二叉搜索树(BST),其中对于所有节点,左右子树的高度差不能超过1. 一个AVL树的示例 上面的树是AVL树,因为每个节点的左子树和右子树的高度之间的差 ...
- LintCode 推断一个二叉树树是否是还有一个二叉树的子书
有两个不同大小的二进制树: T1 有上百万的节点: T2 有好几百的节点. 请设计一种算法.判定 T2 是否为 T1的子树. /** * Definition of TreeNode: * class ...
- Tido 习题-二叉树-树状数组求逆序对
这里给大家提供一个全新的求逆序对的方法 是通过树状数组来实现的 题目描述 样例输入 Copy 5 2 3 1 5 4 样例输出 Copy 3 提示 #include<iostream ...
随机推荐
- Spring Security-用户密码自定义加密
public class SunPasswordEncoder implements PasswordEncoder{ //@实现加密的方法,既将明文转换为密文的方法 public String en ...
- win10快捷键大全
win10快捷键大全大家可以来了解一下,今天小编带来了win10常用快捷键,很多朋友喜欢使用快捷键来操作电脑,那么Windows10系统有哪些新的快捷键呢• 贴靠窗口:Win +左/右> Win ...
- icecap录屏gif
百度云下载地址:http://pan.baidu.com/s/1jIeyb4q 之前写博客,只有文字,有些效果看不出来.有了这个gif录屏工具方便很多哦,嘿嘿
- HUD 1541/BIT(数状数组)
题目链接 /* 按从左到右,从下到上的顺序给出星星的坐标,计算出level为[0,n)的星星的个数. 星星的level为该星星左下边(包括自己正下方的星星,但是不包括自己)星星的个数. BIT模板题. ...
- web配置文件的<load-on-startup>0</load-on-startup>
在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是:标记容器是否在启动的时候就加载这个servlet.当值为0或者大于 ...
- 设置SVN,Git忽略MAC的.DS_Store文件的方法
设置SVN,Git忽略MAC的.DS_Store文件的方法 I. 显示Mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bo ...
- ntp-keygen.c
这个程序产生加密数据文件使用的的密码,遵循Autokey security protocol和NTPv4.文件名被名字和创建时间组成的头部当做前缀,后面跟有一个类型定义的描述符标签和PEM加密的数据结 ...
- AI 人工智能 探索 (九)
链接:http://pan.baidu.com/s/1c0AM3g0 密码:uccw 今天补充 创建物体 移动物体 ,当点击创建后 ,会出来一个 上图的 ui,他跟随 物体,当你把物体拖动到 指定的地 ...
- 使用U盘在Mac机上装win8.1系统
1.首先要准备一个8G的U盘,用苹果机格式化为FAT格式.注意:U盘格式化之前要对U盘里的文件备份,U盘格式化后,里边的内容会清空. 2.下载原版win8.1系统,不要下载ghost版,http:// ...
- Nape 获取碰撞点加特效
package { import nape.phys.Body; import nape.shape.Shape; import nape.shape.Circle; import flash.dis ...