Пример #1
0
        // Method recursively calls itself down the tree until it find an open spot
        public void Insert(Student student)
        {
            // If students name is greater or equal to the parent then insert to right node
            if (string.Compare(student.Name, this.Student.Name) >= 0)
            { // if right child is null create new node
                if (RightChild == null)
                {
                    RightChild = new Node(student);
                }
                else
                { // if right child not null recursively call insert on right child
                    RightChild.Insert(student);
                }
            }
            else
            {     // if the students name is less than the parent then insert to left node
                if (LeftChild == null)
                { // if the left child is null create a new node
                    LeftChild = new Node(student);
                }

                else
                { // If the left child is not null then recursively call insert on left child
                    LeftChild.Insert(student);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Evaluate as boolean
        /// </summary>
        internal override bool BoolEvaluate(ConditionEvaluator.IConditionEvaluationState state)
        {
            ProjectErrorUtilities.VerifyThrowInvalidProject
                (LeftChild.CanBoolEvaluate(state),
                state.ElementLocation,
                "ExpressionDoesNotEvaluateToBoolean",
                LeftChild.GetUnexpandedValue(state),
                LeftChild.GetExpandedValue(state),
                state.Condition);

            if (LeftChild.BoolEvaluate(state))
            {
                // Short circuit
                return(true);
            }
            else
            {
                ProjectErrorUtilities.VerifyThrowInvalidProject
                    (RightChild.CanBoolEvaluate(state),
                    state.ElementLocation,
                    "ExpressionDoesNotEvaluateToBoolean",
                    RightChild.GetUnexpandedValue(state),
                    RightChild.GetExpandedValue(state),
                    state.Condition);

                return(RightChild.BoolEvaluate(state));
            }
        }
Пример #3
0
        internal override QueryResults <TOutput> Open(QuerySettings settings, bool preferStriping)
        {
            QueryResults <TLeftInput>  leftResults  = LeftChild.Open(settings, false);
            QueryResults <TRightInput> rightResults = RightChild.Open(settings, false);

            return(new BinaryQueryOperatorResults(leftResults, rightResults, this, settings, false));
        }
Пример #4
0
        public void Insert(T value)
        {
            if (value.CompareTo(Value) < 0)
            {
                if (LeftChild != null)
                {
                    LeftChild.Insert(value);
                }
                else
                {
                    LeftChild        = new BinarySearchTreeNode <T>(value);
                    LeftChild.Parent = this;
                }
            }

            if (value.CompareTo(Value) >= 0)
            {
                if (RightChild != null)
                {
                    RightChild.Insert(value);
                }
                else
                {
                    RightChild        = new BinarySearchTreeNode <T>(value);
                    RightChild.Parent = this;
                }
            }
        }
Пример #5
0
        public int GetHeight()
        {
            int leftHeight  = LeftChild == null ? 0 : LeftChild.GetHeight();
            int rightHeight = RightChild == null ? 0 : RightChild.GetHeight();

            return((leftHeight > rightHeight ? leftHeight : rightHeight) + 1);
        }
Пример #6
0
        /// <summary>
        /// Evaluate as boolean
        /// </summary>
        internal override bool BoolEvaluate(ConditionEvaluator.IConditionEvaluationState state)
        {
            if (!LeftChild.TryBoolEvaluate(state, out bool leftBool))
            {
                ProjectErrorUtilities.ThrowInvalidProject(
                    state.ElementLocation,
                    "ExpressionDoesNotEvaluateToBoolean",
                    LeftChild.GetUnexpandedValue(state),
                    LeftChild.GetExpandedValue(state),
                    state.Condition);
            }

            if (!leftBool)
            {
                // Short circuit
                return(false);
            }
            else
            {
                if (!RightChild.TryBoolEvaluate(state, out bool rightBool))
                {
                    ProjectErrorUtilities.ThrowInvalidProject(
                        state.ElementLocation,
                        "ExpressionDoesNotEvaluateToBoolean",
                        RightChild.GetUnexpandedValue(state),
                        RightChild.GetExpandedValue(state),
                        state.Condition);
                }

                return(rightBool);
            }
        }
Пример #7
0
        public Rectangle?GetRandomRoom(MT19337 rng)
        {
            if (WalkableSpace != null)
            {
                return(WalkableSpace);
            }

            var leftRoom  = LeftChild.GetRandomRoom(rng);
            var rightRoom = RightChild.GetRandomRoom(rng);

            if (leftRoom == null)
            {
                if (rightRoom == null)
                {
                    return(null);
                }
                return(rightRoom);
            }

            if (rightRoom == null)
            {
                return(leftRoom);
            }

            // got both
            return((rng.Between(0, 1) == 0) ? leftRoom : rightRoom);
        }
Пример #8
0
        public void CreateRooms()
        {
            // this function generates all the rooms and hallways for this cell and all of its children.
            if (LeftChild != null || RightChild != null)
            {
                // this cell has been split, so go into the children cells
                if (LeftChild != null)
                {
                    LeftChild.CreateRooms();
                }
                if (RightChild != null)
                {
                    RightChild.CreateRooms();
                }

                // Check if there's rooms on both sides, and connect those specific rooms
                var room1 = LeftChild.GetRoom();
                var room2 = RightChild.GetRoom();
                if (room1 != null && room2 != null && this.halls.Count == 0)
                {
                    this.ConnectRooms(room1.Value, room2.Value);
                }
            }
            else
            {
                // this cell needs a room
                Point roomSize;
                Point roomPos;
                // the room can be between 3 x 3 up to the size of the cell - 2.
                roomSize = new Point(rand.Next(3, Width - 2), rand.Next(3, Height - 2));

                roomPos = new Point(rand.Next(1, Width - roomSize.X - 1), rand.Next(1, Height - roomSize.Y - 1));
                Room    = new Rectangle(X + roomPos.X, Y + roomPos.Y, roomSize.X, roomSize.Y);
            }
        }
Пример #9
0
        /// <summary>
        /// Evaluate as boolean
        /// </summary>
        internal override bool BoolEvaluate(ConditionEvaluationState state)
        {
            ProjectErrorUtilities.VerifyThrowInvalidProject
                (LeftChild.CanBoolEvaluate(state),
                state.conditionAttribute,
                "ExpressionDoesNotEvaluateToBoolean",
                LeftChild.GetUnexpandedValue(state),
                LeftChild.GetExpandedValue(state),
                state.parsedCondition);

            if (!LeftChild.BoolEvaluate(state))
            {
                // Short circuit
                return(false);
            }
            else
            {
                ProjectErrorUtilities.VerifyThrowInvalidProject
                    (RightChild.CanBoolEvaluate(state),
                    state.conditionAttribute,
                    "ExpressionDoesNotEvaluateToBoolean",
                    RightChild.GetUnexpandedValue(state),
                    RightChild.GetExpandedValue(state),
                    state.parsedCondition);

                return(RightChild.BoolEvaluate(state));
            }
        }
Пример #10
0
        public TreeNode GetNodes()
        {
            var node = new TreeNode("Node value: " + Value +
                                    " Balnce factor: " + BalanceFactor);

            if (RightChild != null)
            {
                node.Nodes.Add(RightChild.GetNodes());
            }
            else
            {
                node.Nodes.Add("*empty*");
            }

            if (LeftChild != null)
            {
                node.Nodes.Add(LeftChild.GetNodes());
            }
            else
            {
                node.Nodes.Add("*empty*");
            }

            return(node);
        }
Пример #11
0
 /// <summary>
 /// Prints addition's expression
 /// </summary>
 public override void Print()
 {
     Console.Write("( + ");
     LeftChild.Print();
     RightChild.Print();
     Console.Write(") ");
 }
Пример #12
0
 public void AddNode(T nodeValue)
 {
     if (nodeValue.CompareTo(this._Value) > 0)
     {
         if (RightChild != null)
         {
             RightChild.AddNode(nodeValue);
         }
         else
         {
             RightChild = new BinaryTree <T>(nodeValue);
         }
     }
     else
     {
         if (LeftChild != null)
         {
             LeftChild.AddNode(nodeValue);
         }
         else
         {
             LeftChild = new BinaryTree <T>(nodeValue);
         }
     }
 }
Пример #13
0
        /// <summary>
        /// för in en ny nod i trädet
        /// </summary>
        /// <param name="value">trädets potentiella nya värde</param>
        public void Insert(T value)
        {
            if (value.CompareTo(this.Value) > 0)                             //jämför infört värde med värdet i aktuella noden som genomsöks
            {
                if (RightChild == null)                                      //om nodens högra barn är tomt
                {
                    RightChild        = new BinarySearchTreeNode <T>(value); //skapar ny instans av typen BinarySearchTreeNode.
                    RightChild.Parent = this;                                //gör aktuell nod till förälder till den nyskapade noden
                }

                else
                {
                    RightChild.Insert(value); //om != null, anropa funktionen rekursivt
                }
            }
            else if (value.CompareTo(this.Value) < 0)                       //jämför infört värde med värdet i aktuella noden som genomsöks
            {
                if (LeftChild == null)                                      //om nodens vänstra barn är tomt
                {
                    LeftChild        = new BinarySearchTreeNode <T>(value); //skapar ny instans av typen BinarySearchTreeNode.
                    LeftChild.Parent = this;                                //gör aktuell nod till förälder till den nyskapade noden
                }
                else
                {
                    LeftChild.Insert(value); //om != null, anropa funktionen rekursivt
                }
            }
        }
Пример #14
0
        private void UpdateConditionedProperties(ConditionEvaluator.IConditionEvaluationState state)
        {
            if (!_conditionedPropertiesUpdated && state.ConditionedPropertiesInProject != null)
            {
                string leftUnexpandedValue  = LeftChild.GetUnexpandedValue(state);
                string rightUnexpandedValue = RightChild.GetUnexpandedValue(state);

                if (leftUnexpandedValue != null)
                {
                    ConditionEvaluator.UpdateConditionedPropertiesTable
                        (state.ConditionedPropertiesInProject,
                        leftUnexpandedValue,
                        RightChild.GetExpandedValue(state));
                }

                if (rightUnexpandedValue != null)
                {
                    ConditionEvaluator.UpdateConditionedPropertiesTable
                        (state.ConditionedPropertiesInProject,
                        rightUnexpandedValue,
                        LeftChild.GetExpandedValue(state));
                }

                _conditionedPropertiesUpdated = true;
            }
        }
Пример #15
0
            private BiTree <T> FindFirstUnbalancedNode()
            {
                if (!isBalancedNode)
                {
                    return(this);
                }
                else
                {
                    BiTree <T> toReturn = null;

                    if (LeftChild != null)
                    {
                        toReturn = LeftChild.FindFirstUnbalancedNode();
                    }

                    if (toReturn != null)
                    {
                        return(toReturn);
                    }

                    if (RightChild != null)
                    {
                        toReturn = RightChild.FindFirstUnbalancedNode();
                    }

                    if (toReturn != null)
                    {
                        return(toReturn);
                    }

                    return(null);
                }
            }
Пример #16
0
        public ShapeNode RBuildTree(Diagram parent)
        {
            ShapeNode node = new ShapeNode(parent)
            {
                Bounds = new Rect(new Point(), MainWindow.NodeSize),
                Text   = Key.ToString(),
                Tag    = this
            };

            parent.Nodes.Add(node);

            if (LeftChild != null)
            {
                var childNode = LeftChild.RBuildTree(parent);
                childNode.AttachTo(node, AttachToNode.TopLeft);


                var link = new DiagramLink(parent, node, childNode);
                parent.Links.Add(link);
            }

            if (RightChild != null)
            {
                var childNode = RightChild.RBuildTree(parent);
                childNode.AttachTo(node, AttachToNode.TopRight);

                var link = new DiagramLink(parent, node, childNode);
                parent.Links.Add(link);
            }

            return(node);
        }
        // Add a node to this node's subtree.
        public void AddNode(TreeNode node, ref TreeNode linkFromParent)
        {
            // See which subtree should hold it.
            if (node.Value < this.Value)
            {
                if (LeftChild == null)
                {
                    LeftChild   = node;
                    node.Height = 1;
                }
                else
                {
                    LeftChild.AddNode(node, ref LeftChild);
                }
            }
            else
            {
                if (RightChild == null)
                {
                    RightChild  = node;
                    node.Height = 1;
                }
                else
                {
                    RightChild.AddNode(node, ref RightChild);
                }
            }

            // Update the node's height (in case subtree heights have changed).
            UpdateHeight();

            // When we return to this point, we may need to rebalance our subtree.
            RebalanceSubtree(this, ref linkFromParent);
        }
Пример #18
0
        public bool Find(T value)
        {
            //Om rotnoden är det värde som söks
            if (Equals(this.Value, value))
            {
                return(true);
            }
            if (value.CompareTo(Value) < 0)
            {
                //Om värdet som söks är lägre än nuvarande nod och dess vänsterbarn ej finns så finns värdet som söks inte
                if (Equals(LeftChild, null))
                {
                    return(false);
                }

                //Om noden har ett vänsterbarn så fortsätter rekursionen
                return(LeftChild.Find(value));
            }
            else
            {
                //Om noden har 2 barn. Då ersätts den borttagna noden med det minsta värdet i höger subträd
                if (Equals(RightChild, null))
                {
                    return(false);
                }

                //Om noden har ett vänsterbarn så fortsätter rekursionen
                return(RightChild.Find(value));
            }
        }
Пример #19
0
        public void GenerateRooms(MT19337 rng)
        {
            // this function generates all the rooms and hallways for this BSPMapNode and all of its children.
            if (LeftChild == null && RightChild == null)
            {
                if (WalkableSpace != null)
                {
                    return;
                }

                // the room can be between 3 x 3 tiles to the size of the leaf - 2.
                var roomWidth  = rng.Between(BSPTreeEngine.MIN_ROOM_WIDTH, Width - 2);
                var roomHeight = rng.Between(BSPTreeEngine.MIN_ROOM_HEIGHT, Height - 2);

                // place the room within the BSPMapNode, but don't put it right
                // against the side of the BSPMapNode (that would merge rooms together)
                var roomStartX = rng.Between(1, Width - roomWidth - 1);
                var roomStartY = rng.Between(1, Height - roomHeight - 1);
                WalkableSpace = new Rectangle(X + roomStartX, Y + roomStartY, roomWidth, roomHeight);
                return;
            }

            // subleafs:
            LeftChild?.GenerateRooms(rng);
            RightChild?.GenerateRooms(rng);

            // both leafs exist. we know their GetRandomRoom shouldn't return null, because we just called GenerateRooms.
            if (LeftChild != null && RightChild != null)
            {
                MakeHallway(rng, (Rectangle)LeftChild.GetRandomRoom(rng), (Rectangle)RightChild.GetRandomRoom(rng));
            }
        }
Пример #20
0
        public void Insert(T value)
        {
            if (Find(value))
            {
                //Om det angivna värdet redan finns i trädet visas ett felmeddelande istället
                throw new NotImplementedException("Det här värdet finns redan i trädet!");
            }
            //Undersöker om värdet är högre eller lägre än rotnoden
            if (value.CompareTo(Value) > 0)
            {
                if (this.RightChild == null)
                {
                    this.RightChild        = new BinarySearchTreeNode <T>(value);
                    this.RightChild.Parent = this;
                }
                else
                {
                    RightChild.Insert(value);
                }
            }

            else
            {
                if (this.LeftChild == null)
                {
                    this.LeftChild        = new BinarySearchTreeNode <T>(value);
                    this.LeftChild.Parent = this;
                }
                else
                {
                    LeftChild.Insert(value);
                }
            }
        }
Пример #21
0
        public bool Find(T value)
        {
            if (value.Equals(Value))
            {
                return(true);
            }

            else
            {
                if (value.CompareTo(Value) < 0)
                {
                    if (LeftChild != null)
                    {
                        return(LeftChild.Find(value));
                    }
                }
                if (value.CompareTo(Value) >= 0)
                {
                    if (RightChild != null)
                    {
                        return(RightChild.Find(value));
                    }
                }
            }
            return(false);
        }
Пример #22
0
            internal void BubbleDownData(Func <T, T, bool> compare)
            {
                if (LeftChild == null && RightChild == null)
                {
                    return;
                }

                if (LeftChild != null && !compare(Data, LeftChild.Data))
                {
                    // Swap
                    // TODO into a method
                    T temp = LeftChild.Data;
                    LeftChild.Data = Data;
                    Data           = temp;
                    LeftChild.BubbleDownData(compare);
                }
                else if (RightChild != null && !compare(Data, RightChild.Data))
                {
                    // Swap
                    T temp = RightChild.Data;
                    RightChild.Data = Data;
                    Data            = temp;
                    RightChild.BubbleDownData(compare);
                }
            }
Пример #23
0
 public virtual TreeNode InsertTreeNode(TreeNode newNode)
 {
     if (newNode.Value >= Value)
     {
         if (RightChild == null)
         {
             RightChild     = newNode;
             newNode.Parent = this;
         }
         else
         {
             RightChild.InsertTreeNode(newNode);
         }
     }
     else
     {
         if (LeftChild == null)
         {
             LeftChild      = newNode;
             newNode.Parent = this;
         }
         else
         {
             LeftChild.InsertTreeNode(newNode);
         }
     }
     return(newNode);
 }
Пример #24
0
    public void ReplaceWithSingleNode(BeachLineElement newNode)
    {
        if (Parent != null)
        {
            newNode.SetParent(Parent);
            if (Parent.LeftChild == this)
            {
                Parent.SetLeftChild(newNode);
            }
            else
            {
                Parent.SetRightChild(newNode);
            }
        }
        else
        {
            // This is the root node
            mBeachLine.SetRoot(newNode);
        }

        if (LeftChild != null)
        {
            newNode.SetLeftChild(LeftChild);
            LeftChild.SetParent(newNode);
        }
        if (RightChild != null)
        {
            newNode.SetRightChild(RightChild);
            RightChild.SetParent(newNode);
        }
        mParent     = null;
        mLeftChild  = null;
        mRightChild = null;
    }
Пример #25
0
 private void PrintRightChild(TravelType travelOrder)
 {
     if (RightChild != null)
     {
         RightChild.PrintTreeNode(travelOrder);
     }
 }
Пример #26
0
    public void DrawTree(Vector3 offset, int iteration)
    {
        Color color = this is BeachLineArc ? Color.green : Color.cyan;
        // Draw square
        float square_size = 0.5f;

        Debug.DrawRay(new Vector3(-square_size, -square_size, 0) + offset, 2 * Vector3.right * square_size, color);
        Debug.DrawRay(new Vector3(-square_size, square_size, 0) + offset, 2 * Vector3.right * square_size, color);
        Debug.DrawRay(new Vector3(-square_size, square_size, 0) + offset, 2 * Vector3.down * square_size, color);
        Debug.DrawRay(new Vector3(square_size, square_size, 0) + offset, 2 * Vector3.down * square_size, color);

        float   child_x_offset     = 4f / Mathf.Pow(2, iteration);
        Vector3 left_child_offset  = new Vector3(offset.x + child_x_offset, offset.y - 1.5f, 0);
        Vector3 right_child_offset = new Vector3(offset.x - child_x_offset, offset.y - 1.5f, 0);

        if (LeftChild != null)
        {
            Debug.DrawLine(
                offset + (new Vector3(0, -square_size, 0)),
                left_child_offset + (new Vector3(0, square_size, 0)),
                Color.white);
            LeftChild.DrawTree(left_child_offset, iteration + 1);
        }
        if (RightChild != null)
        {
            Debug.DrawLine(
                offset + (new Vector3(0, -square_size, 0)),
                right_child_offset + (new Vector3(0, square_size, 0)),
                Color.white);
            RightChild.DrawTree(right_child_offset, iteration + 1);
        }
    }
Пример #27
0
 public override BinaryTree <T> Add(T value)
 {
     if (value.CompareTo(Value) == -1)
     {
         if (LeftChild.GetType() == typeof(Branch <T>))
         {
             LeftChild.Add(value);
         }
         else
         {
             Branch <T> tempBranch = new Branch <T>(value);
             LeftChild = tempBranch;
         }
     }
     else if (value.CompareTo(Value) == 1)
     {
         if (RightChild.GetType() == typeof(Branch <T>))
         {
             RightChild.Add(value);
         }
         else
         {
             Branch <T> tempBranch = new Branch <T>(value);
             RightChild = tempBranch;
         }
     }
     return(this);
 }
Пример #28
0
            private int GetDepth()
            {
                if (IsEmpty)
                {
                    return(0);
                }

                if (LeftChild == null)
                {
                    if (RightChild == null)
                    {
                        return(1);
                    }
                    else
                    {
                        return(RightChild.GetDepth() + 1);
                    }
                }
                else
                {
                    if (RightChild == null)
                    {
                        return(LeftChild.GetDepth() + 1);
                    }
                    else
                    {
                        return(Math.Max(LeftChild.GetDepth(), RightChild.GetDepth()) + 1);
                    }
                }
            }
Пример #29
0
        /// <summary>
        /// Evaluate as boolean
        /// </summary>
        internal override bool BoolEvaluate(ConditionEvaluator.IConditionEvaluationState state)
        {
            bool isLeftNum      = LeftChild.TryNumericEvaluate(state, out double leftNum);
            bool isLeftVersion  = LeftChild.TryVersionEvaluate(state, out Version leftVersion);
            bool isRightNum     = RightChild.TryNumericEvaluate(state, out double rightNum);
            bool isRightVersion = RightChild.TryVersionEvaluate(state, out Version rightVersion);

            if ((!isLeftNum && !isLeftVersion) || (!isRightNum && !isRightVersion))
            {
                ProjectErrorUtilities.ThrowInvalidProject(
                    state.ElementLocation,
                    "ComparisonOnNonNumericExpression",
                    state.Condition,
                    /* helpfully display unexpanded token and expanded result in error message */
                    isLeftNum ? RightChild.GetUnexpandedValue(state) : LeftChild.GetUnexpandedValue(state),
                    isLeftNum ? RightChild.GetExpandedValue(state) : LeftChild.GetExpandedValue(state));
            }

            return((isLeftNum, isLeftVersion, isRightNum, isRightVersion) switch
            {
                (true, _, true, _) => Compare(leftNum, rightNum),
                (_, true, _, true) => Compare(leftVersion, rightVersion),
                (true, _, _, true) => Compare(leftNum, rightVersion),
                (_, true, true, _) => Compare(leftVersion, rightNum),

                _ => false
            });
Пример #30
0
        public void Insert(int inputData)
        {
            //Right input>= root value
            if (inputData >= Data)
            {
                if (RightChild == null) //this is termination condition to add node cause its a leaf node
                {
                    RightChild = new TreeNode(inputData);
                }
                else
                {
                    RightChild.Insert(inputData); //Recursive call the insert
                }
            }

            //Left
            if (inputData <= Data)
            {
                if (LeftChild == null) //this is termination condition to add node cause its a leaf node
                {
                    LeftChild = new TreeNode(inputData);
                }
                else
                {
                    LeftChild.Insert(inputData);//Recursive call the insert
                }
            }
        }