伸展二叉树树(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 ...
随机推荐
- requirejs 一个拆分js项目的类库
http://www.requirejs.cn/ http://requirejs.org/docs/start.html
- EF中用Newtonsoft.Json引发的循环引用问题
描述: 1.双向关系表a->b b->aList 2.在查询a引用b以后 3.用Newtonsoft.Json 去tojsonstring 4.一个只有6条数据的json串 出现了一屏幕字 ...
- php 数组操作符
1.数组操作符 数组运算符 例子 名称 结果 $a + $b 联合 $a 和 $b 的联合. $a == $b 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE. $a === $b 全等 ...
- wpf 类似TextBlock外观的Button的样式
<Style x:Key="noborderbtnStyle" TargetType="{x:Type Button}"> <Setter P ...
- 准备着手学习python
1. python 的框架 Tornado 网址: http://www.tornadoweb.org/en/stable/ github: https://github.com/tornadoweb ...
- 【单源最短路】dijstra poj 1502
#include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...
- Cookie 的设置和获取
获取:var userName = getCookieValue("userName"); 设置:setCookie("userName",equpid,24, ...
- gvim work notes.. a few days' work on 64bit vim and plugin compilations
(a 600MB+ sized c/c++ compiler which is capable of hi-light and JB styled completion!! and of-course ...
- 内存管理 & 内存优化技巧 浅析
内存管理 浅析 下列行为都会增加一个app的内存占用: 1.创建一个OC对象: 2.定义一个变量: 3.调用一个函数或者方法. 如果app占用内存过大,系统可能会强制关闭app,造成闪退现象,影响用户 ...
- Form类的KeyPreview属性
首先需要知道一个知识点,Form控件,Panel控件和GroupBox控件等容器类控件默认是不接收焦点的,而是负责管理容器中控件的焦点.当容器控件被选中时,默认把焦点传送至容器内Tab顺序为0的控件. ...