Ejemplo n.º 1
0
        private List<T> GetAllLeafData(RangeTreeNode<T> node)
        {
            var result = new List<T>();

            if (node.leftChild == null)
            {
                for (int i = 0; i < node.weight; i++)
                {
                    result.Add(node.data);
                }
            }

            result.AddRange(GetAllLeafData(node.leftChild));
            result.AddRange(GetAllLeafData(node.rightChild));

            return result;
        }
Ejemplo n.º 2
0
        private void ConstructRangeTree(T[] sortedData)
        {
            if (sortedData.Length == 0)
            {
                return;
            }

            //Construct leaf nodes
            var nodes = new RangeTreeNode<T>[sortedData.Length];
            var firstNode = new RangeTreeNode<T>()
            {
                data = sortedData[0],
                max = sortedData[0],
                leafNumber = 1
            };
            nodes[0] = firstNode;


            var lastData = sortedData[0];
            var lastNode = firstNode;
            var index = 1;

            for (int i = 1; i < sortedData.Length; i++)
            {
                if (CompareNode(lastData, sortedData[i]) == 0)
                {
                    lastNode.weight++;
                    lastNode.leafNumber++;
                    continue;
                }

                var node = new RangeTreeNode<T>()
                {
                    data = sortedData[i],
                    max = sortedData[i],
                    leafNumber = 1
                };
                nodes[index] = node;
                lastData = node.data;
                lastNode = node;
                index++;
            }

            //Traverse level up to root 
            ConstructBranchNodes(nodes, index);
        }
Ejemplo n.º 3
0
        private void ConstructBranchNodes(RangeTreeNode<T>[] nodes, int length)
        {
            if (length == 1)
            {
                root = nodes[0];
                return;
            }

            var newLength = length / 2;
            if (length % 2 == 1)
            {
                newLength++;
            }
            var newNodes = new RangeTreeNode<T>[newLength];

            for (int i = 0; i < length / 2; i++)
            {
                var node = new RangeTreeNode<T>()
                {
                    data = nodes[i * 2].max,
                    max = nodes[i * 2 + 1].max,
                    leafNumber = nodes[i * 2].leafNumber + nodes[i * 2 + 1].leafNumber,
                    leftChild = nodes[i * 2],
                    rightChild = nodes[i * 2 + 1]
                };
                nodes[i * 2].parent = node;
                nodes[i * 2 + 1].parent = node;

                newNodes[i] = node;
            }

            if (length % 2 == 1)
            {
                newNodes[newLength - 1] = nodes[length - 1];
            }

            ConstructBranchNodes(newNodes, newLength);
        }