Esempio n. 1
0
        static public void printTree(BaseTreeNode node) //in order print
        {
            if (node == null)
            {
                return;
            }
            printTree(node.LeftChild);

            Console.WriteLine(node.Val + " ");

            printTree(node.RightChild);
        }
Esempio n. 2
0
        public void addElement(int value)
        {
            BaseTreeNode currNode = this;

            while (true)
            {
                if (currNode.Val == 0)
                {
                    currNode.Val = value;
                    return;
                }
                if (value < currNode.Val)
                {
                    if (currNode.LeftChild == null)
                    {
                        currNode.LeftChild = new BtreeNode {
                            Val = value, Isleaf = true
                        };
                        currNode.Isleaf = false;
                        return;
                        //height++; Nur ein mal hoch zählen, sonst doppelte height
                    }
                    else
                    {
                        currNode = currNode.LeftChild;
                        continue;
                    }
                }
                else if (value > currNode.Val)
                {
                    if (currNode.RightChild == null)
                    {
                        currNode.RightChild = new BtreeNode {
                            Val = value, Isleaf = true
                        };
                        currNode.Isleaf = false;
                        return;
                        //height++; Nur ein mal hoch zählen, sonst doppelte height
                    }
                    else
                    {
                        currNode = currNode.RightChild;
                        continue;
                    }
                }
                else
                {
                    System.Console.Write("Error: no duplicated Values allowed");
                    return;
                }
            }
        }