Exemplo n.º 1
0
        public void InsertInt(int number, IntNode currentNode = null)
        {
            if (currentNode == null)
            {
                currentNode = root;
            }

            if (currentNode.value > number)
            {
                if (currentNode.childLeft == null)
                {
                    currentNode.childLeft = new IntNode(number, currentNode);
                }
                else
                {
                    InsertInt(number, currentNode.childLeft);
                }
            }
            else if (currentNode.value != number) //(currentNode.value < nodeToAdd.value)
            {
                if (currentNode.childRight == null)
                {
                    currentNode.childRight = new IntNode(number, currentNode);
                }
                else
                {
                    InsertInt(number, currentNode.childRight);
                }
            }
            else
            {
                Console.WriteLine("NO DUPLICATES YOU KNOB");
            }
        }
Exemplo n.º 2
0
 public IntNode(int value, IntNode parent)
 {
     this.value  = value;
     this.parent = parent;
     childLeft   = null;
     childRight  = null;
 }
Exemplo n.º 3
0
        public void InsertRawNode(IntNode nodeToAdd, IntNode currentNode = null) //Deprecated, parent must be given but cannot properly be identified with tree assigning a parent
        {
            if (currentNode == null)
            {
                currentNode = root;
            }

            if (currentNode.value >= nodeToAdd.value)
            {
                if (currentNode.childLeft == null)
                {
                    currentNode.childLeft = nodeToAdd;
                }
                else
                {
                    InsertRawNode(nodeToAdd, currentNode.childLeft);
                }
            }
            else //(currentNode.value < nodeToAdd.value)
            {
                if (currentNode.childRight == null)
                {
                    currentNode.childRight = nodeToAdd;
                }
                else
                {
                    InsertRawNode(nodeToAdd, currentNode.childRight);
                }
            }
        }
Exemplo n.º 4
0
        public int getDepth(int number, IntNode currentNode = null, int depth = 0)
        {
            if (currentNode == null)
            {
                currentNode = root;
            }

            if (currentNode == null)
            {
                return(-1);
            }
            else if (currentNode.value > number)
            {
                return(getDepth(number, currentNode.childLeft, depth + 1));
            }
            else if (currentNode.value < number)
            {
                return(getDepth(number, currentNode.childRight, depth + 1));
            }
            else // (currentNode.value == number)
            {
                return(depth);
            }
        }
Exemplo n.º 5
0
 public Tree(int root)
 {
     this.root = new IntNode(root, null);
 }
Exemplo n.º 6
0
 public void setRightC(IntNode node)
 {
     childRight = node;
 }
Exemplo n.º 7
0
 public void setLeftC(IntNode node)
 {
     childLeft = node;
 }
Exemplo n.º 8
0
        public List <Tuple <int, string> > printFuckingEverything(List <Tuple <int, string> > result = null, IntNode currentNode = null)
        {
            if (currentNode == null)
            {
                currentNode = root;
            }
            if (result == null)
            {
                result = new List <Tuple <int, string> >();
            }
            string toCheck = getDepth(currentNode.value).ToString() + ".";
            //I KNOW THIS IS UGLY AND SLOW BUT IndexOf doesnt work and neither does Contains for some bloody reason
            string direction;

            if (currentNode != root)
            {
                if (currentNode.parent.childLeft == currentNode)
                {
                    direction = "L";
                }
                else
                {
                    direction = "R";
                }
                result.Add(new Tuple <int, string>(getDepth(currentNode.value), currentNode.value.ToString() + direction + currentNode.parent.value.ToString()));
            }
            else
            {
                result.Add(new Tuple <int, string>(0, currentNode.value.ToString()));
            }

            if (currentNode.childLeft != null)
            {
                printFuckingEverything(result, currentNode.childLeft);
            }
            if (currentNode.childRight != null)
            {
                printFuckingEverything(result, currentNode.childRight);
            }
            return(result);
        }