Example #1
0
        public void removeNode(DSNode node)
        {
            List <DSConnection> connectionsToRemove = new List <DSConnection> ();

            if (node.isSelectionNode)
            {
                DSSelectionNode selectionNode = (DSSelectionNode)node;
                for (int i = 0; i < _connections.Count; i++)
                {
                    if (_connections [i].inPoint == selectionNode.inPoint || _connections [i].outPoint == selectionNode.trueOutPoint || _connections [i].outPoint == selectionNode.falseOutPoint)
                    {
                        connectionsToRemove.Add(_connections [i]);
                    }
                }
            }
            else
            {
                for (int i = 0; i < _connections.Count; i++)
                {
                    if (_connections [i].inPoint == node.inPoint || _connections [i].outPoint == node.outPoint)
                    {
                        connectionsToRemove.Add(_connections [i]);
                    }
                }
            }
            if (connectionsToRemove != null && connectionsToRemove.Count > 0)
            {
                for (int i = 0; i < connectionsToRemove.Count; i++)
                {
                    removeConnection(connectionsToRemove [i]);
                }
            }
            _nodes.Remove(node);
        }
        public DSNode <int> Execute(int val)
        {
            if (_head == null)
            {
                return(_head);
            }

            DSNode <int> current = _head;
            DSNode <int> runner  = _head.Next;

            while (runner != null)
            {
                if (runner.Data == val)
                {
                    runner       = runner.Next;
                    current.Next = runner;
                }
                else
                {
                    current = runner;
                    runner  = runner.Next;
                }
            }

            if (_head.Data == val)
            {
                return(_head.Next);
            }

            return(_head);
        }
        public int findCenterIndex()
        {
            if (_data == null)
            {
                return(-1);
            }

            DSNode <T> current = _data;

            DSNode <T> runner = _data;

            int index = 0;

            while (runner != null && runner.Next != null)
            {
                current = current.Next;
                runner  = runner.Next.Next;
                index++;
            }

            if (index % 2 == 0)
            {
                index = index - 1;
            }

            return(index);
        }
        public DSNode <int> Swap()
        {
            if (_head == null)
            {
                return(null);
            }

            if (_head.Next == null)
            {
                return(_head);
            }

            var          current = _head;
            DSNode <int> prev    = null;
            var          next    = _head.Next;
            DSNode <int> newHead = null;

            while (current != null && next != null)
            {
                DSNode <int> cached = this.SwapHelper(current, next, ref prev, ref newHead);

                current = cached;

                if (current != null)
                {
                    next = current.Next;
                }
            }

            return(newHead);
        }
Example #5
0
    DSNode BiderectionalSearch(DSNode start, DSNode end)
    {
        start = begining;
        end   = Finish;
        if (start && end && lookForDPS(end.value, start))
        {
            List <DSNode> pathToEnd = new List <DSNode>();           // Alfredo

            DSNode tmp;
            tmp = end;

            if (pathToEnd.Count <= 0)
            {
                while (tmp.value != start.value)
                {
                    pathToEnd.Add(tmp);
                    tmp = tmp.parent;
                }
            }
            if (lookForDPS(pathToEnd[pathToEnd.Count - 1].value, start))
            {
                tmp = pathToEnd[pathToEnd.Count - 1];

                return(tmp);
            }

            return(null);
        }
        return(null);
    }
Example #6
0
        public DSNode <int> ExecuteUnSorted()
        {
            if (_head == null)
            {
                return(null);
            }

            DSNode <int> current = _head;

            while (current != null)
            {
                DSNode <int> r1     = current;
                DSNode <int> runner = r1.Next;

                while (runner != null)
                {
                    if (current.Data == runner.Data)
                    {
                        r1.Next = runner.Next;
                    }

                    r1     = runner;
                    runner = runner.Next;
                }

                current = current.Next;
            }

            return(_head);
        }
Example #7
0
        public int Execute(int kthIndex)
        {
            if (_head == null)
            {
                return(-1);
            }

            int returnValue = -1;

            DSNode <int> runner  = _head;
            DSNode <int> current = _head;
            int          i       = 0;

            while (i < kthIndex && runner.Next != null)
            {
                runner = runner.Next;
                i++;
            }

            while (runner != null)
            {
                runner      = runner.Next;
                current     = current.Next;
                returnValue = current.Data;
            }

            return(returnValue);
        }
Example #8
0
    public DSNode lookForDPS(float data, DSNode node)
    {
        if (node.Equals(null))
        {
            return(null);
        }

        if (node.value.Equals(data))
        {
            last = node;
            return(node);
        }

        //Aqui se puede añadir a un stack

        for (int i = 0; i < node.vertex.Count; i++)
        {
            if (!node.vertex[i].Equals(null))
            {
                lookForDPS(data, node.vertex[i]);
            }
            if (last.value.Equals(data))
            {
                return(last);
            }
        }

        //Aqui se borraría del stack
        return(null);
    }
Example #9
0
    DSNode searchBFS(float data, DSNode start)
    {
        LinkedList <DSNode> visited = new LinkedList <DSNode>();
        Queue <DSNode>      queue   = new Queue <DSNode>();

        visited.AddLast(start);
        queue.Enqueue(start);

        while (queue.Count > 0)
        {
            start = queue.Peek();
            if (start.value.Equals(data))
            {
                return(start);
            }
            queue.Dequeue();

            for (int i = 0; i < start.vertex.Count; i++)
            {
                DSNode tmp = start.vertex[i];
                if (!visited.Contains(tmp))
                {
                    visited.AddLast(tmp);
                    queue.Enqueue(tmp);
                }
            }
        }

        return(null);
    }
        ///Need to fix the logic of finding the start point of loop in linkedList with out using extra space. using runner technique
        public void FindLoopStartPoint_Positive_Test()
        {
            DSLinkedList <int> lnkLst = new DSLinkedList <int>();

            lnkLst.Add(-1);
            lnkLst.Add(-7);
            lnkLst.Add(7);
            lnkLst.Add(-4);
            lnkLst.Add(19);
            lnkLst.Add(6);
            lnkLst.Add(-9);
            lnkLst.Add(-5);
            lnkLst.Add(2);
            lnkLst.Add(-5);

            lnkLst._current.Next = lnkLst._head.Next.Next.Next.Next.Next.Next;

            DSNode <int> expected = lnkLst._head.Next.Next.Next.Next.Next.Next;

            IntDSNodeEqualityComparer intDSNodeEqualityComparer = new IntDSNodeEqualityComparer();

            DSNode <int> actual = lnkLst.FindLoopStartPoint();

            Assert.IsNotNull(actual);

            Assert.AreEqual(expected, actual);
        }
Example #11
0
        public DSNode <int> ExecuteUnSortedUsingMap()
        {
            if (_head == null)
            {
                return(null);
            }

            Dictionary <int, bool> map = new Dictionary <int, bool>();

            DSNode <int> current = _head;
            DSNode <int> runner  = _head.Next;

            map.Add(current.Data, true);

            while (runner != null)
            {
                if (map.ContainsKey(runner.Data))
                {
                    runner       = runner.Next;
                    current.Next = runner;
                }
                else
                {
                    map.Add(runner.Data, true);
                    current = runner;
                    runner  = runner.Next;
                }
            }

            return(_head);
        }
Example #12
0
    void erase(float data)
    {
        DSNode to_erase = searchDFS(data);

        if (!to_erase.Equals(null))
        {
            //Aqui puede haver un problema
            to_erase = null;
        }
    }
Example #13
0
        public void Contruct_Test_Positive()
        {
            DSNode <int> list = new DSNode <int>()
            {
                Data = 1,
                Next = new DSNode <int>()
                {
                    Data = 2,
                    Next = new DSNode <int>()
                    {
                        Data = 3,
                        Next = new DSNode <int>()
                        {
                            Data = 4,
                            Next = new DSNode <int>()
                            {
                                Data = 5,
                                Next = new DSNode <int>()
                                {
                                    Data = 6,
                                    Next = new DSNode <int>()
                                    {
                                        Data = 7,
                                        Next = new DSNode <int>()
                                        {
                                            Data = 8,
                                            Next = new DSNode <int>()
                                            {
                                                Data = 9
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            };

            CompleteBTreeUsingLinkedList obj = new CompleteBTreeUsingLinkedList(list);

            DSTreeNode <int> head = obj.Construct();

            var traversal = obj.LevelOrderTraversal();

            int i = 1;

            foreach (var item in traversal)
            {
                Assert.AreEqual(item, i);
                i++;
            }
        }
Example #14
0
        public void DeleteMiddleElement_WithAccessToMiddleElement_Positive()
        {
            DSNode <int> expected = new DSNode <int>()
            {
                Data = 2,
                Next = new DSNode <int>()
                {
                    Data = 5,
                    Next = new DSNode <int>()
                    {
                        Data = 6,
                        Next = new DSNode <int>()
                        {
                            Data = 6
                        }
                    }
                }
            };

            DSNode <int> input = new DSNode <int>()
            {
                Data = 1,
                Next = new DSNode <int>()
                {
                    Data = 2,
                    Next = new DSNode <int>()
                    {
                        Data = 5,
                        Next = new DSNode <int>()
                        {
                            Data = 6,
                            Next = new DSNode <int>()
                            {
                                Data = 6
                            }
                        }
                    }
                }
            };

            DSNode <int> actual = _data.ExecuteWithAccessToMiddleNode(input);

            while (expected != null && actual != null)
            {
                Assert.AreEqual(expected.Data, actual.Data);

                expected = expected.Next;
                actual   = actual.Next;
            }

            Assert.IsNull(expected);
            Assert.IsNull(actual);
        }
Example #15
0
        public DSNode <int> ExecuteWithAccessToMiddleNode(DSNode <int> middle)
        {
            if (middle == null || middle.Next == null)
            {
                return(null);
            }

            middle.Data = middle.Next.Data;
            middle.Next = middle.Next.Next;

            return(middle);
        }
Example #16
0
        public void SwapTest()
        {
            DSNode <int> head = new DSNode <int>();

            head.Data = 1;
            head.Next = new DSNode <int>()
            {
                Data = 2,
                Next = new DSNode <int>()
                {
                    Data = 3,
                    Next = new DSNode <int>()
                    {
                        Data = 4
                    }
                }
            };

            DSNode <int> expected = new DSNode <int>();

            expected.Data = 2;
            expected.Next = new DSNode <int>()
            {
                Data = 1,
                Next = new DSNode <int>()
                {
                    Data = 4,
                    Next = new DSNode <int>()
                    {
                        Data = 3
                    }
                }
            };

            SwapPairs pairs = new SwapPairs(head);

            DSNode <int> actual = pairs.Swap();

            while (actual.Next != null && expected.Next != null)
            {
                Assert.AreEqual(actual.Data, expected.Data);
                actual   = actual.Next;
                expected = expected.Next;
            }
        }
        public bool IsPalindrome()
        {
            if (_data == null)
            {
                return(false);
            }

            FindMiddleOfList <char> findMiddle = new FindMiddleOfList <char>(_data);

            int middleIndex = findMiddle.findCenterIndex();

            DSStack <char> stack = new DSStack <char>();

            int index = 0;

            DSNode <char> current = _data;

            while (current != null)
            {
                if (index > middleIndex)
                {
                    var popped = stack.Pop();

                    if (popped != current.Data)
                    {
                        break;
                    }
                }
                else
                {
                    stack.Push(current.Data);
                    if (index == middleIndex && middleIndex % 2 != 0)
                    {
                        stack.Pop();
                    }
                }

                index++;

                current = current.Next;
            }

            return(stack.Count == 0);
        }
Example #18
0
    DSNode lookForDPSLimit(float data, DSNode node, int deep, int maxDeep)
    {
        node    = begining;
        data    = last.value;
        deep    = profuncidad;
        maxDeep = profundidadMaxima;

        int _deep = deep;

        if (_deep <= maxDeep)
        {
            _deep++;
            if (!node)
            {
                return(null);
            }

            if (node.value == data)
            {
                last = node;
                return(node);
            }

            //Aqui se puede añadir a un stack
            for (int i = 0; i < node.vertex.Count; i++)
            {
                if (node.vertex[i])
                {
                    lookForDPSLimit(data, node.vertex[i], _deep, maxDeep);
                }
                if (last.value == data)
                {
                    _deep--;
                    return(last);
                }
            }
            //Aqui se borraría del stack

            return(null);
        }

        return(null);
    }
        public void Remove_List_Element_Execute_Positive_Test()
        {
            int input = 6;

            DSNode <int> actual = _data.Execute(input);

            DSNode <int> expected = new DSNode <int>()
            {
                Data = 1,
                Next = new DSNode <int>()
                {
                    Data = 2,
                    Next = new DSNode <int>()
                    {
                        Data = 3,
                        Next = new DSNode <int>()
                        {
                            Data = 4,
                            Next = new DSNode <int>()
                            {
                                Data = 7,
                                Next = new DSNode <int>()
                                {
                                    Data = 5
                                }
                            }
                        }
                    }
                }
            };

            while (expected != null && actual != null)
            {
                Assert.AreEqual(expected.Data, actual.Data);

                expected = expected.Next;
                actual   = actual.Next;
            }

            Assert.IsNull(expected);
            Assert.IsNull(actual);
        }
Example #20
0
    DSNode lookforDPSIterative(float data, DSNode node)
    {
        if (node.Equals(null))
        {
            return(null);
        }

        //Aqui se puede añadir a un stack
        LinkedList <DSNode> visited = new LinkedList <DSNode>();
        Stack <DSNode>      stack   = new Stack <DSNode>();
        DSNode current = node;

        stack.Push(node);
        while (stack.Count > 0)
        {
            current = stack.Peek();
            stack.Pop();

            if (!visited.Contains(current))
            {
                //std::cout << current->value << " ";
                Debug.Log(current.value);
                visited.AddLast(current);
            }

            if (current.value.Equals(data))
            {
                return(current);
            }

            for (int i = current.vertex.Count - 1; i >= 0; i--)
            {
                if (!visited.Contains(current.vertex[i]))
                {
                    stack.Push(current.vertex[i]);
                }
            }
        }

        return(null);
    }
Example #21
0
    bool insert(float parent, float data)
    {
        DSNode tmp = lookForDPS(parent, root);

        if (tmp == true)
        {
            DSNode nodo = Instantiate(nodoDepthSearch).GetComponent <DSNode>();
            if (lookForDPS(data, root) != null)
            {
                return(false);
            }
            DSNode newNode = new DSNode(data);
            tmp.vertex.Add(nodo);
            newNode.parent = tmp;
            return(true);
        }
        else
        {
            return(false);
        }
    }
        private DSNode <int> SwapHelper(DSNode <int> current, DSNode <int> next, ref DSNode <int> prev, ref DSNode <int> newHead)
        {
            var cached = next.Next;

            next.Next    = current;
            current.Next = cached;

            if (prev != null)
            {
                prev.Next = next;
            }

            prev = current;

            if (newHead == null)
            {
                newHead = next;
            }

            return(cached);
        }
Example #23
0
        public void Remove_Duplicate_Execute_Positive_Test()
        {
            DSNode <int> expected = new DSNode <int>()
            {
                Data = 1,
                Next = new DSNode <int>()
                {
                    Data = 2,
                    Next = new DSNode <int>()
                    {
                        Data = 3,
                        Next = new DSNode <int>()
                        {
                            Data = 4,
                            Next = new DSNode <int>()
                            {
                                Data = 5,
                                Next = new DSNode <int>()
                                {
                                    Data = 6
                                }
                            }
                        }
                    }
                }
            };

            DSNode <int> actual = _data.Execute();

            while (expected != null && actual != null)
            {
                Assert.AreEqual(expected.Data, actual.Data);

                expected = expected.Next;
                actual   = actual.Next;
            }

            Assert.IsNull(expected);
            Assert.IsNull(actual);
        }
Example #24
0
        public void simulate()
        {
            DSNode currentNode = _nodes.Find(x => x.title.Equals("Start"));

            while (currentNode != null)
            {
                currentNode.execute();
                DSConnection next = null;
                if (currentNode.isSelectionNode)
                {
                    DSSelectionNode currentSelectionNode = (DSSelectionNode)currentNode;
                    if (currentSelectionNode.result && currentSelectionNode.trueOutPoint != null)
                    {
                        next = _connections.Find(x => x.outPoint == currentSelectionNode.trueOutPoint);
                    }
                    else if (!currentSelectionNode.result && currentSelectionNode.falseOutPoint != null)
                    {
                        next = _connections.Find(x => x.outPoint == currentSelectionNode.falseOutPoint);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    if (currentNode.outPoint == null)
                    {
                        break;
                    }
                    next = _connections.Find(x => x.outPoint == currentNode.outPoint);
                }
                if (next == null)
                {
                    break;
                }
                currentNode = _nodes.Find(x => x.id == next.inPoint.nodeID);
            }
        }
Example #25
0
        public DSNode <int> Execute()
        {
            if (_head == null)
            {
                return(null);
            }

            DSNode <int> current = _head;
            DSNode <int> runner  = _head.Next;

            while (runner != null && runner.Next != null)
            {
                runner = runner.Next.Next;
                if (runner != null && runner.Next != null)
                {
                    current = current.Next;
                }
            }

            current.Next = current.Next.Next;

            return(_head);
        }
        public void Init()
        {
            var head = new DSNode <int>();

            head.Data = 1;
            head.Next = new DSNode <int>()
            {
                Data = 2,
                Next = new DSNode <int>()
                {
                    Data = 3,
                    Next = new DSNode <int>()
                    {
                        Data = 4,
                        Next = new DSNode <int>()
                        {
                            Data = 6,
                            Next = new DSNode <int>()
                            {
                                Data = 7,
                                Next = new DSNode <int>()
                                {
                                    Data = 5,
                                    Next = new DSNode <int>()
                                    {
                                        Data = 6
                                    }
                                }
                            }
                        }
                    }
                }
            };

            _data = new RemoveListElement(head);
        }
Example #27
0
        public void Remove_Duplicate_Unsorted_Using_Map_Test()
        {
            DSNode <int> expected = new DSNode <int>()
            {
                Data = 3,
                Next = new DSNode <int>()
                {
                    Data = 2,
                    Next = new DSNode <int>()
                    {
                        Data = 1,
                        Next = new DSNode <int>()
                        {
                            Data = 5,
                            Next = new DSNode <int>()
                            {
                                Data = 6
                            }
                        }
                    }
                }
            };

            DSNode <int> actual = _unsortedData.ExecuteUnSortedUsingMap();

            while (expected != null && actual != null)
            {
                Assert.AreEqual(expected.Data, actual.Data);

                expected = expected.Next;
                actual   = actual.Next;
            }

            Assert.IsNull(expected);
            Assert.IsNull(actual);
        }
 public RemoveListElement(DSNode <int> head)
 {
     _head = head;
 }
 public SwapPairs(DSNode <int> head)
 {
     _head = head;
 }
Example #30
0
 public Tree(DSNode node)
 {
     root = node;
 }