Ejemplo n.º 1
0
        private List <T> GetAllLeafData(RangeTreeNode2D <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 ConstructBranchNodes(RangeTreeNode2D <T>[] nodes, int length)
        {
            if (length == 1)
            {
                root          = nodes[0];
                root.leafData = null;
                return;
            }

            var newLength = length / 2;

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

            for (int i = 0; i < length / 2; i++)
            {
                var leafData = nodes[i * 2].leafData;
                leafData.AddRange(nodes[i * 2 + 1].leafData);
                nodes[i * 2].leafData     = null;
                nodes[i * 2 + 1].leafData = null;
                var node = new RangeTreeNode2D <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],
                    leafData   = leafData,
                    subTree    = new RangeTree <T>(leafData, subComparison)
                };
                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);
        }
Ejemplo n.º 3
0
        private void ConstructRangeTree2D(List <T> data)
        {
            if (data.Count == 0)
            {
                return;
            }

            //Sort by mainComparison
            data.Sort(mainComparison);
            var sortedData = data.ToArray();

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

            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++;
                    lastNode.leafData.Add(sortedData[i]);
                    continue;
                }

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

            //Construct subtree for each leaf node
            for (int i = 0; i < index; i++)
            {
                nodes[i].subTree = new RangeTree <T>(nodes[i].leafData, subComparison);
            }

            //Traverse level up to root
            ConstructBranchNodes(nodes, index);
        }