C# 一个自己写的树结构代码(1)

这段树结构代码已经写了三四年。写的过程中经过反复修改,目前在自己的程序中以及比较成熟,在.net4下运行,有需要的人可以拿去。
主要面对字符串保存处理的问题,节点中考虑到扩展性也加入了object对象属性。节点本身使用了大量的字符串。
这篇文章里是底层的树节点以及相关调用代码。这个类是两三年之前在网上找到扩展的,对原作者表示感谢,现在搜了下找不到源了。

树节点代码:


    public class MultTreeNode
    {
        #region members
        private int parentId;
        private int selfId;
        protected String nodeName;
        protected Object obj;
        protected MultTreeNode parentNode;
        protected List<MultTreeNode> childList;
        #endregion members

        #region contrustion
        public MultTreeNode()
        {
            init();
        }

        public MultTreeNode(MultTreeNode parentNode)
        {
            init(parentNode);
        }
        public void init(MultTreeNode pNode = null)
        {
            if (childList == null)
                childList = new List<MultTreeNode>();
            parentId = 0;
            selfId = 0;
            nodeName = "";
            obj = null;

            if (pNode != null)
            {
                nodeName = pNode.getNodeName();
                if (pNode.getObj() != null)
                    obj = new Hashtable((Hashtable)pNode.getObj());
                parentNode = pNode.getParentNode();
                if (pNode.getChildList() != null)
                    childList = new List<MultTreeNode>(pNode.getChildList());
            }

        }
        #endregion contrustion

        #region check node
        public bool isLeaf()
        {
            return childList == null || childList.Count == 0;
        }

        public bool isRoot()
        {
            return parentNode == null;
        }
        /// <summary>
        /// check exist of child node.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool ExistChildNode(string name)//,bool IgnoreCase = false
        {
            bool bRet = false;
            foreach (MultTreeNode node in childList)
            {
                if (node.nodeName == name)
                {
                    bRet = true;
                    break;
                }
            }
            return bRet;
        }

        #endregion check node

        #region insert
        /// <summary>
        /// insert child node to one node(this).
        /// </summary>
        /// <param name="name">new child node name.</param>
        /// <param name="path"></param>
        /// <param name="newnode">can be null. no use I think.</param>
        /// <returns&g
  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值