当前位置:网站首页>C implementation of extended tree of binary tree
C implementation of extended tree of binary tree
2022-07-21 23:00:00 【A shuttle keyboard, Ren lifetime】
I'm Xiaobai Please correct me if there is any mistake
Code up
using System;
// data structure
namespace DataStructure
{
/// <summary>
/// Stretch tree node
/// </summary>
/// <typeparam name="T"></typeparam>
public class SplayNode<T> : TreeNode<T> where T : IComparable<T>
{
public SplayNode() : base()
{
RightChildNodes = null;
LeftChildNodes = null;
}
public SplayNode(T key, TreeNode<T> parentNode, TreeNode<T> leftNode, TreeNode<T> rightNode)
: base(key, parentNode, leftNode, rightNode)
{
MKey = key;
ParentNode = parentNode;
RightChildNodes = rightNode;
LeftChildNodes = leftNode;
}
}
/// <summary>
/// Stretch the tree ( Split the tree ) Can be in O(log n) Insert inside complete 、 lookup 、 Delete operation
/// </summary> characteristic : When a point is accessed, it passes through a series of rotations Make this point the root node The key is Rotation algorithm of extended tree
/// <typeparam name="T"></typeparam>
public class SplayTree<T> : BinaryTree<T> where T : IComparable<T>
{
/// <summary>
/// rotate key The corresponding node is the root node And return the root node
/// </summary>
/// <param name="rootNode"></param>
/// <param name="key"></param>
/// <returns> Return root node </returns>
/// 1. If there is key The corresponding node key The corresponding node is rotated to the root node
/// 2. If it doesn't exist key Corresponding node
/// (1). If key Less than tree.key 1. If there is key The precursor node of Then rotate the precursor node to the root node 2. If it doesn't exist key The precursor node of indicates that there is no ratio key Smaller nodes, then convert the smallest node to the root node
/// (2). If key Greater than tree.key 1. If there is key Successor node Then convert the successor node to the root node 2. If it doesn't exist key The rear drive node of indicates that there is no ratio key For larger nodes, convert the largest node to the root node
/// Precursor node : Less than the maximum node of this node
/// The subsequent nodes : The smallest node larger than this node
private TreeNode<T> SPlay(TreeNode<T> rootNode, T key)
{
if (rootNode == null)
return null;
TreeNode<T> N = new TreeNode<T>();
TreeNode<T> leftNode = N;
TreeNode<T> rightNode = N;
TreeNode<T> tempNode;
for (;;)
{
int cmp = key.CompareTo(rootNode.MKey);
if (cmp < 0)
{
if (rootNode.LeftChildNodes == null)
break;
if (key.CompareTo(rootNode.LeftChildNodes.MKey) < 0) // Judge Find node Is it better than The left node of the root node Small
{
tempNode = rootNode.LeftChildNodes; // Intermediate nodes = The left node of the root node
rootNode.LeftChildNodes = tempNode.RightChildNodes; // The left node of the root node = The right node of the middle node
rootNode.LeftChildNodes.ParentNode = rootNode;
tempNode.RightChildNodes = rootNode; // The right node of the middle node = The root node of the
rootNode.ParentNode = tempNode;
rootNode = tempNode;
if (rootNode.LeftChildNodes == null)
break;
/*
hypothesis key=1 After the rotation
rootNode(7) rootNode(tempNode 4)
/ \ / \
4 10 3 7
/ \ / \ / \
3 6 8 12 6 10
/ \
8 12
*/
}
leftNode.LeftChildNodes = rootNode;
//leftNode Left node assignment of leftNode The left child of (N The left child of ) Start and rootNode Point to the same block of memory (N The left child node of becomes rootNode)
leftNode = rootNode;
//leftNode and N The left child of Start and rootNode Point to the same block of memory N Still point to the original memory leftNode The left child of (N The left child node of the left child node ) Start and rootNode The left child node of points to the same block of memory
rootNode = rootNode.LeftChildNodes; //rootNode Assign and start with rootNode The left child node of points to the same block of memory And here leftNode Start pointing to a piece of memory alone
rootNode.ParentNode = null;
/*
hypothesis key=1 After the assignment N
rootNode(tempNode 4) leftNode(4) /
/ \ / \ leftNode(4)
3 7 (rootNode)3 7 / \
/ \ / \ (rootNode)3 7
6 10 6 10 / \
/ \ / \ 6 10
8 12 8 12 / \
8 12
*/
}
else if (cmp > 0)
{
if (rootNode.RightChildNodes == null)
break;
if (key.CompareTo(rootNode.RightChildNodes.MKey) > 0)
{
tempNode = rootNode.RightChildNodes;
rootNode.RightChildNodes = tempNode.LeftChildNodes;
tempNode.LeftChildNodes = rootNode;
rootNode = tempNode;
if (rootNode.RightChildNodes == null)
break;
}
rightNode.RightChildNodes = rootNode;
rightNode = rootNode;
rootNode = rootNode.RightChildNodes;
}
else
{
break;
}
}
rightNode.RightChildNodes = rootNode.RightChildNodes; //rightNode and N Share a fast memory therefore N The right side of is emptied
leftNode.LeftChildNodes = rootNode.LeftChildNodes; //leftNode Left child node and N The left child node of the left child node of points to the same block of memory N The left child node of the left child node of is cleared
// At this time rootNode For input key The node of rootNode The left node of N The left child node of the left node of
rootNode.LeftChildNodes = N.LeftChildNodes;
// The right node is cleared
rootNode.RightChildNodes = N.RightChildNodes;
if (rootNode.LeftChildNodes != null)
rootNode.LeftChildNodes.ParentNode = rootNode;
else if (rootNode.RightChildNodes != null)
rootNode.RightChildNodes.ParentNode = rootNode;
return rootNode;
}
private TreeNode<T> Remove(TreeNode<T> rootNode, T key)
{
TreeNode<T> tempNode;
if (rootNode == null)
return null;
if (FindNode(key, rootNode) == null)
return rootNode;
rootNode = SPlay(rootNode, key);
if (rootNode.LeftChildNodes != null)
{
tempNode = SPlay(rootNode.LeftChildNodes, key);
tempNode.RightChildNodes = rootNode.RightChildNodes;
tempNode.RightChildNodes.ParentNode = tempNode;
}
else
tempNode = rootNode;
rootNode = null;
return tempNode;
}
public void Remove(T key)
{
MRoot = Remove(MRoot, key);
}
public void SPlay(T key)
{
MRoot = SPlay(MRoot, key);
}
private void print(TreeNode<T> tree, T key, int direction)
{
if (tree != null)
{
if (direction == 0) // tree Root node
Console.Write("{0} is root\n", tree.MKey);
else // tree It's a branch node
Console.Write("{0} is {1}'s {2}s child Parent:{3}\n", tree.MKey, key,
direction == 1 ? "right" : "left",
tree.ParentNode.MKey);
print(tree.LeftChildNodes, tree.MKey, -1);
print(tree.RightChildNodes, tree.MKey, 1);
}
}
public void print()
{
if (MRoot != null)
print(MRoot, MRoot.MKey, 0);
}
}
}
边栏推荐
- OSPF irregular area, LSA and serial number
- Force deduction record: dynamic programming 5 subsequence problem (1) -- 300 longest ascending subsequence, 1143 longest common subsequence, 1035 disjoint lines, 674 longest continuous increasing sequ
- [oops framework] supporting game configuration data generation + data object code generator plug-in
- 一套十万级TPS的IM综合消息系统的架构实践与思考
- Alibaba IM technology sharing (VII): Optimization Practice of online and offline chat data synchronization mechanism of idle fish im
- 视频直播技术分享:一文读懂主流视频直播系统的推拉流架构、传输协议等
- CocosCreator 3.x 环境搭建
- 图集分离RGB和A通道之后的图片合并RGBA通道后导出原图
- Open source cocos creator 3 X game framework oops framework
- HCIP 第二天
猜你喜欢
Function and application of LAN telephone software system
微信团队分享:微信后台在海量并发请求下是如何做到不崩溃的
Alibaba IM technology sharing (VII): Optimization Practice of online and offline chat data synchronization mechanism of idle fish im
HCIP 第一天
VOIP软交换与选线路之间的对比
Im development technology sharing: talking about the best practice of offline messages and historical messages in IM system
Day 3 homework
Directx11-- window initialization (Win32)
Redis publish subscription
HCIP 第七天
随机推荐
Directx11-- window initialization (Win32)
FXS与FXO:有什么区别以及它是如何工作的
Force deduction record: dynamic programming 5 subsequence problem (1) -- 300 longest ascending subsequence, 1143 longest common subsequence, 1035 disjoint lines, 674 longest continuous increasing sequ
D3D function description
Teach you how to realize an efficient im long connection adaptive heartbeat keeping mechanism
Window common messages
CocosCreator3.x 接入腾讯游戏联机对战引擎笔记
VolP terminal and gateway
eyebeam电话呼叫软件使用及配置方法
unity 粒子特效从相机外进入相机 应该是激活状态结果没有播放
How to build the call center customer service system?
HCIP 第七天
[oops framework] resource management
Redis sentinel mechanism and configuration process
[oops framework] supporting hot update plug-ins
HCIP 第六天
Unity iTween的匀速运动
print的运用以及两数的交换
Dx11---纹理与光照(火焰动画,纹理的旋转,贴不同的纹理)
本地计算机上的MySQL57服务启动后停止