Exemple #1
0
        /// <summary>
        /// 获取指定节点的子节点列表
        /// </summary>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public List <CTreeNode <T> > GetChilds(CTreeNode <T> parentNode = null)
        {
            var list = new List <CTreeNode <T> >();

            if (parentNode == null)
            {
                for (int i = 0; i < _treeSize; i++)
                {
                    if (_datas[i] != null)
                    {
                        list.Add(_datas[i]);
                    }
                }
            }
            else
            {
                var currentNode = parentNode.FirstChild;
                while (currentNode != null)
                {
                    list.Add(_datas[currentNode.ChildIndex]);
                    currentNode = currentNode.Next;
                }
            }

            return(list);
        }
Exemple #2
0
        public CTreeNode <T> AddNode(T data, CTreeNode <T> parentNode)
        {
            // 超出容量
            if (Count > _treeSize)
            {
                return(null);
            }

            var parentIndex = this.GetNodeIndex(parentNode);

            if (parentIndex == -1)
            {
                throw new Exception("无效的父节点");
            }

            CTreeNode <T> result = null;

            for (int i = 0; i < _treeSize; i++)
            {
                if (_datas[i] == null)
                {
                    result    = new CTreeNode <T>(data, parentIndex);
                    _datas[i] = result;
                    parentNode.AddChildNode(i);
                    Count++;
                    break;
                }
            }

            return(result);
        }
Exemple #3
0
 public CTree(T rootData, int treeSize)
 {
     _treeSize = treeSize;
     _datas    = new CTreeNode <T> [_treeSize];
     _datas[0] = new CTreeNode <T>(rootData, -1);
     Count++;
 }
Exemple #4
0
        /// <summary>
        /// 获取指定节点的父节点
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public CTreeNode <T> GetParent(CTreeNode <T> node)
        {
            if (node == null)
            {
                return(null);
            }

            return(_datas[node.ParentIndex]);
        }
Exemple #5
0
        /// <summary>
        /// 获取指定节点的Index
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public int GetNodeIndex(CTreeNode <T> node)
        {
            for (int i = 0; i < _treeSize; i++)
            {
                if (_datas[i].Equals(node))
                {
                    return(i);
                }
            }

            return(-1);
        }