Esempio n. 1
0
        public Node addNode(int value, Node currentNode)
        {
            //do not allow nodes with values less than 1
             if(value < 1)
             {
            return null;
             }

             Node newNode = new Node(value);

             //check head
             if (_head == null)
             {
            _head = newNode;
            nodeCount++;
            return _head;
             }
             //if head not null but current node is, we won't be adding to tree
             else if(currentNode == null)
             {
            return null;
             }

             //find first empty child
             Node emptyChild = findEmptyChild(currentNode, newNode);
             nodeCount++;
             return emptyChild;
        }
Esempio n. 2
0
        public bool changeNodeValue(int newValue, Node currentNode)
        {
            bool success = false;
             if (newValue < 1)
             {
            return success;
             }

             currentNode.data = newValue;
             Node[] temp = currentNode.children;
             int numberChildren = countChildren(currentNode);

             //if new value >= populated children then just change children array size
             if (newValue >= numberChildren)
             {
            //resize child array
            currentNode.children = new Node[newValue];
            for (int i = 0; i < numberChildren; i++)
            {
               //copy old children over
               currentNode.children[i] = temp[i];
            }
            success = true;
            return success;
             }
             //number of children outnumber new value, resize array and move excess children down
             else
             {
            //resize child array
            currentNode.children = new Node[newValue];
            for (int i = 0; i < newValue; i++)
            {
               //copy old children over
               currentNode.children[i] = temp[i];
            }
            //for remaining old children, readd them starting from current nodes
            for (int j = newValue; j < numberChildren; j++)
            {
               findEmptyChild(currentNode, temp[j]);
            }
            success = true;
            return success;
             }
        }
Esempio n. 3
0
 public Problem3()
 {
     _head = null;
      nodeCount = 0;
 }
Esempio n. 4
0
 public Node findEmptyChild(Node currentNode, Node newNode)
 {
     //find first empty child of current node
      for (int i = 0; i < currentNode.children.Length; i++)
      {
     if (currentNode.children[i] == null)
     {
        currentNode.children[i] = newNode;
        return currentNode.children[i];
     }
      }
      //move to first child and check it's children for an empty
      //**this causes values to always accumulate on left side of the tree
      Node emptyChild = findEmptyChild(currentNode.children[0], newNode);
      return emptyChild;
 }
Esempio n. 5
0
        public int countChildren(Node currentNode)
        {
            int count = 0;

             foreach (Node child in currentNode.children)
             {
            if (child != null)
            {
               count++;
            }
             }

             return count;
        }
        public void findEmptyChildInCurrentNodeSecondChild()
        {
            Problem3 p3 = new Problem3();
             Node expected = new Node(3);
             p3.addNode(2, p3._head);
             p3.addNode(2, p3._head);
             p3.addNode(1, p3._head);
             p3.addNode(1, p3._head);

             Node actual = p3.findEmptyChild(p3._head, expected);
             Node expectedNode = p3._head.children[0].children[1];    //because we always go down left path

             Assert.AreEqual(expected, actual, "Empty child not found");
             Assert.AreEqual(expectedNode, actual, "Node not placed in empty child");
        }
        public void findEmptyChildInCurrentNodeChildren()
        {
            Problem3 p3 = new Problem3();
             Node expected = new Node(3);
             p3.addNode(2, p3._head);
             p3.addNode(1, p3._head);
             p3.addNode(1, p3._head);

             Node actual = p3.findEmptyChild(p3._head, expected);
             Node expectedNode = p3._head.children[0].children[0];

             Assert.AreEqual(expected, actual, "Empty child not found");
             Assert.AreEqual(expectedNode, actual, "Node not placed in empty child");
        }