Beispiel #1
0
        public void AddElement(T valueToAdd)
        {
            //root is null the element becomes the root
            if (this.Root == null)
            {
                this.Root = new AVLTreeNode <T>(valueToAdd);
            }
            else
            {
                var currNode = this.Root;

                while (currNode != null)
                {
                    //currNode value is > valueToAdd - we go to left child
                    if (currNode.Value.CompareTo(valueToAdd) > 0)
                    {
                        if (currNode.LeftChild == null)
                        {
                            currNode.LeftChild = new AVLTreeNode <T>(valueToAdd);
                            //TODO: change balance
                            break;
                        }
                        else
                        {
                            currNode = currNode.LeftChild;
                        }
                    }
                    //currNode value is < valueToAdd - we go to right child
                    else if (currNode.Value.CompareTo(valueToAdd) < 0)
                    {
                        if (currNode.RightChild == null)
                        {
                            currNode.RightChild = new AVLTreeNode <T>(valueToAdd);
                            //TODO: change balance
                            break;
                        }
                        else
                        {
                            currNode = currNode.RightChild;
                        }
                    }
                    //value is the same as the node - we add it randomly to the left or right child(depending on valueItSelf)
                    else
                    {
                        //we add it to the right
                        if (valueToAdd.GetHashCode() % 2 == 0)
                        {
                            if (currNode.RightChild == null)
                            {
                                currNode.RightChild = new AVLTreeNode <T>(valueToAdd);
                                //TODO: change balance
                                break;
                            }
                            else
                            {
                                currNode = currNode.RightChild;
                            }
                        }
                        else
                        {
                            if (currNode.LeftChild == null)
                            {
                                currNode.LeftChild = new AVLTreeNode <T>(valueToAdd);
                                //TODO: change balance
                                break;
                            }
                            else
                            {
                                currNode = currNode.LeftChild;
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
 public AvlTree()
 {
     this.root = null;
 }