Пример #1
0
        public TreeNode FindSubtreeLeaf(TreeNode node, int element)
        {
            if (node.IsLeaf())
            {
                return(node);
            }

            else
            {
                if (element < node.elements[0])
                {
                    return(this.FindSubtreeLeaf(node.children[0], element));
                }

                else if (node.NumberOfElements() == 1 || (element > node.elements[0] && element < node.elements[1]))
                {
                    return(this.FindSubtreeLeaf(node.children[1], element));
                }

                else if (node.NumberOfElements() == 2 || (element > node.elements[1] && element < node.elements[2]))
                {
                    return(this.FindSubtreeLeaf(node.children[2], element));
                }

                else
                {
                    return(this.FindSubtreeLeaf(node.children[3], element));
                }
            }
        }
Пример #2
0
        public bool TryInsert(TreeNode treeNode, int element)
        {
            bool result = true;

            if (treeNode.NumberOfElements() == 0 && treeNode.parent == null)
            {
                treeNode.elements[0] = element;
            }

            else if (treeNode.NumberOfElements() < this.limit)
            {
                treeNode.elements[treeNode.NumberOfElements()] = element;

                this.SortElements(treeNode.elements, treeNode.NumberOfElements());
            }
            else
            {
                result = false;
            }

            return(result);
        }
Пример #3
0
        //Tries to insert the elements, if not returns false
        public bool TryInsert(TreeNode treeNode, int element)
        {
            bool result = true;

            //If it is root just add the element to the root
            if (treeNode.NumberOfElements() == 0 && treeNode.parent == null)
            {
                treeNode.elements[0] = element;

            }

            else if (treeNode.NumberOfElements() < this.limit)
            {
                treeNode.elements[treeNode.NumberOfElements()] = element;

                this.SortElements(treeNode.elements, treeNode.NumberOfElements());

            }
            else
            {
                result = false;
            }

            return result;
        }
Пример #4
0
        //Look for the right or the closest siblings with two or three elements, if not returns -1
        public int HasChildWithTwoElement(TreeNode node)
        {
            int result = -1;

            //check the number of the children equality to 2
            if (node.NumberOfElements() == 1)
            {
                for (int i = 0; i < node.children.Length; i++)
                {
                    if (node.children[i] != null)
                    {
                        if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                        {
                            result = i;
                            result = result + node.children[i].NumberOfElements() * 10;
                            break;
                        }
                    }
                }
            }

            //number of the children equality to 3
            else if (node.NumberOfElements() == 2)
            {
                if (node.children[0].NumberOfElements() == 0)
                {
                    for (int i = 0; i < node.children.Length; i++)
                    {
                        if (node.children[i] != null)
                        {
                            if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                            {
                                result = i;
                                result = result + node.children[i].NumberOfElements() * 10;
                                break;
                            }
                        }
                    }
                }

                // I should take from the right first so the loop counter is decreasing
                else
                {
                    for (int i = node.children.Length - 1; i >= 0; i--)
                    {
                        if (node.children[i] != null)
                        {
                            if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                            {
                                result = i;
                                result = result + node.children[i].NumberOfElements() * 10;
                                break;
                            }
                        }
                    }
                }
            }

            //number of the children equality to 4
            else if (node.NumberOfElements() == 3)
            {
                if (node.children[0].NumberOfElements() == 0)
                {
                    for (int i = 0; i < node.children.Length; i++)
                    {
                        if (node.children[i] != null)
                        {
                            if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                            {
                                result = i;
                                result = result + node.children[i].NumberOfElements() * 10;
                                break;
                            }
                        }
                    }
                }

               // I should take from the right first so the loop counter is decreasing
                else
                {

                    if (node.children[1].NumberOfElements() == 0)
                    {
                        //Check for the element at the right first
                        for (int i = 1; i < node.children.Length; i++)
                        {
                            if (node.children[i] != null)
                            {
                                if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                                {
                                    result = i;
                                    result = result + node.children[i].NumberOfElements() * 10;
                                    break;
                                }
                            }
                        }

                        //If doesn't exist in the right look left
                        if (result == -1)
                        {
                            if (node.children[0].NumberOfElements() == 2 || node.children[0].NumberOfElements() == 3)
                            {
                                result = node.children[0].NumberOfElements() * 10;
                            }
                        }
                    }

                    else
                    {
                        for (int i = node.children.Length - 1; i >= 0; i--)
                        {
                            if (node.children[i] != null)
                            {
                                if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                                {
                                    result = i;
                                    result = result + node.children[i].NumberOfElements() * 10;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
Пример #5
0
        public TreeNode FindSubtreeLeaf(TreeNode node, int element)
        {
            if (node.IsLeaf())
                return node;

            else
            {
                if (element < node.elements[0])
                    return this.FindSubtreeLeaf(node.children[0], element);

                else if (node.NumberOfElements() == 1 || (element > node.elements[0] && element < node.elements[1]))
                    return this.FindSubtreeLeaf(node.children[1], element);

                else if (node.NumberOfElements() == 2 || (element > node.elements[1] && element < node.elements[2]))
                    return this.FindSubtreeLeaf(node.children[2], element);

                else
                    return this.FindSubtreeLeaf(node.children[3], element);
            }
        }
Пример #6
0
        public TreeNode FindNode(TreeNode node, int element)
        {
            bool isFound = false;
            if (node != null)
            {
                for (int i = 0; i < node.NumberOfElements(); i++)
                {
                    if (node.elements[i] == element)
                        isFound = true;
                }

                if (isFound == true)
                    return node;

                else if (node.NumberOfElements() == 1)
                {
                    if (element < node.elements[0])
                        return FindNode(node.children[0], element);
                    else
                        return FindNode(node.children[1], element);
                }
                else if (node.NumberOfElements() == 2)
                {
                    if (element < node.elements[0])
                    {
                        return FindNode(node.children[0], element);
                    }
                    else if (element > node.elements[1])
                    {
                        return FindNode(node.children[2], element);
                    }
                    else
                    {
                        return FindNode(node.children[1], element);
                    }
                }
                else if (node.NumberOfElements() == 3)
                {
                    if (element < node.elements[0])
                    {
                        return FindNode(node.children[0], element);
                    }
                    else if (element > node.elements[0] && element < node.elements[1])
                    {
                        return FindNode(node.children[1], element);
                    }
                    else if (element > node.elements[1] && element < node.elements[2])
                    {
                        return FindNode(node.children[2], element);
                    }
                    else
                    {
                        return FindNode(node.children[3], element);
                    }
                }
            }

            return null;
        }
Пример #7
0
        public int HasChildWithTwoElement(TreeNode node)
        {
            int result = -1;

            if (node.NumberOfElements() == 1)
            {
                for (int i = 0; i < node.children.Length; i++)
                {
                    if (node.children[i] != null)
                    {
                        if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                        {
                            result = i;
                            result = result + node.children[i].NumberOfElements() * 10;
                            break;
                        }
                    }
                }
            }

            else if (node.NumberOfElements() == 2)
            {
                if (node.children[0].NumberOfElements() == 0)
                {
                    for (int i = 0; i < node.children.Length; i++)
                    {
                        if (node.children[i] != null)
                        {
                            if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                            {
                                result = i;
                                result = result + node.children[i].NumberOfElements() * 10;
                                break;
                            }
                        }
                    }
                }

                else
                {
                    for (int i = node.children.Length - 1; i >= 0; i--)
                    {
                        if (node.children[i] != null)
                        {
                            if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                            {
                                result = i;
                                result = result + node.children[i].NumberOfElements() * 10;
                                break;
                            }
                        }
                    }
                }
            }

            else if (node.NumberOfElements() == 3)
            {
                if (node.children[0].NumberOfElements() == 0)
                {
                    for (int i = 0; i < node.children.Length; i++)
                    {
                        if (node.children[i] != null)
                        {
                            if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                            {
                                result = i;
                                result = result + node.children[i].NumberOfElements() * 10;
                                break;
                            }
                        }
                    }
                }

                else
                {
                    if (node.children[1].NumberOfElements() == 0)
                    {
                        for (int i = 1; i < node.children.Length; i++)
                        {
                            if (node.children[i] != null)
                            {
                                if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                                {
                                    result = i;
                                    result = result + node.children[i].NumberOfElements() * 10;
                                    break;
                                }
                            }
                        }

                        if (result == -1)
                        {
                            if (node.children[0].NumberOfElements() == 2 || node.children[0].NumberOfElements() == 3)
                            {
                                result = node.children[0].NumberOfElements() * 10;
                            }
                        }
                    }

                    else
                    {
                        for (int i = node.children.Length - 1; i >= 0; i--)
                        {
                            if (node.children[i] != null)
                            {
                                if (node.children[i].NumberOfElements() == 2 || node.children[i].NumberOfElements() == 3)
                                {
                                    result = i;
                                    result = result + node.children[i].NumberOfElements() * 10;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Пример #8
0
        public TreeNode FindNode(TreeNode node, int element)
        {
            bool isFound = false;

            if (node != null)
            {
                for (int i = 0; i < node.NumberOfElements(); i++)
                {
                    if (node.elements[i] == element)
                    {
                        isFound = true;
                    }
                }

                if (isFound == true)
                {
                    return(node);
                }

                else if (node.NumberOfElements() == 1)
                {
                    if (element < node.elements[0])
                    {
                        return(FindNode(node.children[0], element));
                    }
                    else
                    {
                        return(FindNode(node.children[1], element));
                    }
                }
                else if (node.NumberOfElements() == 2)
                {
                    if (element < node.elements[0])
                    {
                        return(FindNode(node.children[0], element));
                    }
                    else if (element > node.elements[1])
                    {
                        return(FindNode(node.children[2], element));
                    }
                    else
                    {
                        return(FindNode(node.children[1], element));
                    }
                }
                else if (node.NumberOfElements() == 3)
                {
                    if (element < node.elements[0])
                    {
                        return(FindNode(node.children[0], element));
                    }
                    else if (element > node.elements[0] && element < node.elements[1])
                    {
                        return(FindNode(node.children[1], element));
                    }
                    else if (element > node.elements[1] && element < node.elements[2])
                    {
                        return(FindNode(node.children[2], element));
                    }
                    else
                    {
                        return(FindNode(node.children[3], element));
                    }
                }
            }

            return(null);
        }