算法導論13-有根樹[左孩子右兄弟表示法]_某年某月_新浪博客

算法導論13-有根樹[左孩子右兄弟表示法]

    #region 左孩子右兄弟表示法
    
    public class clsTree
    {
        public clsTree_dtl root { get; set; }
        public List ls = new List();
    }

    public class clsTree_dtl
    {
        public int key { get; set; }
        public clsTree_dtl p { get; set; }
        public clsTree_dtl left_child { get; set; }
        public clsTree_dtl right_sibling { get; set; }
        public int val { get; set; }
    }

    public static class toolTree
    {
        public static int xi = 0;
        public static object tmpobj = null;
        public static void tree_find(clsTree_dtl trp,int k)
        {
            if (xi>0)
            {
                return;
            }
            else
            {
                if (trp.key==k)
                {
                    xi++;
                    tmpobj = trp;
                    return;
                }
                else
                {
                    if (trp.left_child!=null)
                    {
                        tree_find(trp.left_child, k);
                    }
                    if (trp.right_sibling!=null)
                    {
                        tree_find(trp.right_sibling, k);
                    }
                }
            }
        }
        public static clsTree_dtl tree_find_sib(clsTree_dtl trp,clsTree_dtl dl=null)
        {
            if (trp.right_sibling == dl)
            {
                return trp;
            }
            else
            {
                return tree_find_sib(trp.right_sibling);
            }
            
        }
    }

    #endregion

        void re_tv(clsTree t,TreeNode nd=null)
        {
            tv1.Nodes.Clear();
            var _rt = t.root;
            TreeNode nd0 = new TreeNode();
            nd0.Text = _rt.key.ToString();
            nd0.Tag = _rt;
            tv1.Nodes.Add(nd0);
            re_tv_part(nd0, _rt);
            tv1.Tag = t;
            tv1.ExpandAll();
            try
            {
                if (nd!=null)
                {
                    tn_find = null;
                    chk_tree(nd0, nd.Text);
                    if (tn_find!=null)
                    {
                        tv1.SelectedNode = tn_find;
                    }
                }
                else
                {
                    tv1.SelectedNode = nd0;
                }
            }
            catch (Exception)
            {}
        }

        void re_tv_part(TreeNode nd,clsTree_dtl d)
        {
            if (d.left_child!=null)
            {
                TreeNode n1 = new TreeNode();
                n1.Text = d.left_child.key.ToString() ;
                n1.Tag = d.left_child;
                nd.Nodes.Add(n1);
                re_tv_part(n1, d.left_child);
            }
            if (d.right_sibling!=null)
            {
                TreeNode n1 = new TreeNode();
                n1.Text = d.right_sibling.key.ToString();
                n1.Tag = d.right_sibling;
                nd.Parent.Nodes.Add(n1);
                re_tv_part(n1, d.right_sibling);
            }
        }

        //遍歷樹節點
        void chk_tree(TreeNode pt,string key)
        {
            if (pt.Text==key)
            {
                tn_find = pt;
                return;
            }
            else
            {
                if (pt.FirstNode!=null)
                {
                    chk_tree(pt.FirstNode, key);
                }

                if (pt.NextNode!=null)
                {
                    chk_tree(pt.NextNode, key);
                }
            }
        }


        

        private void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                if (tv1.Tag==null)
                {
                    clsTree_dtl d = new clsTree_dtl();
                    d.key = (int)txtkey.Value;
                    clsTree t = new clsTree();
                    t.root = d;
                    t.ls.Add(d);
                    re_tv(t);
                }
                else
                {
                    var t = (clsTree)tv1.Tag;
                    var i = (int)txtkey.Value;
                    if (t.ls.FindAll(s=>s.key==i).Count>0)
                    {
                        MessageBox.Show(string.Format("KEY值 {0} 重複", i));
                        return;
                    }
                    var d = (clsTree_dtl)tv1.SelectedNode.Tag;
                    clsTree_dtl r = null;
                    clsTree_dtl x = new clsTree_dtl();
                    if (d.left_child==null)
                    {
                        x.p = d;
                        d.left_child = x;
                        x.key = (int)txtkey.Value;
                        t.ls.Add(x);
                        re_tv(t, tv1.SelectedNode);
                    }
                    else
                    {
                        r = toolTree.tree_find_sib(d.left_child);
                        if (r != null)
                        {

                            x.p = d;
                            r.right_sibling = x;
                            x.key = (int)txtkey.Value;
                            t.ls.Add(x);
                            re_tv(t, tv1.SelectedNode);
                        }
                    }
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            try
            {
                if (tv1.Tag==null || tv1.Nodes.Count==0)
                {
                    return;
                }
                else
                {
                    if (tv1.SelectedNode==tv1.TopNode)
                    {
                        tv1.Tag = null;
                        tv1.Nodes.Clear();
                    }
                    else
                    {
                        var t = (clsTree)tv1.Tag;
                        var d = (clsTree_dtl)tv1.SelectedNode.Tag;
                        if (tv1.SelectedNode.GetNodeCount(false) > 0)
                        {
                            MessageBox.Show(string.Format("節點[{0}],存在子節點", tv1.SelectedNode.Text));
                        }
                        else
                        {
                            //是否左兒子
                            if (d.p.left_child == d)
                            {
                                d.p.left_child = d.right_sibling;
                            }
                            else
                            {
                                var r = toolTree.tree_find_sib(d.p.left_child, d);
                                r.right_sibling = d.right_sibling;
                            }
                            t.ls.Remove(d);
                            re_tv(t);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值