伸展二叉树树(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 ...
随机推荐
- reflow和repaint
Web页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验 简单地说,页面渲染就是浏览器将html代码根据CSS定义的规则显示在浏览器窗口中的这个过程.先来大致了解一下浏览器都 ...
- 在Linux服务器上增加硬盘没那么简单【转】
运维案例:HP服务器,LINUX系统在保障数据的前提下扩展/home分区 部门需求:研发部门提出需要在现有的服务器上扩容磁盘空间,以满足开发环境的磁盘需求.现有空间1.6T需要增加到2T. 需求调查分 ...
- json解析尖括号<>
如题 rs.getString("HEADLINE").replaceAll("<", " <").replaceAll(&qu ...
- go mode
https://github.com/dominikh/go-mode.el http://blog.altoros.com/golang-part-1-main-concepts-and-proje ...
- sql server获取当前年月日 时分秒
获取当前年月日(字符串): ),) 获取当前时间的时分秒(':'隔开): ),) 将年月日时分秒拼接成一条字符串: ),)),),':','')
- 判断是ios还是android
//判断是ios还是androidvar system;var ua = navigator.userAgent.toLowerCase(); if (/iphone|ipad|ipod/.test( ...
- 使用btoa和atob来进行Base64转码和解码
btoa: 将普通字符串转为Base64字符串 atob: 将Base64字符串转为普通字符串 说明:window.btoa不支持汉字: ===>使用window.encodeURI ...
- Guess the Array
Guess the Array time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- JS取非行间样式
<title>无标题文档</title> <style> #div1{width:200px;height:200px; background:red;} < ...
- Linux学习 -- 权限管理
1 ACL权限 1.1 简介与开启 1.1.1 ACL权限是什么 access control list 访问控制表 解决传统的(owner,group,others)身份不足的情况 可以设置 特定用 ...