예제 #1
0
        private static SortedTree <KeyType, ValueType> Siblings(SortedTree <KeyType, ValueType> thisNode)
        {
            SortedTree <KeyType, ValueType> currentNode = thisNode;

            if (!currentNode.IsRoot)
            {
                int keyIndex = currentNode.ParentNode.List.IndexOfKey(currentNode.ParentKey) + 1;

                foreach (KeyType key in currentNode.ParentNode.List.Keys.Skip(keyIndex))
                {
                    var newTempNode = currentNode.ParentNode.List[key];

                    if (newTempNode != null)
                    {
                        if (!newTempNode.IsSet)
                        {
                            newTempNode = Next(newTempNode);
                        }

                        if (newTempNode != null && newTempNode.IsSet)
                        {
                            return(newTempNode);
                        }
                    }
                }

                return(Siblings(currentNode.ParentNode));
            }

            return(null);
        }
예제 #2
0
        public KeyType[] GetPath()
        {
            SortedTree <KeyType, ValueType> currentNode = this;
            Stack <KeyType> keys = new Stack <KeyType>();

            while (!currentNode.IsRoot)
            {
                keys.Push(currentNode.ParentKey);
                currentNode = currentNode.ParentNode;
            }

            return(keys.ToArray());
        }
예제 #3
0
        private static SortedTree <KeyType, ValueType> Next(SortedTree <KeyType, ValueType> thisNode)
        {
            // Check for childeren first
            var nextNode = FirstChild(thisNode);

            if (nextNode != null && nextNode.IsSet)
            {
                return(nextNode);
            }

            // Looks for siblings instead
            nextNode = Siblings(thisNode);

            if (nextNode != null && nextNode.IsSet)
            {
                return(nextNode);
            }

            return(null);
        }
예제 #4
0
        public void MoveTo(SortedTree <KeyType, ValueType> node, bool replaceIfExists = false)
        {
            if (this.IsRoot)
            {
                throw new CircularStructureException("The root node can not be moved");
            }

            var parentNode = node;

            while (parentNode != null)
            {
                if (parentNode.ChildIsSet(this.ParentKey))
                {
                    var value = parentNode.List[this.ParentKey];
                }

                if (this == parentNode)
                {
                    throw new CircularStructureException("cannot be assigned as your own ancestor");
                }

                parentNode = parentNode.ParentNode;
            }


            if (node.List.ContainsKey(this.ParentKey))
            {
                if (replaceIfExists)
                {
                    node.List.Remove(this.ParentKey);
                }
                else
                {
                    throw new ArgumentException("Node alreay exists");
                }
            }

            node.List.Add(this.ParentKey, this);
            this.ParentNode.List.Remove(this.ParentKey);
            this.ParentNode = node;
        }
예제 #5
0
        private static SortedTree <KeyType, ValueType> FirstChild(SortedTree <KeyType, ValueType> thisNode)
        {
            // Check for childeren first
            foreach (KeyType key in thisNode.List.Keys)
            {
                var temp = thisNode.List[key];

                if (temp != null && temp.IsSet)
                {
                    return(temp);
                }

                temp = FirstChild(temp);

                if (temp != null && !temp.IsRoot && temp.IsSet)
                {
                    return(temp);
                }
            }

            return(null);
        }
예제 #6
0
        public SortedTree <KeyType, ValueType> Visit(params KeyType[] keys)
        {
            if (keys.Any())
            {
                KeyType key = keys[0];
                keys = keys.Skip(1).ToArray();

                if (List.ContainsKey(key))
                {
                    return(List[key].Visit(keys));
                }
                else
                {
                    var node = new SortedTree <KeyType, ValueType>(ref key, this);
                    List.Add(key, node);
                    return(node.Visit(keys));
                }
            }
            else
            {
                return(this);
            }
        }
예제 #7
0
 public SortedTree(ref KeyType parentKey, SortedTree <KeyType, ValueType> parentNode)
 {
     this.ParentKey  = parentKey;
     this.ParentNode = parentNode;
     this.IsRoot     = false;
 }