二叉树的实现:

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;
        }
    }

 

posted on 2013-12-26 10:29  higirle  阅读(380)  评论(0编辑  收藏  举报