• 数据结构之二叉树


    二叉树的实现:

    public class Node<T>
        {
            T date;
    
            public T Date
            {
                get { return date; }
                set { date = value; }
            }
            Node<T> lChild;
    
            public Node<T> LChild
            {
                get { return lChild; }
                set { lChild = value; }
            }
            Node<T> rChild;
    
            public Node<T> RChild
            {
                get { return rChild; }
                set { rChild = value; }
            }
    
            public Node(T value)
            {
                date = value;
                lChild = null;
                rChild = null;
            }
    
            public Node()
            {
                date = default(T);
                lChild = null;
                rChild = null;
            }
    
            public Node(Node<T> lc, Node<T> rc)
            {
                date = default(T);
                lChild = lc;
                rChild = rc;
            }
        }
    
        public class BinaryTree<T>
        {
            Node<T> head;
    
            public Node<T> Head
            {
                get { return head; }
                set { head = value; }
            }
    
            public BinaryTree()
            {
                head = null;
            }
    
            public bool IsEmpty()
            {
                if (head == null)
                {
                    return true;
                }
                else
                {
                  return  false;
                }
            }
    
            public Node<T> GetLeftChild(Node<T> p)
            {
                if (p != null)
                {
                    return p.LChild;
                }
                else
                {
                    return null;
                }
            }
    
            public Node<T> GetRightChild(Node<T> p)
            {
                if (p != null)
                {
                    return p.RChild;
                }
                else
                {
                    return null;
                }
            }
    
            public BinaryTree(T value)
            {
                Node<T> root = new Node<T>(value);
                head = root;
            }
    
            public void InsertL(T value, Node<T> p)
            {
                Node<T> newNode = new Node<T>(value);
                newNode.LChild = p.LChild;
                p.LChild = newNode;
            }
    
            public void InsertR(T value, Node<T> p)
            {
                Node<T> newNode = new Node<T>(value);
                newNode.RChild = p.RChild;
                p.RChild = newNode;
            }
    
            public Node<T> DeleteLeftChild(Node<T> p)
            {
                if (p == null)
                {
                    return null;
                }
    
                Node<T> temp = null;
                if (p != null && p.LChild != null) 
                {
                    temp = p.LChild;
                }
    
                p.LChild = null;
                return temp;
            }
    
            public Node<T> DeleteRightChild(Node<T> p)
            {
                if (p == null)
                {
                    return null;
                }
    
                Node<T> temp = null;
                if (p != null && p.RChild != null)
                {
                    temp = p.RChild;
                }
    
                p.RChild = null;
                return temp;
            }
    
            public bool IsLeftNode(Node<T> p)
            {
                if (p != null &&
                    p.LChild == null
                    && p.RChild == null)
                {
                    return true;
                }
    
                return false;
            }
        }
  • 相关阅读:
    读《SQL优化核心思想》:你不知道的优化技巧
    MySQL 索引建立原则及注意事项
    MySQL 捕获有问题的SQL-慢查日志 实例
    MySQL 分区间进行数据展示 实例
    MySQL 删除重复数据实例
    MySql 索引优化实例
    JDK1.6 Java.lang.Null.Pointer.Exception
    关于 MySQL LEFT JOIN 不可不知的事
    搞定queryString
    数据库不得不说的陷阱
  • 原文地址:https://www.cnblogs.com/Jessy/p/3491882.html
Copyright © 2020-2023  润新知