zoukankan      html  css  js  c++  java
  • 二叉排序树

    1.

    public class Node
    {
        public Node(int key)
        {
            Value = key;
        }
    
        public int Value { get; set; }
    
        public Node Left { get; set; }
    
        public Node Right { get; set; }
    
        public void Display()
        {
            Node current = this;
            if (current != null)
            {
                Console.Write("parent: "+this.Value);
                if (current.Left != null)
                    Console.Write(" left: "+current.Left.Value);
                if (current.Right != null)
                    Console.Write(" right: " + current.Right.Value);
                Console.WriteLine();
                if (current.Left != null)
                    current.Left.Display();
                if (current.Right != null)
                    current.Right.Display();
            }
        }
    }
    
    public class BSTree
    {
    
    
        private Node _root;
    
        public void Display()
        {
            Node current = _root;
            if(current!=null)
            {
               _root.Display();
            }
        }
    
        public Node Find(int key)
        {
            Node current = _root;
            while (current != null && current.Value != key)
            {
                current = key < current.Value ? current.Left : current.Right;
            }
            return current;
        }
    
        public void Insert(int key)
        {
            var newNode = new Node(key);
            if (_root == null)
            {
                _root = newNode;
                return;
            }
            Node current = _root;
            Node parent;
            while (true)
            {
                parent = current;
                if (key < current.Value)
                {
                    current = current.Left;
                    if (current == null)
                    {
                        parent.Left = newNode;
                        return;
                    }
                }
                else
                {
                    current = current.Right;
                    if (current == null)
                    {
                        parent.Right = newNode;
                        return;
                    }
                }
            }
        }
    
        static void Main()
        {
    
            var tree = new BSTree();
            var numbers = new int[] { 10, 18, 3, 8, 12, 2, 7 };
            foreach (var number in numbers)
            {
                tree.Insert(number);
            }
            tree.Display();
            var node = tree.Find(8);
        }
    }

    2

  • 相关阅读:
    composer 的安装以及一些插件的下载等
    linux 服务器安装php5.6
    数据库异地备份---服务器配置流程
    expect安装
    linux 服务器安装mysql5.6
    使用navicat 使用IP、用户名、密码直接连接linux服务器里面的数据库
    函数指针 指针函数
    信号量
    消息队列-Message Queue
    生成库文件,会链接依赖的库文件吗?
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1941147.html
Copyright © 2011-2022 走看看