zoukankan      html  css  js  c++  java
  • 伸展二叉树树(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(4);
                tree.Insert(2);
                tree.Insert(6);
                tree.Insert(1);
                tree.Insert(3);
                tree.Insert(5);
                tree.Insert(7);
    
                tree.Search(1);
    
                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 > 0)
                    {
                        if (node.LeftNode == null)
                        {
                            node.LeftNode = new SplayTreeNode<T>(key, null, null);
                        }
                        node = node.LeftNode;
                    }
                    else if (result < 0)
                    {
                        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 < 0)
                {
                    node= RightRotation(node);
                    if (node.RightNode != null)
                    {
                        RootNode = node;
                    }
                    else
                    {
                        Search(node, key);
                    }
                    
                }
                else if (result > 0)
                {
                    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;
            }
        }
    }
  • 相关阅读:
    数据变换
    离群点的检验
    数据清洗
    数据采样
    FM与FFM
    EM算法与高斯混合模型
    最大熵模型
    PageRank
    软件技术基础
    原来炫酷的可视化地图,用Python就能搞定!
  • 原文地址:https://www.cnblogs.com/bbvi/p/5148564.html
Copyright © 2011-2022 走看看