参考过好几篇关于将伸展树的代码,发现看不懂。看图能看懂原理。就尝试自己实现了下。

自顶向上的算法。

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#)的更多相关文章

  1. 洛谷U4727小L的二叉树[树转序列 LIS]

    题目背景 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣. 所以,小L当时卡在了二叉树. 题目描述 在计算机科学中,二叉树是每个结点最多有两个子结点的 ...

  2. NOIP2003加分二叉树[树 区间DP]

    题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...

  3. 复习--二叉树&&树

    树是一种很常用的数据结构,日后的学习中会经常碰到运用树的知识. //构造二叉树#include<cstdio> #include<iostream> #include<a ...

  4. BZOJ 1864 三色二叉树 - 树型dp

    传送门 题目大意: 给一颗二叉树染色红绿蓝,父亲和儿子颜色必须不同,两个儿子颜色必须不同,问最多和最少能染多少个绿色的. 题目分析: 裸的树型dp:\(dp[u][col][type]\)表示u节点染 ...

  5. 二叉苹果树 - 二叉树树型DP

    传送门 中文题面: 题目描述 有一棵苹果树,如果树枝有分叉,一定是分 2 叉(就是说没有只有 1 个儿子的结点,这棵树共有N 个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一 ...

  6. 6、二叉树树(java实现)

    1.创建树的节点 public class Node { public Object data; //存储数据 public Node leftChild; //左子树指针 public Node r ...

  7. AVL排序二叉树树

    AVL树第一部分,(插入) AVL树是一种自平衡二叉搜索树(BST),其中对于所有节点,左右子树的高度差不能超过1. 一个AVL树的示例 上面的树是AVL树,因为每个节点的左子树和右子树的高度之间的差 ...

  8. LintCode 推断一个二叉树树是否是还有一个二叉树的子书

    有两个不同大小的二进制树: T1 有上百万的节点: T2 有好几百的节点. 请设计一种算法.判定 T2 是否为 T1的子树. /** * Definition of TreeNode: * class ...

  9. Tido 习题-二叉树-树状数组求逆序对

    这里给大家提供一个全新的求逆序对的方法 是通过树状数组来实现的 题目描述   样例输入 Copy 5 2 3 1 5 4 样例输出 Copy 3 提示     #include<iostream ...

随机推荐

  1. ueditor浏览器 无法上传文件.问题

    dll也都引用了 路径绝对tmd没问题 最后 我一点一点的调试发现了问题 草tmd百度程序员 */UE.ajax = function() { //创建一个ajaxRequest对象 var fnSt ...

  2. AutoMapper 自动映射工具

    先引用对应的DLL. 11.转换匿名对象 结合LINQ映射新的实体类. using System;using System.Collections.Generic;using System.Linq; ...

  3. DataBinding

    <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http ...

  4. 以excel方式输出数据

    主类Test: public class D201 {//get set 方法略去 private String d201_01; private String d201_02; private St ...

  5. MyBatis-执行插入语句的时候返回主键ID到传入的参数对象中

    <!-- 保存项目信息 --> <insert id="saveItem" parameterType="pd" useGeneratedKe ...

  6. vim 设置默认显示行号

    vim 要默认打开的时候显示行号,这样就可以不用每次编辑的时候在手动在命令模式下:set number 来显示行号. 具体做法是: 创建一个 .vimrc的配置文件. 在启动vim时,当前用户根目录下 ...

  7. vi命令的常用操作

    G:移动到底部 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi +n filename :打开文件,并将光标置于第n行首 vi + filename :打开文件, ...

  8. Spring Boot 系列教程13-注解定时任务

    注解 @Scheduled(cron = "0/5 * * * * ?") 相当于原来的xml版本的如下配置 <task:scheduled ref="schedu ...

  9. HUST - 1599 Multiple

    input 长度不大于3*10e5的数字串 output 不含前导0的能整除64的字串的个数(0算一个,064不算) 一般数组中找能整除一个数的字串都是用取余来做的 用一个a[64]来存下从1-i位累 ...

  10. Webdriver+testNG+ReportNG+Maven+SVN+Jenkins自动化测试框架的pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...