数据结构-------二叉树

  class Node<T>
    {
        private T data;
        private Node<T> IChild;
        private Node<T> rChild;
        public T Data
        {
            get { return data; }
            set { data = value; }
        }
        public Node<T> Rchild
        {
            get { return rChild; }
            set { rChild = value; }
        }

        internal Node<T> LChild { get => IChild; set => IChild = value; }

        public Node (T val,Node <T>Ip,Node<T>rp )
        {
            data = val;
            IChild = Ip;
            rChild = rp;
        }
        public Node( Node<T> Ip, Node<T> rp)
        {
            data = default(T);
            IChild = Ip;
            rChild = rp;
        }
        public Node(T val)
        {
            data = val;
            IChild = null;
            rChild = null;
        }
        public Node()
        {
            data = default(T);
            IChild = null;
            rChild = null;
        }
    }

//链表—二叉树

    class BiTree<T>
    {
        private Node<T> head;
        public Node<T> Head
        {
            get { return head; }
            set { head = value; }
        }
        public BiTree()
        {
            head = null;
        }
        public BiTree(T val,Node<T>lp,Node<T>rp)
        {
            Node<T> p = new Node<T>(val,lp,rp );
            head = p;
        }
        public BiTree(T val)
        {
            Node<T> p = new Node<T>(val);
            head = p;
        }

        public bool IsEmpty()//是否为空树
        {
            return head == null ? true:false ;
        }
        public Node<T> Root()//获取根节点
        {
            return head;
        }
        public Node<T> GetLChild(Node<T>p)//获取左子孩子节点
        {
            return p.LChild;
        }
        public Node<T> GetRChild(Node<T> p)//获取右子孩子节点
        {
            return p.Rchild ;
        }
        //将结点p的左子树插入值为val的新结点,
            //原来的左子树成为新结点的左子树
        public void InsertL(T val,Node<T>p)
        {
            Node<T> temp = new Node<T>(val);
            temp.LChild = p.LChild;
            p.LChild = temp;
        }
        //将结点p的右子树插入值为val的新结点,
        //原来的右子树成为新结点的右子树
        public void InsertR(T val, Node<T> p)
        {
            Node<T> temp = new Node<T>(val);
            temp.Rchild  = p.Rchild ;
            p.Rchild  = temp;
        }
        //若p非空,删除p的左子树
        public Node<T >DeleteL(Node<T>p)
        {
            if (p==null ||p.LChild==null )
            {
                return null;
            }
            if (p.LChild.Rchild==null )
            {
                Node<T> temp = p.LChild;
                 p.LChild = p.LChild.LChild;
                //p.LChild = null;
                return temp;
            }
            else
            {
                Node<T> temp = p.LChild;
                Node<T> q = temp.Rchild;             
                while (q.LChild!=null )
                {
                    q = q.LChild;
                }
                p.LChild = q;
                q.Rchild = p.LChild.Rchild;
                q.LChild = p.LChild.LChild;
                return temp;
            }         
        }
        //判断是否是叶子结点
        public bool IsLeaf(Node<T> p)
        {
            if (p!=null&&p.LChild==null&&p.Rchild==null )
            {
              return   true;
            }
            else
            {
             return    false;
            }
        }
        public void InOeder(Node<T> root)
        {
            //节点为空
            if (root==null )
            {
                return;
            }
            Console.WriteLine("{0}", root.Data);
            //中序遍历左子树
            InOeder(root.LChild);
            //处理根节点
           
            //中序遍历右子树
            InOeder(root.Rchild);
           
        }


        public Node<T>DeleteR(Node <T> p)
        {
            if (p==null||p.Rchild==null )
            {
                return null ;
            }
            Node<T> temp = p.Rchild;
            p.Rchild = null;
            return temp;
        }

        public void Insert(T  i)
        {
            Node<T> newNode = new Node<T>();
            newNode.Data = i;
            if (head  == null)
                head  = newNode;
            else
            {
                Node<T> current = head ;
                Node<T> parent;
                while (true)
                {
                    parent = current;
                    if (Convert.ToInt32(i)< Convert.ToInt32(current.Data) )
                    {
                        current = current.LChild ;
                        if (current == null)
                        {
                            parent.LChild  = newNode;
                            break;
                        }
                    }
                    else
                    {
                        current = current.Rchild ;
                        if (current == null)
                        {
                            parent.Rchild  = newNode;
                            break;
                        }
                    }
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卿屿­­­­­­­-轻尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值