zoukankan      html  css  js  c++  java
  • Binary sort tree的C#实现

    1. 定义:

    代码
        public class BinarySortTree<T> where T : IComparable
        {
            
    public T Data { setget; }
            
    public BinarySortTree<T> LeftChild { setget; }
            
    public BinarySortTree<T> RightChild { setget; }

            
    private BinarySortTree()
            {
            }
        }

    2. 创建:

    代码
            #region Create a new binary sort tree
            
    public static BinarySortTree<T> Create(T[] dataList)
            {
                
    if (dataList == null || dataList.Count() < 1)
                {
                    
    return null;
                }

                BinarySortTree
    <T> tree = new BinarySortTree<T>
                {
                    Data 
    = dataList[0]
                };
                
    for (int i = 1; i < dataList.Length; i++)
                {
                    InsertNode(tree, dataList[i]);
                }

                
    return tree;
            }

            
    private static void InsertNode(BinarySortTree<T> tree, T data)
            {
                
    if (tree == null)
                {
                    tree 
    = new BinarySortTree<T>
                    {
                        Data 
    = data
                    };
                    
    return;
                }

                BinarySortTree
    <T> node = tree;
                
    do
                {
                    
    if (data.CompareTo(node.Data) < 0)
                    {
                        
    if (node.LeftChild == null)
                        {
                            node.LeftChild 
    = new BinarySortTree<T>
                            {
                                Data 
    = data
                            };
                            
    return;
                        }
                        
    else
                        {
                            node 
    = node.LeftChild;
                        }
                    }
                    
    else
                    {
                        
    if (node.RightChild == null)
                        {
                            node.RightChild 
    = new BinarySortTree<T>
                            {
                                Data 
    = data
                            };
                            
    return;
                        }
                        
    else
                        {
                            node 
    = node.RightChild;
                        }
                    }
                } 
    while (node != null);
            }
            
    #endregion

    3. 插入新节点:

    代码
            /// <summary>
            
    /// insert a node
            
    /// </summary>
            
    /// <param name="data"></param>
            public void InsertNode(T data)
            {
                BinarySortTree
    <T> node = this;
                
    do
                {
                    
    if (data.CompareTo(node.Data) < 0)
                    {
                        
    if (node.LeftChild == null)
                        {
                            node.LeftChild 
    = new BinarySortTree<T>
                            {
                                Data 
    = data
                            };
                            
    return;
                        }
                        
    else
                        {
                            node 
    = node.LeftChild;
                        }
                    }
                    
    else
                    {
                        
    if (node.RightChild == null)
                        {
                            node.RightChild 
    = new BinarySortTree<T>
                            {
                                Data 
    = data
                            };
                            
    return;
                        }
                        
    else
                        {
                            node 
    = node.RightChild;
                        }
                    }
                } 
    while (node != null);
            }

    4. search:

    代码
            /// <summary>
            
    /// Search the node of whose Data equal to data
            
    /// </summary>
            
    /// <param name="data"></param>
            
    /// <returns></returns>
            public BinarySortTree<T> Search(T data)
            {
                BinarySortTree
    <T> node = this;
                
    do
                {
                    
    if (data.CompareTo(node.Data) == 0)
                    {
                        
    return node;
                    }
                    
    else
                    {
                        
    if (data.CompareTo(node.Data) < 0)
                        {
                            
    if (node.LeftChild == null)
                            {
                                
    break;
                            }
                            
    else
                            {
                                node 
    = node.LeftChild;
                            }
                        }
                        
    else
                        {
                            
    if (node.RightChild == null)
                            {
                                
    break;
                            }
                            
    else
                            {
                                node 
    = node.RightChild;
                            }
                        }
                    }
                } 
    while (node != null);

                
    return null;
            }

    5. 删除节点:

    代码
            /// <summary>
            
    /// Remove the node of whose data equal to data from binary sort tree
            
    /// </summary>
            
    /// <param name="data"></param>
            public static void Remove(BinarySortTree<T> tree, T data)
            {
                
    if (tree == null)
                    
    return;

                
    // if equal to root node
                BinarySortTree<T> node = tree;
                
    if (data.CompareTo(node.Data) == 0)
                {
                    
    if (node.LeftChild == null)
                    {
                        tree 
    = node.RightChild;
                    }
                    
    else
                    {
                        
    if (node.RightChild == null)
                        {
                            tree 
    = node.LeftChild;
                        }
                        
    else
                        {
                            BinarySortTree
    <T> minParentNode = node;
                            BinarySortTree
    <T> minNode = node.RightChild;
                            
    while (minNode.RightChild != null)
                            {
                                minParentNode 
    = minNode;
                                minNode 
    = minNode.RightChild;
                            }
                            minNode.LeftChild 
    = node.LeftChild;
                            minNode.RightChild 
    = minNode.Equals(node.RightChild) ? null : node.RightChild;
                            minParentNode.LeftChild 
    = null;

                            tree 
    = minNode;
                        }
                    }

                    
    return;
                }

                
    // don't equal to root node
                BinarySortTree<T> parentNode = tree;
                
    bool isLeft = true;
                
    do
                {
                    
    // equal to current node
                    if (data.CompareTo(node.Data) == 0)
                    {
                        
    if (node.LeftChild == null)
                        {
                            node 
    = node.RightChild;
                        }
                        
    else
                        {
                            
    if (node.RightChild == null)
                            {
                                node 
    = node.LeftChild;
                            }
                            
    else
                            {
                                BinarySortTree
    <T> minParentNode = node;
                                BinarySortTree
    <T> minNode = node.RightChild;
                                
    while (minNode.LeftChild != null)
                                {
                                    minParentNode 
    = minNode;
                                    minNode 
    = minNode.LeftChild;
                                }
                                minNode.LeftChild 
    = node.LeftChild;
                                minNode.RightChild 
    = node.RightChild;
                                minParentNode.LeftChild 
    = null;

                                node 
    = minNode;
                            }
                        }

                        
    if (isLeft)
                            parentNode.LeftChild 
    = node;
                        
    else
                            parentNode.RightChild 
    = node;

                        
    return;
                    }
                    
    else
                    {
                        
    if (data.CompareTo(node.Data) < 0)
                        {
                            
    if (node.LeftChild == null)
                                
    return;
                            
    else
                            {
                                parentNode 
    = node;
                                node 
    = node.LeftChild;
                                isLeft 
    = true;
                            }
                        }
                        
    else
                        {
                            
    if (node.RightChild == null)
                                
    return;
                            
    else
                            {
                                parentNode 
    = node;
                                node 
    = node.RightChild;
                                isLeft 
    = false;
                            }
                        }
                    }
                } 
    while (node != null);
            }

     6. 测试:

    代码
                BinarySortTree<int> tree = BinarySortTree<int>.Create(new int[]{5105201712192});
                tree.InsertNode(
    1);
                tree.InsertNode(
    0);
                BinarySortTree
    <int> node = tree.Search(1);
                TreeAction
    <int>.Remove(tree, 10);

    download

  • 相关阅读:
    Ubuntu 16.09下iptables通过raw表实现日志输出和调试
    CentOS 6.9永久设置静态路由表以及路由表常用设置
    Linux下添加静态路由表设置网关出现SIOCADDRT: Network is unreachable的问题分析
    Linux下使用ISC DHCP可以实现动态推送静态路由表
    Linux下使用ping出现destination is unreachable的问题可能性
    树莓派(Debian)系统开启iptables的raw表实现日志输出
    MySQL时间戳与日期互转
    树莓派(Debian)系统设置了静态IP之后还会获取动态IP的问题解决(scope global secondary eth0)
    Linux下同一网段内的IP中两台主机通信不经过路由器(ARP)(转)
    OpenWrt包管理软件opkg的使用(极路由)
  • 原文地址:https://www.cnblogs.com/Langzi127/p/1781345.html
Copyright © 2011-2022 走看看