Example #1
0
 public void updateTree(B_Tree <ComparableInt> b_Tree)
 {
     bTree = b_Tree;
     Invalidate();
     foundedKey  = null;
     foundedNode = null;
 }
Example #2
0
        private void Split_Child(B_Tree_Node <T> node, int i)
        {
            var z   = new B_Tree_Node <T>(nodeCount++);
            var y   = node.Pointers[i];
            int mid = t - 1;

            for (int j = mid + 1; j < y.Keys.Count; j++)
            {
                z.Keys.Add(y.Keys[j]);
            }
            for (int j = y.Keys.Count - 1; j > mid; j--)
            {
                y.Keys.RemoveAt(j);
            }
            //if (y.Pointers != null)
            //{
            for (int j = mid + 1; j < y.Pointers.Count; j++)
            {
                z.Pointers.Add(y.Pointers[j]);
            }
            for (int j = y.Pointers.Count - 1; j > mid; j--)
            {
                y.Pointers.RemoveAt(j);
            }
            //}

            node.Keys.Insert(i, y.Keys[mid]);
            y.Keys.RemoveAt(mid);
            node.Pointers.Insert(i + 1, z);
        }
Example #3
0
        static private string ToString(B_Tree_Node <T> root, string prefix = "")
        {
            string result = "";

            if (root == null || root.Keys.Count == 0)
            {
                return("");
            }

            result += prefix + "|--";
            prefix += "|   ";
            result += "[";
            var i = 0;

            foreach (var k in root.Keys)
            {
                result += k.ToString();
                if (i != root.Keys.Count - 1)
                {
                    result += ",";
                }
                i++;
            }
            result += "]";
            result += "\n";
            if (root.Pointers != null)
            {
                foreach (var node in root.Pointers)
                {
                    result += ToString(node, prefix);
                }
            }

            return(result);
        }
Example #4
0
        private void ReadNodeFromFile(B_Tree_Node <T> node)
        {
            FileStream   fileStream = new FileStream(TreeDirectory + "/" + node.NodeId + ".txt", FileMode.Open);
            StreamReader reader     = new StreamReader(fileStream);
            var          keys       = reader.ReadLine().Split('\t');

            if (node.Keys == null)
            {
                node.Keys = new List <T>();
            }
            node.Keys.Clear();
            foreach (var key in keys)
            {
                node.Keys.Add(KeyFromStr(key));
            }

            var ids = reader.ReadLine();

            reader.Close();
            if (ids != null)
            {
                node.Pointers.Clear();
                var childIds = ids.Split('\t');
                foreach (var childId in childIds)
                {
                    node.Pointers.Add(new B_Tree_Node <T>(Convert.ToInt32(childId)));
                    nodeCount++;
                }
            }
        }
Example #5
0
        private void InsertNonfull(B_Tree_Node <T> node, T key)
        {
            var i = 0;

            for (i = 0; i < node.Keys.Count; i++)
            {
                if (key.CompareTo(node.Keys[i]) == 0)
                {
                    return;
                }
                if (key.CompareTo(node.Keys[i]) < 0)
                {
                    break;
                }
            }
            if (node.Pointers == null || node.Pointers.Count == 0)
            {
                node.Keys.Insert(i, key);
            }
            else
            {
                if (node.Pointers[i].Keys.Count == 2 * t - 1)
                {
                    Split_Child(node, i);
                    if (key.CompareTo(node.Keys[i]) > 0)
                    {
                        i++;
                    }
                }
                InsertNonfull(node.Pointers[i], key);
            }
        }
Example #6
0
 public void updateTreeWithFoundedElement(B_Tree <ComparableInt> b_Tree, ComparableInt key, B_Tree_Node <ComparableInt> inNode)
 {
     foundedKey  = key;
     foundedNode = inNode;
     bTree       = b_Tree;
     Invalidate();
 }
Example #7
0
        private void WriteNodeToFile(B_Tree_Node <T> node)
        {
            FileStream   fileStream = new FileStream(TreeDirectory + "/" + node.NodeId + ".txt", FileMode.Create);
            StreamWriter writer     = new StreamWriter(fileStream);

            for (int i = 0; i < node.Keys.Count; i++)
            {
                writer.Write(node.Keys[i].ToString());
                if (i < node.Keys.Count - 1)
                {
                    writer.Write("\t");
                }
            }
            writer.WriteLine();
            for (int i = 0; i < node.Pointers.Count; i++)
            {
                writer.Write(node.Pointers[i].NodeId);
                if (i < node.Pointers.Count - 1)
                {
                    writer.Write("\t");
                }
            }
            writer.Flush();
            writer.Close();
        }
Example #8
0
 private void ReadTreeFromFile_r(B_Tree_Node <T> root)
 {
     ReadNodeFromFile(root);
     foreach (var child in root.Pointers)
     {
         ReadTreeFromFile_r(child);
     }
 }
Example #9
0
        private void WriteTreeToFile_r(B_Tree_Node <T> root)
        {
            WriteNodeToFile(root);

            foreach (var node in root.Pointers)
            {
                WriteTreeToFile_r(node);
            }
        }
Example #10
0
 private void ToList_r(B_Tree_Node <T> root, List <T> list)
 {
     if (root.Pointers.Count != 0)
     {
         foreach (var node in root.Pointers)
         {
             ToList_r(node, list);
             //list.AddRange(list);
         }
     }
     list.AddRange(root.Keys);
 }
Example #11
0
 public void Insert(T key)
 {
     if (root.Keys.Count == 2 * t - 1)
     {
         var s = new B_Tree_Node <T>(null, new B_Tree_Node <T>[] { root }, nodeCount++);
         root = s;
         Split_Child(s, 0);
         InsertNonfull(s, key);
     }
     else
     {
         InsertNonfull(root, key);
     }
 }
Example #12
0
        public void ReadFromDir(string dir)
        {
            TreeDirectory = dir;
            FileStream   fileStream = new FileStream(dir + "/" + "root.txt", FileMode.Open);
            StreamReader reader     = new StreamReader(fileStream);

            int rootId = Convert.ToInt32(reader.ReadLine());
            int t      = Convert.ToInt32(reader.ReadLine());

            reader.Close();
            this.t    = t;
            this.root = new B_Tree_Node <T>(rootId);
            nodeCount++;
            ReadTreeFromFile_r(root);
        }
Example #13
0
        private NodeSize DrawTree_r(B_Tree_Node <ComparableInt> node, int x_beg, int y_beg, PaintEventArgs e)
        {
            int             x = x_beg, y = y_beg + oneNodeHeight + 20;
            int             size  = 0;
            List <NodeSize> sizes = new List <NodeSize>();

            foreach (var i in node.Pointers)
            {
                NodeSize nodeSize = DrawTree_r(i, x, y, e);
                size = nodeSize.ChildrenSize;
                sizes.Add(nodeSize);
                x += size + 20;
            }
            if (x == x_beg)//Is leaf
            {
                size = DrawNode(node, x_beg, y_beg, e);
                return(new NodeSize()
                {
                    ChildrenSize = size, currentNodeSize = size, currentNodeXBegin = x_beg
                });
            }
            if (x > Size.Width)
            {
                Size = new Size(x + 2 * oneKeyWidth, Size.Height);
            }
            if (y > Size.Height)
            {
                Size = new Size(Size.Width, 2 * y);
            }
            int element_begin = (x_beg + x - size - 20) / 2;

            int element_size = DrawNode(node, element_begin, y_beg, e);

            for (int pointerIndex = 0; pointerIndex < sizes.Count; pointerIndex++)
            {
                e.Graphics.DrawLine(rectanglePen, element_begin + element_size / (sizes.Count - 1) * pointerIndex, y - 20, sizes[pointerIndex].currentNodeXBegin + sizes[pointerIndex].currentNodeSize / 2, y);
            }
            return(new NodeSize()
            {
                ChildrenSize = x - x_beg, currentNodeSize = element_size, currentNodeXBegin = element_begin
            });
        }
Example #14
0
        private int DrawNode(B_Tree_Node <ComparableInt> node, int x_beg, int y_beg, PaintEventArgs e)
        {
            int nodeSize = node.Keys.Count * oneKeyWidth + (node.Keys.Count - 1) * delimiterSize;

            e.Graphics.DrawRectangle(rectanglePen, x_beg, y_beg, nodeSize, oneNodeHeight);
            int currX = x_beg + delimiterSize, currY = y_beg + delimiterSize;

            foreach (var i in node.Keys)
            {
                if (foundedKey != null && i.Value == foundedKey.Value)
                {
                    e.Graphics.FillRectangle(foundedElementBrush, currX, currY, oneKeyWidth, oneNodeHeight);
                }
                e.Graphics.DrawString(i.ToString(), stringFont, stringBrush, currX, currY);
                currX += oneKeyWidth;
                e.Graphics.DrawLine(rectanglePen, currX, currY, currX, currY + oneNodeHeight);
                currX += delimiterSize;
            }
            return(nodeSize);
        }
Example #15
0
        private FoundElement <T> Search_r(T key, B_Tree_Node <T> root)
        {
            FoundElement <T> foundElement = new FoundElement <T>();

            int i = 0;

            while (i < root.Keys.Count && root.Keys[i].CompareTo(key) < 0)
            {
                i++;
            }
            if (i < root.Keys.Count && root.Keys[i].CompareTo(key) == 0)
            {
                return(new FoundElement <T> {
                    index = i, node = root
                });
            }

            if (root.Pointers != null && root.Pointers.Count > i)
            {
                return(Search_r(key, root.Pointers[i]));
            }

            return(null);
        }
Example #16
0
        private void Remove_r(B_Tree_Node <T> node, T key)
        {
            var index = -1;

            for (int i = 0; i < node.Keys.Count; i++)//Ищем ключ в текущем узле
            {
                if (key.CompareTo(node.Keys[i]) == 0)
                {
                    index = i;
                }
            }
            if (index >= 0)                                            //Ключ в текущем узле
            {
                if (node.Pointers == null || node.Pointers.Count == 0) //Текущий узел - лист
                {
                    node.Keys.RemoveAt(index);
                }
                else
                {
                    var y = node.Pointers[index];
                    var z = node.Pointers[index + 1];
                    if (y.Keys.Count >= t)
                    {
                        var key1 = y.Keys[y.Keys.Count - 1];
                        Remove_r(y, key1);
                        node.Keys[index] = key1;
                    }
                    else if (z.Keys.Count >= t)
                    {
                        var key1 = z.Keys[0];
                        Remove_r(z, key1);
                        node.Keys[index] = key1;
                    }
                    else
                    {
                        y.Keys.Add(key);
                        foreach (var k in z.Keys)
                        {
                            y.Keys.Add(k);
                        }
                        foreach (var p in z.Pointers)
                        {
                            y.Pointers.Add(p);
                        }
                        node.Keys.RemoveAt(index);
                        node.Pointers.RemoveAt(index + 1);
                        if (node == root && node.Keys.Count == 0)
                        {
                            root = y;
                        }
                        Remove_r(y, key);
                    }
                }
            }
            else if (node.Pointers != null && node.Pointers.Count > 0)
            {
                var i = 0;
                for (i = 0; i < node.Keys.Count; i++)
                {
                    if (key.CompareTo(node.Keys[i]) < 0)
                    {
                        break;
                    }
                }
                if (node.Pointers[i].Keys.Count >= t)
                {
                    Remove_r(node.Pointers[i], key);
                }
                else
                {
                    //var y = node.Pointers[i - 1];
                    var x = node.Pointers[i];
                    if (i + 1 < node.Pointers.Count && node.Pointers[i + 1].Keys.Count >= t)
                    {
                        var z = node.Pointers[i + 1];
                        x.Keys.Add(node.Keys[i]);
                        if (x.Pointers.Count > 0)
                        {
                            x.Pointers.Add(z.Pointers[0]);
                            z.Pointers.RemoveAt(0);
                        }

                        node.Keys[i] = z.Keys[0];
                        z.Keys.RemoveAt(0);
                        Remove_r(x, key);
                    }
                    else if (i - 1 >= 0 && node.Pointers[i - 1].Keys.Count >= t)
                    {
                        var y = node.Pointers[i - 1];
                        x.Keys.Insert(0, node.Keys[i - 1]);
                        if (x.Pointers.Count > 0)
                        {
                            x.Pointers.Insert(0, y.Pointers[y.Pointers.Count - 1]);
                            y.Pointers.RemoveAt(y.Pointers.Count - 1);
                        }

                        node.Keys[i - 1] = y.Keys[y.Keys.Count - 1];
                        y.Keys.RemoveAt(y.Keys.Count - 1);
                        Remove_r(x, key);
                    }
                    else
                    {
                        var y = new B_Tree_Node <T>(nodeCount++);
                        if (i + 1 < node.Pointers.Count)
                        {
                            y = node.Pointers[i + 1];
                            x.Keys.Add(node.Keys[i]);
                            node.Keys.RemoveAt(i);
                            for (int j = 0; j < y.Keys.Count; j++)
                            {
                                x.Keys.Add(y.Keys[j]);
                            }
                            for (int j = 0; j < y.Pointers.Count; j++)
                            {
                                x.Pointers.Add(y.Pointers[j]);
                            }
                            node.Pointers.RemoveAt(i + 1);
                            Remove_r(x, key);
                        }
                        else if (i - 1 >= 0)
                        {
                            y = node.Pointers[i - 1];
                            x.Keys.Insert(0, node.Keys[i - 1]);
                            node.Keys.RemoveAt(i - 1);
                            for (int j = y.Keys.Count - 1; j >= 0; j--)
                            {
                                x.Keys.Insert(0, y.Keys[j]);
                            }
                            for (int j = y.Pointers.Count - 1; j >= 0; j--)
                            {
                                x.Pointers.Insert(0, y.Pointers[j]);
                            }
                            node.Pointers.RemoveAt(i - 1);
                            if (node.Keys.Count == 0 && node == root)
                            {
                                root = x;
                            }
                            Remove_r(x, key);
                        }
                    }
                }
            }
        }
Example #17
0
 public B_Tree(int t, keyFromString fromStr)
 {
     this.t     = t;
     KeyFromStr = fromStr;
     root       = new B_Tree_Node <T>(nodeCount++);
 }