zoukankan      html  css  js  c++  java
  • 数据结构-二叉排序树

     结点类代码实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _013_二叉树
    {
        class BsNode
        {
            private BsNode LeftChild=null;
            private BsNode RightChild=null;
            private BsNode Parent=null;
            private int Data;
            internal BsNode rightChild { get => RightChild; set => RightChild = value; }
            internal BsNode leftChild { get => LeftChild; set => LeftChild = value; }
            internal BsNode parent { get => Parent; set => Parent = value; }
            internal int data { get => Data; set => Data = value; }
    
            public BsNode()
            {
    
            }
            public BsNode(int item)
            {
                this.data = item;
            }
    
       
        }
    }

    排序树代码实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace _013_二叉树
    {
        /// <summary>
        /// 二叉排序树
        /// </summary>
        class BsTree
        {
            private BsNode RootNode = null;
    
    
            /// <summary>
            /// 添加数据
            /// </summary>
            /// <param name="item"></param>
            public void Add(int item)
            {
                BsNode newNode = new BsNode(item);
                if (RootNode == null)//空树
                    RootNode = newNode;
                else
                {
                    BsNode temp = RootNode;
                    while (true)
                    {
                        if (item >= temp.data)//放在temp的右边
                        {
                            if (temp.rightChild == null) //如果右边为空  就直接添加
                            {
                                temp.rightChild =  newNode;
                                newNode.parent = temp;
                                break;
                            }
                            else //不为空 ,就赋值继续查找
                            {
                                temp = temp.rightChild;
                            }
                        }
                        else //放在temp的左边
                        {
                            if (temp.leftChild == null) //如果左边为空  就直接添加
                            {
                                temp.leftChild = newNode;
                                newNode.parent = temp;
                                break;
                            }
                            else//不为空 ,就赋值继续查找
                            {
                                temp = temp.leftChild;
                            }
                        }
                    }
                }
            }
            /// <summary>
            /// 查找
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            public bool Find(int item)
            {
                //return Find(item, RootNode);
    
                //方法二
                BsNode temp = RootNode;
                while (true)
                {
                    if (temp == null) return false;
                    if (temp.data == item) return true;
                    if (temp.data < item)
                    {
                        temp = temp.rightChild;
                    }
                    else
                    {
                        temp = temp.leftChild;
                    }
                }
            }
            /// <summary>
            /// 查找方法1
            /// </summary>
            /// <param name="item"></param>
            /// <param name="node"></param>
            /// <returns></returns>
            public bool Find(int item,BsNode node)
            {
                if (node == null) return false;
                if (node.data == item) return true;
                else
                {
                    if (item > node.data)
                    {
                       return Find(item, node.rightChild);
                    }
                    else
                    {
                        return  Find(item, node.leftChild);
                    }
                }
            }
            public void MiddleTraversal()
            {
                MiddleTraversal(RootNode);
            }
            /// <summary>
            /// 中序遍历
            /// </summary>
            /// <param name="node"></param>
            private void MiddleTraversal(BsNode node)
            {
                if (node == null) return;
    
                MiddleTraversal(node.leftChild);
                Console.Write(node.data+" ");
                MiddleTraversal(node.rightChild);
            }
    
            public bool Delete(int item)
            {
                BsNode temp = RootNode;
                while (true)
                {
                    if (temp == null) return false;
                    if (temp.data == item)
                    {
                        Delete(temp);
                        return true;
                    }
                    if (temp.data < item)
                    {
                        temp = temp.rightChild;
                    }
                    else
                    {
                        temp = temp.leftChild;
                    }
                }
            }
            private void Delete(BsNode node)
            {
                //第一种情况 删除叶子结点
                if(node.leftChild == null && node.rightChild==null)
                {
                    if (node.parent == null)
                    {
                        RootNode = null;
                    }
                    if (node.parent.leftChild == node)
                    {
                        node.parent.leftChild = null;
                    }
                    else if(node.parent.rightChild == node)
                    {
                        node.parent.rightChild = null;
                    }
                    return;
                }
    
                // 第二种情况 仅有左子树或者右子数的结点
                if (node.leftChild==null && node.rightChild != null)
                {
                    node.data = node.rightChild.data;
                    node.rightChild =null;
                    return;
                }
                if (node.leftChild != null && node.rightChild == null)
                {
                    node.data = node.leftChild.data;
                    node.leftChild = null;
                    return;
                }
    
                //第三种情况 左右子树都有节点
                BsNode temp = node.rightChild;
                while (temp.leftChild != null)
                {
                        temp = temp.leftChild;
                }
                node.data = temp.data;
                Delete(temp);
            }
        }
    }
  • 相关阅读:
    PCA算法的最小平方误差解释
    windows下面安装Python和pip终极教程
    理解C/C++的复杂声明
    C++的特殊预处理定义#、##和#@
    虚拟机中linux系统无法打开原保存的显示器配置解决方法
    Visual C++内存泄露检测—VLD工具使用说明
    C++重载、覆盖与隐藏——转载
    python爬虫实战(二)--------千图网高清图
    Linux常用命令12
    Linux常用命令11
  • 原文地址:https://www.cnblogs.com/rongweijun/p/8263185.html
Copyright © 2011-2022 走看看