예제 #1
0
        private void Traverse(SortedListNode <T> currNode)
        {
            if (currNode == null)
            {
                return;
            }

            lock (this.locker)
            {
                if (currNode.HasLeftNode)
                {
                    this.Traverse(currNode.LeftNode);
                }

                foreach (var data in currNode.Datas)
                {
                    this.nodes[this.index] = data;
                    this.index++;
                }

                if (currNode.HasRightNode)
                {
                    this.Traverse(currNode.RightNode);
                }
            }
        }
예제 #2
0
        public SortedListNode <T> FindNode(SortedListNode <T> head, T data, ref int level)
        {
            // todo: use itiration vs recursion

            if (head == null)
            {
                level++;
                return(new SortedListNode <T>(data));
            }

            var comparison = head.Data.CompareTo(data);

            if (comparison < 0)
            {
                // go right
                if (head.HasRightNode)
                {
                    level++;
                    return(FindNode(head.RightNode, data, ref level));
                }
            }
            else if (comparison > 0)
            {
                // go left
                if (head.HasLeftNode)
                {
                    level++;
                    return(FindNode(head.LeftNode, data, ref level));
                }
            }

            return(head);
        }
예제 #3
0
        public void AddNode(SortedListNode <T> parent, T data, ref int level)
        {
            // todo: rename local head var
            var head       = this.FindNode(parent, data, ref level);
            var comparison = head.Data.CompareTo(data);
            var newNode    = new SortedListNode <T>(data);

            if (comparison > 0)
            {
                head.LeftNode      = newNode;
                newNode.ParentNode = head;
            }
            else if (comparison < 0)
            {
                head.RightNode     = newNode;
                newNode.ParentNode = head;
            }
            else
            {
                head.Data = data;
            }
        }
예제 #4
0
        public void Add(T data)
        {
            lock (this.locker)
            {
                if (this.head == null)
                {
                    this.head = new SortedListNode <T>(data);
                    this.count++;
                }
                else
                {
                    this.InternalAdd(data);
                    this.count++;

                    if (this.count == 2)
                    {
                        var temp = this.head.RightNode;
                        temp.ParentNode      = null;
                        temp.LeftNode        = this.head;
                        this.head.ParentNode = temp;
                        this.head.RightNode  = null;
                        this.head            = temp;
                    }

                    if (this.count > 2)
                    {
                        var comparison = this.head.Data.CompareTo(data);
                        if (comparison > 0)
                        {
                            if (this.head.HasLeftNode)
                            {
                                var temp = this.head.LeftNode;
                                this.head.ParentNode = temp;
                                temp.ParentNode      = null;

                                var tempPointer = temp;
                                while (tempPointer.HasRightNode)
                                {
                                    tempPointer = tempPointer.RightNode;
                                }

                                this.head.LeftNode    = null;
                                tempPointer.RightNode = this.head;
                                this.head             = temp;
                            }
                        }
                        else if (comparison < 0)
                        {
                            if (this.head.HasRightNode)
                            {
                                var temp = this.head.RightNode;
                                this.head.ParentNode = temp;
                                temp.ParentNode      = null;

                                var tempPointer = temp;
                                while (tempPointer.HasLeftNode)
                                {
                                    tempPointer = tempPointer.LeftNode;
                                }

                                this.head.RightNode  = null;
                                tempPointer.LeftNode = this.head;
                                this.head            = temp;
                            }
                        }
                    }
                }
            }
        }
예제 #5
0
 public void AddNode(SortedListNode <T> parent, T data, ref int level)
 {
 }
예제 #6
0
 public SortedListNode <T> FindNode(SortedListNode <T> head, T data, ref int level)
 {
     throw new NotImplementedException();
 }
예제 #7
0
 public PreOrderTraversal(SortedListNode <T> head, int count)
 {
     this.head  = head;
     this.nodes = new T[count];
 }
예제 #8
0
 public SortedListNode(T data)
 {
     this.internalData.Add(data);
     this.leftNode  = null;
     this.rightNode = null;
 }