From 16a36836f5ac121e9d431e15769e285234e16dd1 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Tue, 23 Mar 2021 01:32:29 +0300 Subject: [PATCH 01/12] start code Insert function --- hw3B-tree/hw3B-tree.sln | 25 ++++++++ hw3B-tree/hw3B-tree/BTree.cs | 95 ++++++++++++++++++++++++++++ hw3B-tree/hw3B-tree/Program.cs | 12 ++++ hw3B-tree/hw3B-tree/hw3B-tree.csproj | 9 +++ 4 files changed, 141 insertions(+) create mode 100644 hw3B-tree/hw3B-tree.sln create mode 100644 hw3B-tree/hw3B-tree/BTree.cs create mode 100644 hw3B-tree/hw3B-tree/Program.cs create mode 100644 hw3B-tree/hw3B-tree/hw3B-tree.csproj diff --git a/hw3B-tree/hw3B-tree.sln b/hw3B-tree/hw3B-tree.sln new file mode 100644 index 0000000..629ba4c --- /dev/null +++ b/hw3B-tree/hw3B-tree.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31005.135 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw3B-tree", "hw3B-tree\hw3B-tree.csproj", "{53642C16-3AA3-4A57-8380-46B9C2E9651E}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {53642C16-3AA3-4A57-8380-46B9C2E9651E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53642C16-3AA3-4A57-8380-46B9C2E9651E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53642C16-3AA3-4A57-8380-46B9C2E9651E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53642C16-3AA3-4A57-8380-46B9C2E9651E}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A28793F3-951B-4D93-BC1D-F351834FA2F5} + EndGlobalSection +EndGlobal diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs new file mode 100644 index 0000000..49c0b20 --- /dev/null +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace hw3B_tree +{ + class BTree + { + public int MinimumDegreeOfTree { get; set; } + + class Node + { + + public int CountKeys { get; set; } + public (string key, string value)[] Keys { get; set; } + + public Node[] Sons { get; set; } + + public bool Leaf { get; set; } + + public Node(int minimumDegreeOfTree) + { + CountKeys = 0; + Leaf = false; + var Keys = new (string key, string value)[2 * minimumDegreeOfTree - 1]; + Sons = new Node[2 * minimumDegreeOfTree]; + } + } + + private Node root; + + public BTree(int minimumDegreeOfTree) + { + MinimumDegreeOfTree = minimumDegreeOfTree; + root = null; + } + + public void Insert(string key, string value) + { + var runner = root; + if (runner == null) + { + root = new Node(MinimumDegreeOfTree); + } + if (runner.CountKeys == (2 * MinimumDegreeOfTree) - 1) + { + var newRoot = new Node(MinimumDegreeOfTree); + root = newRoot; + newRoot.Sons[newRoot.CountKeys] = root; + // Split function + Split(); + // function InsertInNode + InsertInNode(key, value); + } + else + { + InsertInNode(key, value); + } + + private void Split() + { + + } + + private void InsertInNode(string key, string value) + { + int size = root.CountKeys; // ??? + if (root.Leaf == true) + { + + // вставить key впорядке возрастания + for (int i = 0; i < size; ++i) + { + if (String.Compare(root.Keys[i].key, key) < 0) + { + var keySwap = root.Keys[i].key; + var valueSwap = root.Keys[i].value; + root.Keys[i].key = key; + root.Keys[i].value = value; + for (int j = i; j < size; ++j) + { + // дошли до вставляемого индекса, теперь надо сдвинуть оставшиеся + } + } + } + } + else + { + + } + } + } + + } +} \ No newline at end of file diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs new file mode 100644 index 0000000..df8a05a --- /dev/null +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace hw3B_tree +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/hw3B-tree/hw3B-tree/hw3B-tree.csproj b/hw3B-tree/hw3B-tree/hw3B-tree.csproj new file mode 100644 index 0000000..5b12930 --- /dev/null +++ b/hw3B-tree/hw3B-tree/hw3B-tree.csproj @@ -0,0 +1,9 @@ + + + + Exe + netcoreapp3.1 + hw3B_tree + + + From 75031730b38e220405428b55fda7fa535f4bf369 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Wed, 24 Mar 2021 00:43:20 +0300 Subject: [PATCH 02/12] Split don't finish --- hw3B-tree/hw3B-tree/BTree.cs | 66 ++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs index 49c0b20..fa5b577 100644 --- a/hw3B-tree/hw3B-tree/BTree.cs +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -18,10 +18,10 @@ class Node public bool Leaf { get; set; } - public Node(int minimumDegreeOfTree) + public Node(int minimumDegreeOfTree, bool leaf) { CountKeys = 0; - Leaf = false; + Leaf = leaf; var Keys = new (string key, string value)[2 * minimumDegreeOfTree - 1]; Sons = new Node[2 * minimumDegreeOfTree]; } @@ -29,6 +29,8 @@ public Node(int minimumDegreeOfTree) private Node root; + private Node runner; + public BTree(int minimumDegreeOfTree) { MinimumDegreeOfTree = minimumDegreeOfTree; @@ -40,11 +42,11 @@ public void Insert(string key, string value) var runner = root; if (runner == null) { - root = new Node(MinimumDegreeOfTree); + root = new Node(MinimumDegreeOfTree, true); } if (runner.CountKeys == (2 * MinimumDegreeOfTree) - 1) { - var newRoot = new Node(MinimumDegreeOfTree); + var newRoot = new Node(MinimumDegreeOfTree, false); root = newRoot; newRoot.Sons[newRoot.CountKeys] = root; // Split function @@ -57,37 +59,51 @@ public void Insert(string key, string value) InsertInNode(key, value); } - private void Split() + void Split(int index, Node node)// разобраться со split { - + var newNode = new Node(MinimumDegreeOfTree, runner.Leaf); + newNode.CountKeys = MinimumDegreeOfTree - 1; + Array.Copy(runner.Keys, MinimumDegreeOfTree, newNode.Keys, 0, MinimumDegreeOfTree - 1); + if (!runner.Leaf) + { + Array.Copy(runner.Sons, MinimumDegreeOfTree, newNode.Sons, 0, MinimumDegreeOfTree); + } + runner.CountKeys = MinimumDegreeOfTree - 1; } - private void InsertInNode(string key, string value) + void InsertInNode(string key, string value) { - int size = root.CountKeys; // ??? - if (root.Leaf == true) + int size = runner.CountKeys; // + int index = size - 1; + if (runner.Leaf == true) { - - // вставить key впорядке возрастания - for (int i = 0; i < size; ++i) + while (index >= 0 && String.Compare(runner.Keys[index].key, key) < 0) // проверитб сравнение { - if (String.Compare(root.Keys[i].key, key) < 0) - { - var keySwap = root.Keys[i].key; - var valueSwap = root.Keys[i].value; - root.Keys[i].key = key; - root.Keys[i].value = value; - for (int j = i; j < size; ++j) - { - // дошли до вставляемого индекса, теперь надо сдвинуть оставшиеся - } - } + runner.Keys[index + 1].key = runner.Keys[index].key; + runner.Keys[index + 1].value = runner.Keys[index].value; + index--; } + runner.Keys[index + 1].key = key; + runner.Keys[index + 1].value = value; + runner.CountKeys++; } else { - - } + while (index >= 0 && String.Compare(root.Keys[index].key, key) < 0) // проверитб сравнение + { + index--; + } + if (root.Sons[index].CountKeys == (2 * MinimumDegreeOfTree - 1)) + { + Split(); + if (String.Compare(root.Keys[index].key, key) < 0) // проверитб сравнение + { + index++; + } + } + runner = runner.Sons[index + 1]; + InsertInNode(key, value); + } } } From b93fe0e6c601c81a3e047780e659b0f87f5987f1 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 26 Mar 2021 02:36:05 +0300 Subject: [PATCH 03/12] add GetValue and IsConsist in BTree. connect TestB-tree.cs, and write Menu in Program.cs --- hw3B-tree/hw3B-tree.Test/TestB-tree.cs | 46 +++++ .../hw3B-tree.Test/hw3B-tree.Test.csproj | 20 +++ hw3B-tree/hw3B-tree.sln | 8 +- hw3B-tree/hw3B-tree/BTree.cs | 163 ++++++++++++++---- hw3B-tree/hw3B-tree/Program.cs | 57 +++++- 5 files changed, 259 insertions(+), 35 deletions(-) create mode 100644 hw3B-tree/hw3B-tree.Test/TestB-tree.cs create mode 100644 hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj diff --git a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs new file mode 100644 index 0000000..40fd216 --- /dev/null +++ b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs @@ -0,0 +1,46 @@ +using NUnit.Framework; + +namespace hw3B_tree.Test +{ + public class Tests + { + private BTree tree = new BTree(2); + + [SetUp] + public void Setup() + { + tree.Insert("1", "a"); + tree.Insert("2", "b"); + tree.Insert("3", "c"); + tree.Insert("4", "d"); + tree.Insert("5", "e"); + tree.Insert("6", "f"); + tree.Insert("7", "g"); + tree.Insert("8", "h"); + } + + [TestCase] + public void CheckInsert1() + { + Assert.IsTrue(tree.IsConsist("6")); + } + + [TestCase] + public void CheckInsert2() + { + Assert.IsTrue(tree.IsConsist("8")); + } + + [TestCase] + public void CheckGetValue1() + { + Assert.IsTrue(tree.GetValue("5") == "e"); + } + + [TestCase] + public void CheckGetValue2() + { + Assert.IsTrue(tree.GetValue("8") == "h"); + } + } +} \ No newline at end of file diff --git a/hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj b/hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj new file mode 100644 index 0000000..20f5a14 --- /dev/null +++ b/hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + hw3B_tree.Test + + false + + + + + + + + + + + + + diff --git a/hw3B-tree/hw3B-tree.sln b/hw3B-tree/hw3B-tree.sln index 629ba4c..5ed2491 100644 --- a/hw3B-tree/hw3B-tree.sln +++ b/hw3B-tree/hw3B-tree.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31005.135 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw3B-tree", "hw3B-tree\hw3B-tree.csproj", "{53642C16-3AA3-4A57-8380-46B9C2E9651E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw3B-tree", "hw3B-tree\hw3B-tree.csproj", "{53642C16-3AA3-4A57-8380-46B9C2E9651E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw3B-tree.Test", "hw3B-tree.Test\hw3B-tree.Test.csproj", "{A2EE96D2-650B-457E-87E5-A6410F6DF06D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {53642C16-3AA3-4A57-8380-46B9C2E9651E}.Debug|Any CPU.Build.0 = Debug|Any CPU {53642C16-3AA3-4A57-8380-46B9C2E9651E}.Release|Any CPU.ActiveCfg = Release|Any CPU {53642C16-3AA3-4A57-8380-46B9C2E9651E}.Release|Any CPU.Build.0 = Release|Any CPU + {A2EE96D2-650B-457E-87E5-A6410F6DF06D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A2EE96D2-650B-457E-87E5-A6410F6DF06D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A2EE96D2-650B-457E-87E5-A6410F6DF06D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A2EE96D2-650B-457E-87E5-A6410F6DF06D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs index fa5b577..d97cacd 100644 --- a/hw3B-tree/hw3B-tree/BTree.cs +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -4,11 +4,11 @@ namespace hw3B_tree { - class BTree + public class BTree { public int MinimumDegreeOfTree { get; set; } - class Node + private class Node { public int CountKeys { get; set; } @@ -22,7 +22,7 @@ public Node(int minimumDegreeOfTree, bool leaf) { CountKeys = 0; Leaf = leaf; - var Keys = new (string key, string value)[2 * minimumDegreeOfTree - 1]; + Keys = new (string key, string value)[2 * minimumDegreeOfTree - 1]; Sons = new Node[2 * minimumDegreeOfTree]; } } @@ -33,78 +33,175 @@ public Node(int minimumDegreeOfTree, bool leaf) public BTree(int minimumDegreeOfTree) { + if (minimumDegreeOfTree < 2) + { + throw new ArgumentException("Минимальная степень дерева выбрана неправильна!"); + } MinimumDegreeOfTree = minimumDegreeOfTree; - root = null; + } + + public bool IsConsist(string key) + { + var node = root; + while (!node.Leaf) + { + for (int i = 0; i < node.CountKeys; ++i) + { + if (key == node.Keys[i].key) + { + return true; + } + if (CompareKey(key, node.Keys[i].key)) + { + node = node.Sons[i]; + break; + } + if (i == node.CountKeys - 1) + { + node = node.Sons[i + 1]; + } + } + } + for (int i = 0; i < node.CountKeys; ++i) + { + if (key == node.Keys[i].key) + { + return true; + } + } + return false; + } + + + public string GetValue(string key) + { + var node = root; + while (!node.Leaf) + { + for (int i = 0; i < node.CountKeys; ++i) + { + if (key == node.Keys[i].key) + { + return node.Keys[i].value; + } + if (CompareKey(key, node.Keys[i].key)) + { + node = node.Sons[i]; + break; + } + if (i == node.CountKeys - 1) + { + node = node.Sons[i + 1]; + } + } + } + for (int i = 0; i < node.CountKeys; ++i) + { + if (key == node.Keys[i].key) + { + return node.Keys[i].value; + } + } + return null; + } + + public bool CompareKey(string key, string keyInNode) + { + if (key.Length < keyInNode.Length) + { + return true; + } + else if (key.Length > keyInNode.Length) + { + return false; + } + return String.Compare(key, keyInNode) < 0 ? true : false; } public void Insert(string key, string value) { var runner = root; - if (runner == null) + if (root == null) { root = new Node(MinimumDegreeOfTree, true); + root.Keys[0].key = key; + root.Keys[0].value = value; + root.CountKeys++; } - if (runner.CountKeys == (2 * MinimumDegreeOfTree) - 1) + else if (root.CountKeys == (2 * MinimumDegreeOfTree) - 1) { var newRoot = new Node(MinimumDegreeOfTree, false); - root = newRoot; newRoot.Sons[newRoot.CountKeys] = root; - // Split function - Split(); - // function InsertInNode - InsertInNode(key, value); + root = newRoot; + Split(0, root); + InsertInNode(key, value, root); } else { - InsertInNode(key, value); + InsertInNode(key, value, runner); } - void Split(int index, Node node)// разобраться со split + void Split(int index, Node node) { - var newNode = new Node(MinimumDegreeOfTree, runner.Leaf); + var helpNode = node.Sons[index]; + var newNode = new Node(MinimumDegreeOfTree, helpNode.Leaf); newNode.CountKeys = MinimumDegreeOfTree - 1; - Array.Copy(runner.Keys, MinimumDegreeOfTree, newNode.Keys, 0, MinimumDegreeOfTree - 1); - if (!runner.Leaf) + Array.Copy(helpNode.Keys, MinimumDegreeOfTree, newNode.Keys, 0, MinimumDegreeOfTree - 1); + if (!node.Leaf) + { + Array.Copy(helpNode.Sons, MinimumDegreeOfTree, newNode.Sons, 0, MinimumDegreeOfTree); + } + helpNode.CountKeys = MinimumDegreeOfTree - 1; + for (int i = node.CountKeys; i >= index + 1; i--) { - Array.Copy(runner.Sons, MinimumDegreeOfTree, newNode.Sons, 0, MinimumDegreeOfTree); + node.Sons[i + 1] = node.Sons[i]; } - runner.CountKeys = MinimumDegreeOfTree - 1; + node.Sons[index + 1] = newNode; + for (int i = node.CountKeys - 1; i >= index; i--) + { + node.Keys[i + 1].key = node.Keys[i].key; + node.Keys[i + 1].value = node.Keys[i].value; + } + node.Keys[index].key = helpNode.Keys[MinimumDegreeOfTree - 1].key; + node.Keys[index].value = helpNode.Keys[MinimumDegreeOfTree - 1].value; + node.CountKeys++; } - void InsertInNode(string key, string value) + void InsertInNode(string key, string value, Node node) { - int size = runner.CountKeys; // + int size = node.CountKeys; // int index = size - 1; - if (runner.Leaf == true) + if (node.Leaf) { - while (index >= 0 && String.Compare(runner.Keys[index].key, key) < 0) // проверитб сравнение + while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение { - runner.Keys[index + 1].key = runner.Keys[index].key; - runner.Keys[index + 1].value = runner.Keys[index].value; + node.Keys[index + 1].key = node.Keys[index].key; + node.Keys[index + 1].value = node.Keys[index].value; index--; } - runner.Keys[index + 1].key = key; - runner.Keys[index + 1].value = value; - runner.CountKeys++; + node.Keys[index + 1].key = key; + node.Keys[index + 1].value = value; + node.CountKeys++; } else { - while (index >= 0 && String.Compare(root.Keys[index].key, key) < 0) // проверитб сравнение + while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение { index--; } - if (root.Sons[index].CountKeys == (2 * MinimumDegreeOfTree - 1)) + index++; + if (node.Sons[index].CountKeys == (2 * MinimumDegreeOfTree - 1)) { - Split(); - if (String.Compare(root.Keys[index].key, key) < 0) // проверитб сравнение + Split(index, node); + if (!CompareKey(key, node.Keys[index].key)) { index++; } } - runner = runner.Sons[index + 1]; - InsertInNode(key, value); + InsertInNode(key, value, node.Sons[index]); } } + } } diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index df8a05a..7f71069 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -6,7 +6,62 @@ class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + var tree = new BTree(2); + Console.WriteLine("Меню словаря"); + Console.WriteLine("0 - добавить слово в словарь."); + Console.WriteLine("1 - вернуть слово по ключу."); + Console.WriteLine("2 - проверка на наличия ключа в словаре."); + Console.WriteLine("3 - изменение слова по ключу."); + Console.WriteLine("4 - удаление слова по ключу."); + while (true) + { + Console.WriteLine("Ваш выбор."); + var str = Console.ReadLine(); + if (!int.TryParse(str, out int choice)) + { + Console.WriteLine("Ошибка ввода!"); + Console.WriteLine("Попробуйте еще раз!"); + continue; + } + var key = ""; + var value = ""; + switch (choice) + { + case 0: + Console.WriteLine("Введите ключ."); + key = Console.ReadLine(); + Console.WriteLine("Введите слово."); + value = Console.ReadLine(); + tree.Insert(key, value); + break; + case 1: + Console.WriteLine("Введите ключ."); + key = Console.ReadLine(); + str = tree.GetValue(key); + if (str != null) + { + Console.WriteLine("Ваше слово -", str); + } + else + { + Console.WriteLine("Слова с таким ключом нет."); + } + break; + case 2: + + break; + case 3: + + break; + case 4: + + break; + default: + Console.WriteLine("Ошибка ввода!"); + Console.WriteLine("Попробуйте еще раз!"); + break; + } + } } } } From f7ec8da97183ba6cd63a3eaeff3f57c2cf771d0e Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Fri, 26 Mar 2021 22:22:44 +0300 Subject: [PATCH 04/12] add function ChangeValueByKey --- hw3B-tree/hw3B-tree/BTree.cs | 214 +++++++++++++++++++++++---------- hw3B-tree/hw3B-tree/Program.cs | 113 +++++++++-------- 2 files changed, 210 insertions(+), 117 deletions(-) diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs index d97cacd..669baab 100644 --- a/hw3B-tree/hw3B-tree/BTree.cs +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -40,7 +40,7 @@ public BTree(int minimumDegreeOfTree) MinimumDegreeOfTree = minimumDegreeOfTree; } - public bool IsConsist(string key) + private Node FindNood(string key) { var node = root; while (!node.Leaf) @@ -49,7 +49,7 @@ public bool IsConsist(string key) { if (key == node.Keys[i].key) { - return true; + return node; } if (CompareKey(key, node.Keys[i].key)) { @@ -63,6 +63,23 @@ public bool IsConsist(string key) } } for (int i = 0; i < node.CountKeys; ++i) + { + if (key == node.Keys[i].key) + { + return node; + } + } + return null; + } + + public bool IsConsist(string key) + { + var node = FindNood(key); + if (node == null) + { + return false; + } + for (int i = 0; i < node.CountKeys; ++i) { if (key == node.Keys[i].key) { @@ -71,29 +88,13 @@ public bool IsConsist(string key) } return false; } - public string GetValue(string key) { - var node = root; - while (!node.Leaf) + var node = FindNood(key); + if (node == null) { - for (int i = 0; i < node.CountKeys; ++i) - { - if (key == node.Keys[i].key) - { - return node.Keys[i].value; - } - if (CompareKey(key, node.Keys[i].key)) - { - node = node.Sons[i]; - break; - } - if (i == node.CountKeys - 1) - { - node = node.Sons[i + 1]; - } - } + return null; } for (int i = 0; i < node.CountKeys; ++i) { @@ -105,6 +106,24 @@ public string GetValue(string key) return null; } + public bool ChangeValueByKey(string key, string value) + { + var node = FindNood(key); + if (node == null) + { + return false; + } + for (int i = 0; i < node.CountKeys; ++i) + { + if (key == node.Keys[i].key) + { + node.Keys[i].value = value; + return true; + } + } + return false; + } + public bool CompareKey(string key, string keyInNode) { if (key.Length < keyInNode.Length) @@ -140,68 +159,133 @@ public void Insert(string key, string value) { InsertInNode(key, value, runner); } + } - void Split(int index, Node node) + void Split(int index, Node node) + { + var helpNode = node.Sons[index]; + var newNode = new Node(MinimumDegreeOfTree, helpNode.Leaf); + newNode.CountKeys = MinimumDegreeOfTree - 1; + Array.Copy(helpNode.Keys, MinimumDegreeOfTree, newNode.Keys, 0, MinimumDegreeOfTree - 1); + if (!node.Leaf) + { + Array.Copy(helpNode.Sons, MinimumDegreeOfTree, newNode.Sons, 0, MinimumDegreeOfTree); + } + helpNode.CountKeys = MinimumDegreeOfTree - 1; + for (int i = node.CountKeys; i >= index + 1; i--) + { + node.Sons[i + 1] = node.Sons[i]; + } + node.Sons[index + 1] = newNode; + for (int i = node.CountKeys - 1; i >= index; i--) { - var helpNode = node.Sons[index]; - var newNode = new Node(MinimumDegreeOfTree, helpNode.Leaf); - newNode.CountKeys = MinimumDegreeOfTree - 1; - Array.Copy(helpNode.Keys, MinimumDegreeOfTree, newNode.Keys, 0, MinimumDegreeOfTree - 1); - if (!node.Leaf) + node.Keys[i + 1].key = node.Keys[i].key; + node.Keys[i + 1].value = node.Keys[i].value; + } + node.Keys[index].key = helpNode.Keys[MinimumDegreeOfTree - 1].key; + node.Keys[index].value = helpNode.Keys[MinimumDegreeOfTree - 1].value; + node.CountKeys++; + } + + void InsertInNode(string key, string value, Node node) + { + int size = node.CountKeys; // + int index = size - 1; + if (node.Leaf) + { + while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение { - Array.Copy(helpNode.Sons, MinimumDegreeOfTree, newNode.Sons, 0, MinimumDegreeOfTree); + node.Keys[index + 1].key = node.Keys[index].key; + node.Keys[index + 1].value = node.Keys[index].value; + index--; } - helpNode.CountKeys = MinimumDegreeOfTree - 1; - for (int i = node.CountKeys; i >= index + 1; i--) + node.Keys[index + 1].key = key; + node.Keys[index + 1].value = value; + node.CountKeys++; + } + else + { + while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение { - node.Sons[i + 1] = node.Sons[i]; + index--; } - node.Sons[index + 1] = newNode; - for (int i = node.CountKeys - 1; i >= index; i--) + index++; + if (node.Sons[index].CountKeys == (2 * MinimumDegreeOfTree - 1)) { - node.Keys[i + 1].key = node.Keys[i].key; - node.Keys[i + 1].value = node.Keys[i].value; + Split(index, node); + if (!CompareKey(key, node.Keys[index].key)) + { + index++; + } } - node.Keys[index].key = helpNode.Keys[MinimumDegreeOfTree - 1].key; - node.Keys[index].value = helpNode.Keys[MinimumDegreeOfTree - 1].value; - node.CountKeys++; + InsertInNode(key, value, node.Sons[index]); + } + } + + private int FindKeyIndex(string key) + { + var index = 0; + while (index < runner.CountKeys && CompareKey(key, runner.Keys[index].key)) /*runner.Keys[index].key < key */ + { + index++; } + return index; + } - void InsertInNode(string key, string value, Node node) + private void Remove(string key, Node node) + { + runner = node; + var index = FindKeyIndex(key); + if (index < node.CountKeys && node.Keys[index].key == key) { - int size = node.CountKeys; // - int index = size - 1; if (node.Leaf) { - while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение - { - node.Keys[index + 1].key = node.Keys[index].key; - node.Keys[index + 1].value = node.Keys[index].value; - index--; - } - node.Keys[index + 1].key = key; - node.Keys[index + 1].value = value; - node.CountKeys++; + DeleteFromLeaf(); } else { - while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение - { - index--; - } - index++; - if (node.Sons[index].CountKeys == (2 * MinimumDegreeOfTree - 1)) - { - Split(index, node); - if (!CompareKey(key, node.Keys[index].key)) - { - index++; - } - } - InsertInNode(key, value, node.Sons[index]); - } + DeleteFromNotLeaf(); + } } + else + { + if (node.Leaf) + { + // + return; + } + if (index == node.CountKeys) + { + var check = true; + } + else + { + var check = false; + } + + } + } + + public void Delete(string key) + { + runner = root; + if (root == null) + { + return; + } + Remove(key, root); + if (root.CountKeys == 0) + { + if (root.Leaf) + { + root = null; + } + else + { + root = root.Sons[0]; + } + } } } diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index 7f71069..e039066 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -7,61 +7,70 @@ class Program static void Main(string[] args) { var tree = new BTree(2); - Console.WriteLine("Меню словаря"); - Console.WriteLine("0 - добавить слово в словарь."); - Console.WriteLine("1 - вернуть слово по ключу."); - Console.WriteLine("2 - проверка на наличия ключа в словаре."); - Console.WriteLine("3 - изменение слова по ключу."); - Console.WriteLine("4 - удаление слова по ключу."); - while (true) - { - Console.WriteLine("Ваш выбор."); - var str = Console.ReadLine(); - if (!int.TryParse(str, out int choice)) - { - Console.WriteLine("Ошибка ввода!"); - Console.WriteLine("Попробуйте еще раз!"); - continue; - } - var key = ""; - var value = ""; - switch (choice) - { - case 0: - Console.WriteLine("Введите ключ."); - key = Console.ReadLine(); - Console.WriteLine("Введите слово."); - value = Console.ReadLine(); - tree.Insert(key, value); - break; - case 1: - Console.WriteLine("Введите ключ."); - key = Console.ReadLine(); - str = tree.GetValue(key); - if (str != null) - { - Console.WriteLine("Ваше слово -", str); - } - else - { - Console.WriteLine("Слова с таким ключом нет."); - } - break; - case 2: + tree.Insert("1", "a"); + tree.Insert("2", "b"); + tree.Insert("3", "c"); + tree.Insert("4", "d"); + tree.Insert("5", "e"); + tree.Insert("6", "f"); + tree.Insert("7", "g"); + tree.Insert("8", "h"); + /* var tree = new BTree(2); + Console.WriteLine("Меню словаря"); + Console.WriteLine("0 - добавить слово в словарь."); + Console.WriteLine("1 - вернуть слово по ключу."); + Console.WriteLine("2 - проверка на наличия ключа в словаре."); + Console.WriteLine("3 - изменение слова по ключу."); + Console.WriteLine("4 - удаление слова по ключу."); + while (true) + { + Console.WriteLine("Ваш выбор."); + var str = Console.ReadLine(); + if (!int.TryParse(str, out int choice)) + { + Console.WriteLine("Ошибка ввода!"); + Console.WriteLine("Попробуйте еще раз!"); + continue; + } + var key = ""; + var value = ""; + switch (choice) + { + case 0: + Console.WriteLine("Введите ключ."); + key = Console.ReadLine(); + Console.WriteLine("Введите слово."); + value = Console.ReadLine(); + tree.Insert(key, value); + break; + case 1: + Console.WriteLine("Введите ключ."); + key = Console.ReadLine(); + str = tree.GetValue(key); + if (str != null) + { + Console.WriteLine("Ваше слово -", str); + } + else + { + Console.WriteLine("Слова с таким ключом нет."); + } + break; + case 2: - break; - case 3: + break; + case 3: - break; - case 4: + break; + case 4: - break; - default: - Console.WriteLine("Ошибка ввода!"); - Console.WriteLine("Попробуйте еще раз!"); - break; - } - } + break; + default: + Console.WriteLine("Ошибка ввода!"); + Console.WriteLine("Попробуйте еще раз!"); + break; + } + }*/ } } } From c9ab1e9ea2a80060a141faaee518a7115f2e17fb Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sat, 27 Mar 2021 01:08:55 +0300 Subject: [PATCH 05/12] add Delete, tests not finished --- hw3B-tree/hw3B-tree.Test/TestB-tree.cs | 8 + hw3B-tree/hw3B-tree/BTree.cs | 211 +++++++++++++++++++++++-- hw3B-tree/hw3B-tree/Program.cs | 113 ++++++------- 3 files changed, 255 insertions(+), 77 deletions(-) diff --git a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs index 40fd216..7ca9a93 100644 --- a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs +++ b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs @@ -42,5 +42,13 @@ public void CheckGetValue2() { Assert.IsTrue(tree.GetValue("8") == "h"); } + + [TestCase] + public void CheckChangeValueByKey() + { + tree.ChangeValueByKey("5", "ololo"); + Assert.IsTrue(tree.GetValue("5") == "ololo"); + } + } } \ No newline at end of file diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs index 669baab..0d2ab24 100644 --- a/hw3B-tree/hw3B-tree/BTree.cs +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -124,7 +124,7 @@ public bool ChangeValueByKey(string key, string value) return false; } - public bool CompareKey(string key, string keyInNode) + private bool CompareKey(string key, string keyInNode) { if (key.Length < keyInNode.Length) { @@ -161,7 +161,7 @@ public void Insert(string key, string value) } } - void Split(int index, Node node) + private void Split(int index, Node node) { var helpNode = node.Sons[index]; var newNode = new Node(MinimumDegreeOfTree, helpNode.Leaf); @@ -187,7 +187,7 @@ void Split(int index, Node node) node.CountKeys++; } - void InsertInNode(string key, string value, Node node) + private void InsertInNode(string key, string value, Node node) { int size = node.CountKeys; // int index = size - 1; @@ -222,6 +222,27 @@ void InsertInNode(string key, string value, Node node) } } + public void Delete(string key) + { + runner = root; + if (root == null) + { + return; + } + Remove(key, root); + if (root.CountKeys == 0) + { + if (root.Leaf) + { + root = null; + } + else + { + root = root.Sons[0]; + } + } + } + private int FindKeyIndex(string key) { var index = 0; @@ -240,11 +261,11 @@ private void Remove(string key, Node node) { if (node.Leaf) { - DeleteFromLeaf(); + DeleteFromLeaf(index, node); } else { - DeleteFromNotLeaf(); + DeleteFromNotLeaf(index, node); } } else @@ -254,39 +275,197 @@ private void Remove(string key, Node node) // return; } + bool check; if (index == node.CountKeys) { - var check = true; + check = true; } else { - var check = false; + check = false; + } + var helpNode = node.Sons[index]; + if (helpNode.CountKeys < MinimumDegreeOfTree) + { + runner = node; + Fill(index); + } + if (check && index < node.CountKeys) + { + Remove(key, node.Sons[index - 1]); } + else + { + Remove(key, node.Sons[index]); + } + } + } + private void DeleteFromLeaf(int index, Node node) + { + for ( int i = index + 1; i < node.CountKeys; ++i) + { + node.Keys[i - 1].key = node.Keys[i].key; + node.Keys[i - 1].value = node.Keys[i].value; + } + node.CountKeys--; + } + private void DeleteFromNotLeaf(int index, Node node) + { + string key = node.Keys[index].key; + if (node.Sons[index].CountKeys >= MinimumDegreeOfTree) + { + runner = node; + (string previousKey, string value) = GetPrev(index); + node.Keys[index].key = previousKey; + node.Keys[index].value = value; + Remove(previousKey, node.Sons[index]); + } + else if (node.Sons[index + 1].CountKeys >= MinimumDegreeOfTree) + { + runner = node; + (string succKey, string value) = GetSucc(index); + node.Keys[index].key = succKey; + node.Keys[index].value = value; + Remove(succKey, node.Sons[index + 1]); + } + else + { + runner = node; + Merge(index); + Remove(key, node.Sons[index]); } } - public void Delete(string key) + private (string, string) GetPrev(int index) { - runner = root; - if (root == null) + var helpNode = runner.Sons[index]; + while (!helpNode.Leaf) { - return; + helpNode = helpNode.Sons[helpNode.CountKeys]; } - Remove(key, root); - if (root.CountKeys == 0) + return (helpNode.Keys[helpNode.CountKeys - 1].key, helpNode.Keys[helpNode.CountKeys - 1].value); + } + + private (string, string) GetSucc(int index) + { + var helpNode = runner.Sons[index + 1]; + while (!helpNode.Leaf) { - if (root.Leaf) + helpNode = helpNode.Sons[0]; + } + return (helpNode.Keys[0].key, helpNode.Keys[0].value); + } + + private void Fill(int index) + { + if (index != 0 && runner.Sons[index - 1].CountKeys >= MinimumDegreeOfTree) + { + TakeKeyInPrev(index); + } + else if (index != runner.CountKeys && runner.Sons[index + 1].CountKeys == MinimumDegreeOfTree) + { + TakeKeyInNext(index); + } + else + { + if (index != runner.CountKeys) { - root = null; + Merge(index); } else { - root = root.Sons[0]; + Merge(index - 1); + } + } + } + + private void TakeKeyInPrev(int index) + { + var child = runner.Sons[index]; + var siblings = runner.Sons[index - 1]; + for (int i =child.CountKeys - 1; i >= 0; --i) + { + child.Keys[i + 1].key = child.Keys[i].key; + child.Keys[i + 1].value = child.Keys[i].value; + } + if (!child.Leaf) + { + for (int i = child.CountKeys; i < 0; i++) + { + child.Sons[i + 1] = child.Sons[i]; + } + } + child.Keys[0].key = runner.Keys[index - 1].key; + child.Keys[0].value = runner.Keys[index - 1].value; + if (!child.Leaf) + { + child.Sons[0] = siblings.Sons[siblings.CountKeys]; + } + runner.Keys[index - 1].key = siblings.Keys[siblings.CountKeys - 1].key; + runner.Keys[index - 1].value = siblings.Keys[siblings.CountKeys - 1].value; + child.CountKeys++; + siblings.CountKeys--; + } + + private void TakeKeyInNext(int index) + { + var child = runner.Sons[index]; + var siblings = runner.Sons[index + 1]; + child.Keys[child.CountKeys].key = runner.Keys[index].key; + child.Keys[child.CountKeys].value = runner.Keys[index].value; + if (!child.Leaf) + { + child.Sons[child.CountKeys + 1] = siblings.Sons[0]; + } + runner.Keys[index].key = siblings.Keys[0].key; + runner.Keys[index].value = siblings.Keys[0].value; + for (int i = 1; i < siblings.CountKeys; ++i) + { + siblings.Keys[i - 1].key = siblings.Keys[i].key; + siblings.Keys[i - 1].value = siblings.Keys[i].value; + } + if (!siblings.Leaf) + { + for (int i = 1; i < siblings.CountKeys; ++i) + { + siblings.Sons[i - 1] = siblings.Sons[i]; } } + child.CountKeys++; + siblings.CountKeys--; } + private void Merge(int index) + { + var child = runner.Sons[index]; + var siblings = runner.Sons[index + 1]; + child.Keys[MinimumDegreeOfTree - 1].key = runner.Keys[index].key; + child.Keys[MinimumDegreeOfTree - 1].value = runner.Keys[index].value; + for (int i = 0; i < siblings.CountKeys; i++) + { + child.Keys[i + MinimumDegreeOfTree].key = siblings.Keys[i].key; + child.Keys[i + MinimumDegreeOfTree].value = siblings.Keys[i].value; + } + if (!child.Leaf) + { + for (int i = 0; i <= siblings.CountKeys; i++) + { + child.Sons[i + MinimumDegreeOfTree] = siblings.Sons[i]; + } + } + for (int i = index + 1; i < runner.CountKeys; i++) + { + runner.Keys[i - 1].key = runner.Keys[i].key; + runner.Keys[i - 1].value = runner.Keys[i].value; + } + for (int i = index + 2; i <= runner.CountKeys; i++) + { + runner.Sons[i - 1] = runner.Sons[i]; + } + child.CountKeys += siblings.CountKeys + 1; + runner.CountKeys--; + } } } \ No newline at end of file diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index e039066..c8d02a9 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -7,70 +7,61 @@ class Program static void Main(string[] args) { var tree = new BTree(2); - tree.Insert("1", "a"); - tree.Insert("2", "b"); - tree.Insert("3", "c"); - tree.Insert("4", "d"); - tree.Insert("5", "e"); - tree.Insert("6", "f"); - tree.Insert("7", "g"); - tree.Insert("8", "h"); - /* var tree = new BTree(2); - Console.WriteLine("Меню словаря"); - Console.WriteLine("0 - добавить слово в словарь."); - Console.WriteLine("1 - вернуть слово по ключу."); - Console.WriteLine("2 - проверка на наличия ключа в словаре."); - Console.WriteLine("3 - изменение слова по ключу."); - Console.WriteLine("4 - удаление слова по ключу."); - while (true) - { - Console.WriteLine("Ваш выбор."); - var str = Console.ReadLine(); - if (!int.TryParse(str, out int choice)) - { - Console.WriteLine("Ошибка ввода!"); - Console.WriteLine("Попробуйте еще раз!"); - continue; - } - var key = ""; - var value = ""; - switch (choice) - { - case 0: - Console.WriteLine("Введите ключ."); - key = Console.ReadLine(); - Console.WriteLine("Введите слово."); - value = Console.ReadLine(); - tree.Insert(key, value); - break; - case 1: - Console.WriteLine("Введите ключ."); - key = Console.ReadLine(); - str = tree.GetValue(key); - if (str != null) - { - Console.WriteLine("Ваше слово -", str); - } - else - { - Console.WriteLine("Слова с таким ключом нет."); - } - break; - case 2: + Console.WriteLine("Меню словаря"); + Console.WriteLine("0 - добавить слово в словарь."); + Console.WriteLine("1 - вернуть слово по ключу."); + Console.WriteLine("2 - проверка на наличия ключа в словаре."); + Console.WriteLine("3 - изменение слова по ключу."); + Console.WriteLine("4 - удаление слова по ключу."); + while (true) + { + Console.WriteLine("Ваш выбор."); + var str = Console.ReadLine(); + if (!int.TryParse(str, out int choice)) + { + Console.WriteLine("Ошибка ввода!"); + Console.WriteLine("Попробуйте еще раз!"); + continue; + } + var key = ""; + var value = ""; + switch (choice) + { + case 0: + Console.WriteLine("Введите ключ."); + key = Console.ReadLine(); + Console.WriteLine("Введите слово."); + value = Console.ReadLine(); + tree.Insert(key, value); + break; + case 1: + Console.WriteLine("Введите ключ."); + key = Console.ReadLine(); + str = tree.GetValue(key); + if (str != null) + { + Console.WriteLine("Ваше слово -", str); + } + else + { + Console.WriteLine("Слова с таким ключом нет."); + } + break; + case 2: - break; - case 3: + break; + case 3: - break; - case 4: + break; + case 4: - break; - default: - Console.WriteLine("Ошибка ввода!"); - Console.WriteLine("Попробуйте еще раз!"); - break; - } - }*/ + break; + default: + Console.WriteLine("Ошибка ввода!"); + Console.WriteLine("Попробуйте еще раз!"); + break; + } + } } } } From 7df6e2af365b2517257246228b71ccfb3e1fbbac Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Sun, 28 Mar 2021 01:54:39 +0300 Subject: [PATCH 06/12] fix Delete. Add and fix test --- hw3B-tree/hw3B-tree.Test/TestB-tree.cs | 70 ++++++++++------ hw3B-tree/hw3B-tree/BTree.cs | 109 ++++++++++++++----------- hw3B-tree/hw3B-tree/Program.cs | 73 ++++------------- 3 files changed, 124 insertions(+), 128 deletions(-) diff --git a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs index 7ca9a93..a12c201 100644 --- a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs +++ b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs @@ -4,43 +4,48 @@ namespace hw3B_tree.Test { public class Tests { - private BTree tree = new BTree(2); + private BTree tree; [SetUp] public void Setup() { - tree.Insert("1", "a"); - tree.Insert("2", "b"); - tree.Insert("3", "c"); - tree.Insert("4", "d"); - tree.Insert("5", "e"); - tree.Insert("6", "f"); - tree.Insert("7", "g"); - tree.Insert("8", "h"); + tree = new BTree(2); + tree.Insert("1", "1"); + tree.Insert("2", "2"); + tree.Insert("3", "3"); + tree.Insert("4", "4"); + tree.Insert("5", "5"); + tree.Insert("6", "6"); + tree.Insert("7", "7"); + tree.Insert("8", "8"); + tree.Insert("9", "9"); + tree.Insert("10", "10"); + tree.Insert("11", "11"); + tree.Insert("12", "12"); + tree.Insert("13", "13"); + tree.Insert("14", "14"); + tree.Insert("15", "15"); + tree.Insert("16", "16"); + tree.Insert("17", "17"); + tree.Insert("18", "18"); } [TestCase] - public void CheckInsert1() + public void CheckInsert() { - Assert.IsTrue(tree.IsConsist("6")); + for (int i = 1; i <= 18; ++i) + { + Assert.IsTrue(tree.IsConsist(i.ToString())); + } } [TestCase] - public void CheckInsert2() + public void CheckGetValue() { - Assert.IsTrue(tree.IsConsist("8")); - } - - [TestCase] - public void CheckGetValue1() - { - Assert.IsTrue(tree.GetValue("5") == "e"); - } - - [TestCase] - public void CheckGetValue2() - { - Assert.IsTrue(tree.GetValue("8") == "h"); + for (int i = 1; i <= 18; ++i) + { + Assert.IsTrue(tree.GetValue(i.ToString()) == i.ToString()); + } } [TestCase] @@ -49,6 +54,21 @@ public void CheckChangeValueByKey() tree.ChangeValueByKey("5", "ololo"); Assert.IsTrue(tree.GetValue("5") == "ololo"); } + + [TestCase] + public void CheckDelete() + { + tree.Delete("8"); + for (int i = 1; i <= 18; ++i) + { + if (i == 8) + { + Assert.IsFalse(tree.IsConsist(i.ToString())); + continue; + } + Assert.IsTrue(tree.IsConsist(i.ToString())); + } + } } } \ No newline at end of file diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs index 0d2ab24..adb4ebb 100644 --- a/hw3B-tree/hw3B-tree/BTree.cs +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -4,6 +4,9 @@ namespace hw3B_tree { + /// + /// btree class + /// public class BTree { public int MinimumDegreeOfTree { get; set; } @@ -59,6 +62,7 @@ private Node FindNood(string key) if (i == node.CountKeys - 1) { node = node.Sons[i + 1]; + break; } } } @@ -72,6 +76,10 @@ private Node FindNood(string key) return null; } + /// + /// checking for the presence of a key in a tree + /// + /// true - if key is in tree, false - if key isn't in tree public bool IsConsist(string key) { var node = FindNood(key); @@ -89,6 +97,10 @@ public bool IsConsist(string key) return false; } + /// + /// get value by key + /// + /// return value, if we find key and return null,if we don't find key public string GetValue(string key) { var node = FindNood(key); @@ -106,6 +118,10 @@ public string GetValue(string key) return null; } + /// + /// chenge value in tree by key + /// + /// return true,if we change value, and false, if we don't change public bool ChangeValueByKey(string key, string value) { var node = FindNood(key); @@ -124,7 +140,7 @@ public bool ChangeValueByKey(string key, string value) return false; } - private bool CompareKey(string key, string keyInNode) + public bool CompareKey(string key, string keyInNode) { if (key.Length < keyInNode.Length) { @@ -137,6 +153,9 @@ private bool CompareKey(string key, string keyInNode) return String.Compare(key, keyInNode) < 0 ? true : false; } + /// + /// function of insert (key,value) in tree + /// public void Insert(string key, string value) { var runner = root; @@ -189,11 +208,10 @@ private void Split(int index, Node node) private void InsertInNode(string key, string value, Node node) { - int size = node.CountKeys; // - int index = size - 1; + int index = node.CountKeys - 1; if (node.Leaf) { - while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение + while (index >= 0 && CompareKey(key, node.Keys[index].key)) { node.Keys[index + 1].key = node.Keys[index].key; node.Keys[index + 1].value = node.Keys[index].value; @@ -205,7 +223,7 @@ private void InsertInNode(string key, string value, Node node) } else { - while (index >= 0 && CompareKey(key, node.Keys[index].key)) // проверитб сравнение + while (index >= 0 && CompareKey(key, node.Keys[index].key)) { index--; } @@ -222,6 +240,9 @@ private void InsertInNode(string key, string value, Node node) } } + /// + /// finction delete (key, value) from tree + /// public void Delete(string key) { runner = root; @@ -229,7 +250,7 @@ public void Delete(string key) { return; } - Remove(key, root); + Remove(key); if (root.CountKeys == 0) { if (root.Leaf) @@ -246,37 +267,35 @@ public void Delete(string key) private int FindKeyIndex(string key) { var index = 0; - while (index < runner.CountKeys && CompareKey(key, runner.Keys[index].key)) /*runner.Keys[index].key < key */ + while (index < runner.CountKeys && CompareKey(runner.Keys[index].key, key)) { index++; } return index; } - private void Remove(string key, Node node) + private void Remove(string key) { - runner = node; var index = FindKeyIndex(key); - if (index < node.CountKeys && node.Keys[index].key == key) + if (index < runner.CountKeys && runner.Keys[index].key == key) { - if (node.Leaf) + if (runner.Leaf) { - DeleteFromLeaf(index, node); + DeleteFromLeaf(index, runner); } else { - DeleteFromNotLeaf(index, node); + DeleteFromNotLeaf(index, runner); } } else { - if (node.Leaf) + if (runner.Leaf) { - // return; } bool check; - if (index == node.CountKeys) + if (index == runner.CountKeys) { check = true; } @@ -284,19 +303,20 @@ private void Remove(string key, Node node) { check = false; } - var helpNode = node.Sons[index]; + var helpNode = runner.Sons[index]; if (helpNode.CountKeys < MinimumDegreeOfTree) { - runner = node; Fill(index); } - if (check && index < node.CountKeys) + if (check && index < runner.CountKeys) { - Remove(key, node.Sons[index - 1]); + runner = runner.Sons[index - 1]; + Remove(key); } else { - Remove(key, node.Sons[index]); + runner = runner.Sons[index]; + Remove(key); } } } @@ -305,8 +325,7 @@ private void DeleteFromLeaf(int index, Node node) { for ( int i = index + 1; i < node.CountKeys; ++i) { - node.Keys[i - 1].key = node.Keys[i].key; - node.Keys[i - 1].value = node.Keys[i].value; + node.Keys[i - 1] = node.Keys[i]; } node.CountKeys--; } @@ -320,7 +339,8 @@ private void DeleteFromNotLeaf(int index, Node node) (string previousKey, string value) = GetPrev(index); node.Keys[index].key = previousKey; node.Keys[index].value = value; - Remove(previousKey, node.Sons[index]); + runner = runner.Sons[index]; + Remove(previousKey); } else if (node.Sons[index + 1].CountKeys >= MinimumDegreeOfTree) { @@ -328,13 +348,15 @@ private void DeleteFromNotLeaf(int index, Node node) (string succKey, string value) = GetSucc(index); node.Keys[index].key = succKey; node.Keys[index].value = value; - Remove(succKey, node.Sons[index + 1]); + runner = node.Sons[index + 1]; + Remove(succKey); } else { runner = node; Merge(index); - Remove(key, node.Sons[index]); + runner = node.Sons[index]; + Remove(key); } } @@ -364,7 +386,7 @@ private void Fill(int index) { TakeKeyInPrev(index); } - else if (index != runner.CountKeys && runner.Sons[index + 1].CountKeys == MinimumDegreeOfTree) + else if (index != runner.CountKeys && runner.Sons[index + 1].CountKeys >= MinimumDegreeOfTree) { TakeKeyInNext(index); } @@ -385,26 +407,23 @@ private void TakeKeyInPrev(int index) { var child = runner.Sons[index]; var siblings = runner.Sons[index - 1]; - for (int i =child.CountKeys - 1; i >= 0; --i) + for (int i = child.CountKeys - 1; i >= 0; --i) { - child.Keys[i + 1].key = child.Keys[i].key; - child.Keys[i + 1].value = child.Keys[i].value; + child.Keys[i + 1] = child.Keys[i]; } if (!child.Leaf) { - for (int i = child.CountKeys; i < 0; i++) + for (int i = child.CountKeys; i >= 0; i--) { child.Sons[i + 1] = child.Sons[i]; } } - child.Keys[0].key = runner.Keys[index - 1].key; - child.Keys[0].value = runner.Keys[index - 1].value; + child.Keys[0] = runner.Keys[index - 1]; if (!child.Leaf) { child.Sons[0] = siblings.Sons[siblings.CountKeys]; } - runner.Keys[index - 1].key = siblings.Keys[siblings.CountKeys - 1].key; - runner.Keys[index - 1].value = siblings.Keys[siblings.CountKeys - 1].value; + runner.Keys[index - 1] = siblings.Keys[siblings.CountKeys - 1]; child.CountKeys++; siblings.CountKeys--; } @@ -413,22 +432,19 @@ private void TakeKeyInNext(int index) { var child = runner.Sons[index]; var siblings = runner.Sons[index + 1]; - child.Keys[child.CountKeys].key = runner.Keys[index].key; - child.Keys[child.CountKeys].value = runner.Keys[index].value; + child.Keys[child.CountKeys] = runner.Keys[index]; if (!child.Leaf) { child.Sons[child.CountKeys + 1] = siblings.Sons[0]; } - runner.Keys[index].key = siblings.Keys[0].key; - runner.Keys[index].value = siblings.Keys[0].value; + runner.Keys[index] = siblings.Keys[0]; for (int i = 1; i < siblings.CountKeys; ++i) { - siblings.Keys[i - 1].key = siblings.Keys[i].key; - siblings.Keys[i - 1].value = siblings.Keys[i].value; + siblings.Keys[i - 1] = siblings.Keys[i]; } if (!siblings.Leaf) { - for (int i = 1; i < siblings.CountKeys; ++i) + for (int i = 1; i <= siblings.CountKeys; ++i) { siblings.Sons[i - 1] = siblings.Sons[i]; } @@ -441,12 +457,10 @@ private void Merge(int index) { var child = runner.Sons[index]; var siblings = runner.Sons[index + 1]; - child.Keys[MinimumDegreeOfTree - 1].key = runner.Keys[index].key; - child.Keys[MinimumDegreeOfTree - 1].value = runner.Keys[index].value; + child.Keys[MinimumDegreeOfTree - 1] = runner.Keys[index]; for (int i = 0; i < siblings.CountKeys; i++) { - child.Keys[i + MinimumDegreeOfTree].key = siblings.Keys[i].key; - child.Keys[i + MinimumDegreeOfTree].value = siblings.Keys[i].value; + child.Keys[i + MinimumDegreeOfTree] = siblings.Keys[i]; } if (!child.Leaf) { @@ -457,8 +471,7 @@ private void Merge(int index) } for (int i = index + 1; i < runner.CountKeys; i++) { - runner.Keys[i - 1].key = runner.Keys[i].key; - runner.Keys[i - 1].value = runner.Keys[i].value; + runner.Keys[i - 1] = runner.Keys[i]; } for (int i = index + 2; i <= runner.CountKeys; i++) { diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index c8d02a9..15b1894 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -7,61 +7,24 @@ class Program static void Main(string[] args) { var tree = new BTree(2); - Console.WriteLine("Меню словаря"); - Console.WriteLine("0 - добавить слово в словарь."); - Console.WriteLine("1 - вернуть слово по ключу."); - Console.WriteLine("2 - проверка на наличия ключа в словаре."); - Console.WriteLine("3 - изменение слова по ключу."); - Console.WriteLine("4 - удаление слова по ключу."); - while (true) - { - Console.WriteLine("Ваш выбор."); - var str = Console.ReadLine(); - if (!int.TryParse(str, out int choice)) - { - Console.WriteLine("Ошибка ввода!"); - Console.WriteLine("Попробуйте еще раз!"); - continue; - } - var key = ""; - var value = ""; - switch (choice) - { - case 0: - Console.WriteLine("Введите ключ."); - key = Console.ReadLine(); - Console.WriteLine("Введите слово."); - value = Console.ReadLine(); - tree.Insert(key, value); - break; - case 1: - Console.WriteLine("Введите ключ."); - key = Console.ReadLine(); - str = tree.GetValue(key); - if (str != null) - { - Console.WriteLine("Ваше слово -", str); - } - else - { - Console.WriteLine("Слова с таким ключом нет."); - } - break; - case 2: - - break; - case 3: - - break; - case 4: - - break; - default: - Console.WriteLine("Ошибка ввода!"); - Console.WriteLine("Попробуйте еще раз!"); - break; - } - } + tree.Insert("1", "1"); + tree.Insert("2", "2"); + tree.Insert("3", "3"); + tree.Insert("4", "4"); + tree.Insert("5", "5"); + tree.Insert("6", "6"); + tree.Insert("7", "7"); + tree.Insert("8", "8"); + tree.Insert("9", "9"); + tree.Insert("10", "10"); + tree.Insert("11", "11"); + tree.Insert("12", "12"); + tree.Insert("13", "13"); + tree.Insert("14", "14"); + tree.Insert("15", "15"); + tree.Insert("16", "16"); + tree.Insert("17", "17"); + tree.Insert("18", "18"); } } } From 3ba402d672ec86c742a5b065bdb01daef0d6843e Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Wed, 7 Apr 2021 18:14:30 +0300 Subject: [PATCH 07/12] appveyor.yml --- appveyor.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 appveyor.yml diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..e7e7cb3 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,6 @@ +image: Visual Studio 2019 + +build_script: + - For /R %%I in (*.sln) do dotnet test %%I + +test: off \ No newline at end of file From b66134aea24328ac08b544ccd189ed9bde8b01a0 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Fri, 14 May 2021 01:38:44 +0300 Subject: [PATCH 08/12] fix mistakes, tests wait me --- hw3B-tree/hw3B-tree.Test/TestB-tree.cs | 8 +- .../hw3B-tree.Test/hw3B-tree.Test.csproj | 2 +- hw3B-tree/hw3B-tree.sln | 4 +- hw3B-tree/hw3B-tree/BTree.cs | 151 ++++++++---------- hw3B-tree/hw3B-tree/Program.cs | 3 +- 5 files changed, 79 insertions(+), 89 deletions(-) diff --git a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs index a12c201..bba95d5 100644 --- a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs +++ b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs @@ -1,6 +1,6 @@ using NUnit.Framework; -namespace hw3B_tree.Test +namespace Hw3B_tree.Test { public class Tests { @@ -35,7 +35,7 @@ public void CheckInsert() { for (int i = 1; i <= 18; ++i) { - Assert.IsTrue(tree.IsConsist(i.ToString())); + Assert.IsTrue(tree.IsExists(i.ToString())); } } @@ -63,10 +63,10 @@ public void CheckDelete() { if (i == 8) { - Assert.IsFalse(tree.IsConsist(i.ToString())); + Assert.IsFalse(tree.IsExists(i.ToString())); continue; } - Assert.IsTrue(tree.IsConsist(i.ToString())); + Assert.IsTrue(tree.IsExists(i.ToString())); } } diff --git a/hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj b/hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj index 20f5a14..953b990 100644 --- a/hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj +++ b/hw3B-tree/hw3B-tree.Test/hw3B-tree.Test.csproj @@ -14,7 +14,7 @@ - + diff --git a/hw3B-tree/hw3B-tree.sln b/hw3B-tree/hw3B-tree.sln index 5ed2491..6ea80e4 100644 --- a/hw3B-tree/hw3B-tree.sln +++ b/hw3B-tree/hw3B-tree.sln @@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31005.135 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw3B-tree", "hw3B-tree\hw3B-tree.csproj", "{53642C16-3AA3-4A57-8380-46B9C2E9651E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hw3B-tree", "hw3B-tree\Hw3B-tree.csproj", "{53642C16-3AA3-4A57-8380-46B9C2E9651E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw3B-tree.Test", "hw3B-tree.Test\hw3B-tree.Test.csproj", "{A2EE96D2-650B-457E-87E5-A6410F6DF06D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "hw3B-tree.Test", "hw3B-tree.Test\hw3B-tree.Test.csproj", "{A2EE96D2-650B-457E-87E5-A6410F6DF06D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs index adb4ebb..43aa43f 100644 --- a/hw3B-tree/hw3B-tree/BTree.cs +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -2,14 +2,14 @@ using System.Collections.Generic; using System.Text; -namespace hw3B_tree +namespace Hw3B_tree { /// /// btree class /// public class BTree { - public int MinimumDegreeOfTree { get; set; } + public int MinimumDegreeOfTree { get; } private class Node { @@ -32,8 +32,6 @@ public Node(int minimumDegreeOfTree, bool leaf) private Node root; - private Node runner; - public BTree(int minimumDegreeOfTree) { if (minimumDegreeOfTree < 2) @@ -43,7 +41,7 @@ public BTree(int minimumDegreeOfTree) MinimumDegreeOfTree = minimumDegreeOfTree; } - private Node FindNood(string key) + private Node FindNode(string key) { var node = root; while (!node.Leaf) @@ -80,9 +78,9 @@ private Node FindNood(string key) /// checking for the presence of a key in a tree /// /// true - if key is in tree, false - if key isn't in tree - public bool IsConsist(string key) + public bool IsExists(string key) { - var node = FindNood(key); + var node = FindNode(key); if (node == null) { return false; @@ -103,7 +101,7 @@ public bool IsConsist(string key) /// return value, if we find key and return null,if we don't find key public string GetValue(string key) { - var node = FindNood(key); + var node = FindNode(key); if (node == null) { return null; @@ -124,7 +122,7 @@ public string GetValue(string key) /// return true,if we change value, and false, if we don't change public bool ChangeValueByKey(string key, string value) { - var node = FindNood(key); + var node = FindNode(key); if (node == null) { return false; @@ -150,7 +148,7 @@ public bool CompareKey(string key, string keyInNode) { return false; } - return String.Compare(key, keyInNode) < 0 ? true : false; + return String.Compare(key, keyInNode) < 0; } /// @@ -245,12 +243,12 @@ private void InsertInNode(string key, string value, Node node) /// public void Delete(string key) { - runner = root; + var runner = root; if (root == null) { return; } - Remove(key); + Remove(key, ref runner); if (root.CountKeys == 0) { if (root.Leaf) @@ -264,66 +262,58 @@ public void Delete(string key) } } - private int FindKeyIndex(string key) + private int FindKeyIndex(string key, Node node) { var index = 0; - while (index < runner.CountKeys && CompareKey(runner.Keys[index].key, key)) + while (index < node.CountKeys && CompareKey(node.Keys[index].key, key)) { index++; } return index; } - private void Remove(string key) + private void Remove(string key, ref Node cursor) { - var index = FindKeyIndex(key); - if (index < runner.CountKeys && runner.Keys[index].key == key) + var index = FindKeyIndex(key, cursor); + if (index < cursor.CountKeys && cursor.Keys[index].key == key) { - if (runner.Leaf) + if (cursor.Leaf) { - DeleteFromLeaf(index, runner); + DeleteFromLeaf(index, cursor); } else { - DeleteFromNotLeaf(index, runner); + DeleteFromNotLeaf(index, cursor); } } else { - if (runner.Leaf) + if (cursor.Leaf) { return; } - bool check; - if (index == runner.CountKeys) - { - check = true; - } - else - { - check = false; - } - var helpNode = runner.Sons[index]; + bool check = index == cursor.CountKeys; + var helpNode = cursor.Sons[index]; if (helpNode.CountKeys < MinimumDegreeOfTree) { - Fill(index); + Fill(index, ref cursor); } - if (check && index < runner.CountKeys) + if (check && index < cursor.CountKeys) { - runner = runner.Sons[index - 1]; - Remove(key); + cursor = cursor.Sons[index - 1]; + Remove(key, ref cursor); } else { - runner = runner.Sons[index]; - Remove(key); + cursor = cursor.Sons[index]; + Remove(key, ref cursor); } } } private void DeleteFromLeaf(int index, Node node) { - for ( int i = index + 1; i < node.CountKeys; ++i) + for (int i = index + 1; i < node.CountKeys; ++i) { node.Keys[i - 1] = node.Keys[i]; } @@ -333,36 +323,35 @@ private void DeleteFromLeaf(int index, Node node) private void DeleteFromNotLeaf(int index, Node node) { string key = node.Keys[index].key; + var cursor = node; if (node.Sons[index].CountKeys >= MinimumDegreeOfTree) { - runner = node; - (string previousKey, string value) = GetPrev(index); + (string previousKey, string value) = GetPrev(index, cursor); node.Keys[index].key = previousKey; node.Keys[index].value = value; - runner = runner.Sons[index]; - Remove(previousKey); + cursor = cursor.Sons[index]; + Remove(previousKey, ref cursor); } else if (node.Sons[index + 1].CountKeys >= MinimumDegreeOfTree) { - runner = node; - (string succKey, string value) = GetSucc(index); + (string succKey, string value) = GetSucc(index, cursor); node.Keys[index].key = succKey; node.Keys[index].value = value; - runner = node.Sons[index + 1]; - Remove(succKey); + cursor = node.Sons[index + 1]; + Remove(succKey, ref cursor); } else { - runner = node; - Merge(index); - runner = node.Sons[index]; - Remove(key); + cursor = node;//////// + Merge(index, ref cursor); + cursor = node.Sons[index]; + Remove(key, ref cursor); } } - private (string, string) GetPrev(int index) + private (string, string) GetPrev(int index, Node node) { - var helpNode = runner.Sons[index]; + var helpNode = node.Sons[index]; while (!helpNode.Leaf) { helpNode = helpNode.Sons[helpNode.CountKeys]; @@ -370,9 +359,9 @@ private void DeleteFromNotLeaf(int index, Node node) return (helpNode.Keys[helpNode.CountKeys - 1].key, helpNode.Keys[helpNode.CountKeys - 1].value); } - private (string, string) GetSucc(int index) + private (string, string) GetSucc(int index, Node node) { - var helpNode = runner.Sons[index + 1]; + var helpNode = node.Sons[index + 1]; while (!helpNode.Leaf) { helpNode = helpNode.Sons[0]; @@ -380,33 +369,33 @@ private void DeleteFromNotLeaf(int index, Node node) return (helpNode.Keys[0].key, helpNode.Keys[0].value); } - private void Fill(int index) + private void Fill(int index, ref Node cursor) { - if (index != 0 && runner.Sons[index - 1].CountKeys >= MinimumDegreeOfTree) + if (index != 0 && cursor.Sons[index - 1].CountKeys >= MinimumDegreeOfTree) { - TakeKeyInPrev(index); + TakeKeyInPrev(index, ref cursor); } - else if (index != runner.CountKeys && runner.Sons[index + 1].CountKeys >= MinimumDegreeOfTree) + else if (index != cursor.CountKeys && cursor.Sons[index + 1].CountKeys >= MinimumDegreeOfTree) { - TakeKeyInNext(index); + TakeKeyInNext(index, ref cursor); } else { - if (index != runner.CountKeys) + if (index != cursor.CountKeys) { - Merge(index); + Merge(index, ref cursor); } else { - Merge(index - 1); + Merge(index - 1, ref cursor); } } } - private void TakeKeyInPrev(int index) + private void TakeKeyInPrev(int index, ref Node cursor) { - var child = runner.Sons[index]; - var siblings = runner.Sons[index - 1]; + var child = cursor.Sons[index]; + var siblings = cursor.Sons[index - 1]; for (int i = child.CountKeys - 1; i >= 0; --i) { child.Keys[i + 1] = child.Keys[i]; @@ -418,26 +407,26 @@ private void TakeKeyInPrev(int index) child.Sons[i + 1] = child.Sons[i]; } } - child.Keys[0] = runner.Keys[index - 1]; + child.Keys[0] = cursor.Keys[index - 1]; if (!child.Leaf) { child.Sons[0] = siblings.Sons[siblings.CountKeys]; } - runner.Keys[index - 1] = siblings.Keys[siblings.CountKeys - 1]; + cursor.Keys[index - 1] = siblings.Keys[siblings.CountKeys - 1]; child.CountKeys++; siblings.CountKeys--; } - private void TakeKeyInNext(int index) + private void TakeKeyInNext(int index, ref Node cursor) { - var child = runner.Sons[index]; - var siblings = runner.Sons[index + 1]; - child.Keys[child.CountKeys] = runner.Keys[index]; + var child = cursor.Sons[index]; + var siblings = cursor.Sons[index + 1]; + child.Keys[child.CountKeys] = cursor.Keys[index]; if (!child.Leaf) { child.Sons[child.CountKeys + 1] = siblings.Sons[0]; } - runner.Keys[index] = siblings.Keys[0]; + cursor.Keys[index] = siblings.Keys[0]; for (int i = 1; i < siblings.CountKeys; ++i) { siblings.Keys[i - 1] = siblings.Keys[i]; @@ -453,11 +442,11 @@ private void TakeKeyInNext(int index) siblings.CountKeys--; } - private void Merge(int index) + private void Merge(int index, ref Node cursor) { - var child = runner.Sons[index]; - var siblings = runner.Sons[index + 1]; - child.Keys[MinimumDegreeOfTree - 1] = runner.Keys[index]; + var child = cursor.Sons[index]; + var siblings = cursor.Sons[index + 1]; + child.Keys[MinimumDegreeOfTree - 1] = cursor.Keys[index]; for (int i = 0; i < siblings.CountKeys; i++) { child.Keys[i + MinimumDegreeOfTree] = siblings.Keys[i]; @@ -469,16 +458,16 @@ private void Merge(int index) child.Sons[i + MinimumDegreeOfTree] = siblings.Sons[i]; } } - for (int i = index + 1; i < runner.CountKeys; i++) + for (int i = index + 1; i < cursor.CountKeys; i++) { - runner.Keys[i - 1] = runner.Keys[i]; + cursor.Keys[i - 1] = cursor.Keys[i]; } - for (int i = index + 2; i <= runner.CountKeys; i++) + for (int i = index + 2; i <= cursor.CountKeys; i++) { - runner.Sons[i - 1] = runner.Sons[i]; + cursor.Sons[i - 1] = cursor.Sons[i]; } child.CountKeys += siblings.CountKeys + 1; - runner.CountKeys--; + cursor.CountKeys--; } } } \ No newline at end of file diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index 15b1894..abbca57 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -1,6 +1,6 @@ using System; -namespace hw3B_tree +namespace Hw3B_tree { class Program { @@ -25,6 +25,7 @@ static void Main(string[] args) tree.Insert("16", "16"); tree.Insert("17", "17"); tree.Insert("18", "18"); + tree.Delete("8"); } } } From 1b78027eea586f83c54f64b413a4ef012e2f82d0 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Sun, 16 May 2021 14:48:23 +0300 Subject: [PATCH 09/12] add tests --- hw3B-tree/hw3B-tree.Test/TestB-tree.cs | 158 +++++++++++++++++++++---- hw3B-tree/hw3B-tree/Program.cs | 25 +++- 2 files changed, 160 insertions(+), 23 deletions(-) diff --git a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs index bba95d5..d74ee2a 100644 --- a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs +++ b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs @@ -6,33 +6,21 @@ public class Tests { private BTree tree; - [SetUp] - public void Setup() + public BTree Setup(int MinimumDegreeOfTree) { - tree = new BTree(2); - tree.Insert("1", "1"); - tree.Insert("2", "2"); - tree.Insert("3", "3"); - tree.Insert("4", "4"); - tree.Insert("5", "5"); - tree.Insert("6", "6"); - tree.Insert("7", "7"); - tree.Insert("8", "8"); - tree.Insert("9", "9"); - tree.Insert("10", "10"); - tree.Insert("11", "11"); - tree.Insert("12", "12"); - tree.Insert("13", "13"); - tree.Insert("14", "14"); - tree.Insert("15", "15"); - tree.Insert("16", "16"); - tree.Insert("17", "17"); - tree.Insert("18", "18"); + tree = new BTree(MinimumDegreeOfTree); + for (int i = 1; i < 19; ++i) + { + tree.Insert(i.ToString(), i.ToString()); + + } + return tree; } [TestCase] public void CheckInsert() { + var tree = Setup(2); for (int i = 1; i <= 18; ++i) { Assert.IsTrue(tree.IsExists(i.ToString())); @@ -42,6 +30,7 @@ public void CheckInsert() [TestCase] public void CheckGetValue() { + var tree = Setup(2); for (int i = 1; i <= 18; ++i) { Assert.IsTrue(tree.GetValue(i.ToString()) == i.ToString()); @@ -51,13 +40,107 @@ public void CheckGetValue() [TestCase] public void CheckChangeValueByKey() { + var tree = Setup(2); tree.ChangeValueByKey("5", "ololo"); Assert.IsTrue(tree.GetValue("5") == "ololo"); } [TestCase] - public void CheckDelete() + public void CheckDeleteFromRoot() + { + var tree = Setup(2); + tree.Delete("8"); + for (int i = 1; i <= 18; ++i) + { + if (i == 8) + { + Assert.IsFalse(tree.IsExists(i.ToString())); + continue; + } + Assert.IsTrue(tree.IsExists(i.ToString())); + } + } + + [TestCase] + public void CheckDeleteFromLeaf() + { + var tree = Setup(2); + tree.Delete("5"); + for (int i = 1; i <= 18; ++i) + { + if (i == 5) + { + Assert.IsFalse(tree.IsExists(i.ToString())); + continue; + } + Assert.IsTrue(tree.IsExists(i.ToString())); + } + } + + [TestCase] + public void CheckDeleteFromNotLeafWithChangeRoot() + { + var tree = Setup(2); + tree.Delete("6"); + for (int i = 1; i <= 18; ++i) + { + if (i == 6) + { + Assert.IsFalse(tree.IsExists(i.ToString())); + continue; + } + Assert.IsTrue(tree.IsExists(i.ToString())); + } + } + + [TestCase] + public void CheckDeleteFromLeafWithChangeValueInLeafs() + { + var tree = Setup(2); + tree.Delete("15"); + for (int i = 1; i <= 18; ++i) + { + if (i == 15) + { + Assert.IsFalse(tree.IsExists(i.ToString())); + continue; + } + Assert.IsTrue(tree.IsExists(i.ToString())); + } + } + + [TestCase] + public void CheckInsertWithAnotherDegree() { + var tree = Setup(4); + for (int i = 1; i <= 18; ++i) + { + Assert.IsTrue(tree.IsExists(i.ToString())); + } + } + + [TestCase] + public void CheckGetValueWithAnotherDegree() + { + var tree = Setup(4); + for (int i = 1; i <= 18; ++i) + { + Assert.IsTrue(tree.GetValue(i.ToString()) == i.ToString()); + } + } + + [TestCase] + public void CheckChangeValueByKeyWithAnotherDegree() + { + var tree = Setup(4); + tree.ChangeValueByKey("5", "ololo"); + Assert.IsTrue(tree.GetValue("5") == "ololo"); + } + + [TestCase] + public void CheckDeleteFromRootWithAnotherDegree() + { + var tree = Setup(4); tree.Delete("8"); for (int i = 1; i <= 18; ++i) { @@ -70,5 +153,36 @@ public void CheckDelete() } } + [TestCase] + public void CheckDeleteFromLeafWithMergeLefsWithAnotherDegree() + { + var tree = Setup(4); + tree.Delete("2"); + for (int i = 1; i <= 18; ++i) + { + if (i == 2) + { + Assert.IsFalse(tree.IsExists(i.ToString())); + continue; + } + Assert.IsTrue(tree.IsExists(i.ToString())); + } + } + + [TestCase] + public void CheckDeleteFromLeafWithChangeValueInLeafsWithAnotherDegree() + { + var tree = Setup(4); + tree.Delete("10"); + for (int i = 1; i <= 18; ++i) + { + if (i == 10) + { + Assert.IsFalse(tree.IsExists(i.ToString())); + continue; + } + Assert.IsTrue(tree.IsExists(i.ToString())); + } + } } } \ No newline at end of file diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index abbca57..1c9d00f 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -25,7 +25,30 @@ static void Main(string[] args) tree.Insert("16", "16"); tree.Insert("17", "17"); tree.Insert("18", "18"); - tree.Delete("8"); + tree.Delete("9"); + for (int i = 1; i <= 18; ++i) + { + if (i == 9) + { + if (!tree.IsExists(i.ToString())) + { + Console.WriteLine("true"); + } + else + { + Console.WriteLine("false"); + } + continue; + } + if (tree.IsExists(i.ToString())) + { + Console.WriteLine("true"); + } + else + { + Console.WriteLine("false"); + } + } } } } From f143f5a245d705a5a4007f16eec6e38d4d369c41 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Sun, 16 May 2021 14:49:42 +0300 Subject: [PATCH 10/12] add tests --- hw3B-tree/hw3B-tree/Program.cs | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index 1c9d00f..c2ac249 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -26,29 +26,6 @@ static void Main(string[] args) tree.Insert("17", "17"); tree.Insert("18", "18"); tree.Delete("9"); - for (int i = 1; i <= 18; ++i) - { - if (i == 9) - { - if (!tree.IsExists(i.ToString())) - { - Console.WriteLine("true"); - } - else - { - Console.WriteLine("false"); - } - continue; - } - if (tree.IsExists(i.ToString())) - { - Console.WriteLine("true"); - } - else - { - Console.WriteLine("false"); - } - } } } -} +} \ No newline at end of file From 3ca4589305de5f96d17ebd918644ab092dd78331 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Sun, 16 May 2021 14:50:39 +0300 Subject: [PATCH 11/12] clear --- hw3B-tree/hw3B-tree/Program.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/hw3B-tree/hw3B-tree/Program.cs b/hw3B-tree/hw3B-tree/Program.cs index c2ac249..68678b7 100644 --- a/hw3B-tree/hw3B-tree/Program.cs +++ b/hw3B-tree/hw3B-tree/Program.cs @@ -16,16 +16,6 @@ static void Main(string[] args) tree.Insert("7", "7"); tree.Insert("8", "8"); tree.Insert("9", "9"); - tree.Insert("10", "10"); - tree.Insert("11", "11"); - tree.Insert("12", "12"); - tree.Insert("13", "13"); - tree.Insert("14", "14"); - tree.Insert("15", "15"); - tree.Insert("16", "16"); - tree.Insert("17", "17"); - tree.Insert("18", "18"); - tree.Delete("9"); } } } \ No newline at end of file From 240c2838eb153bc9551ee023f269e22c6fb75a6b Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Tue, 18 May 2021 03:23:03 +0300 Subject: [PATCH 12/12] fix mistakes --- hw3B-tree/hw3B-tree.Test/TestB-tree.cs | 45 +++++++++++++------------- hw3B-tree/hw3B-tree/BTree.cs | 16 +++------ 2 files changed, 26 insertions(+), 35 deletions(-) diff --git a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs index d74ee2a..9a0acf6 100644 --- a/hw3B-tree/hw3B-tree.Test/TestB-tree.cs +++ b/hw3B-tree/hw3B-tree.Test/TestB-tree.cs @@ -6,13 +6,12 @@ public class Tests { private BTree tree; - public BTree Setup(int MinimumDegreeOfTree) + public BTree Setup(int minimumDegreeOfTree) { - tree = new BTree(MinimumDegreeOfTree); + tree = new BTree(minimumDegreeOfTree); for (int i = 1; i < 19; ++i) { tree.Insert(i.ToString(), i.ToString()); - } return tree; } @@ -23,7 +22,7 @@ public void CheckInsert() var tree = Setup(2); for (int i = 1; i <= 18; ++i) { - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -33,7 +32,7 @@ public void CheckGetValue() var tree = Setup(2); for (int i = 1; i <= 18; ++i) { - Assert.IsTrue(tree.GetValue(i.ToString()) == i.ToString()); + Assert.AreEqual(i.ToString(), tree.GetValue(i.ToString())); } } @@ -42,7 +41,7 @@ public void CheckChangeValueByKey() { var tree = Setup(2); tree.ChangeValueByKey("5", "ololo"); - Assert.IsTrue(tree.GetValue("5") == "ololo"); + Assert.AreEqual("ololo", tree.GetValue("5")); } [TestCase] @@ -54,10 +53,10 @@ public void CheckDeleteFromRoot() { if (i == 8) { - Assert.IsFalse(tree.IsExists(i.ToString())); + Assert.IsFalse(tree.Exists(i.ToString())); continue; } - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -70,10 +69,10 @@ public void CheckDeleteFromLeaf() { if (i == 5) { - Assert.IsFalse(tree.IsExists(i.ToString())); + Assert.IsFalse(tree.Exists(i.ToString())); continue; } - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -86,10 +85,10 @@ public void CheckDeleteFromNotLeafWithChangeRoot() { if (i == 6) { - Assert.IsFalse(tree.IsExists(i.ToString())); + Assert.IsFalse(tree.Exists(i.ToString())); continue; } - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -102,10 +101,10 @@ public void CheckDeleteFromLeafWithChangeValueInLeafs() { if (i == 15) { - Assert.IsFalse(tree.IsExists(i.ToString())); + Assert.IsFalse(tree.Exists(i.ToString())); continue; } - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -115,7 +114,7 @@ public void CheckInsertWithAnotherDegree() var tree = Setup(4); for (int i = 1; i <= 18; ++i) { - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -125,7 +124,7 @@ public void CheckGetValueWithAnotherDegree() var tree = Setup(4); for (int i = 1; i <= 18; ++i) { - Assert.IsTrue(tree.GetValue(i.ToString()) == i.ToString()); + Assert.AreEqual(i.ToString(), tree.GetValue(i.ToString())); } } @@ -134,7 +133,7 @@ public void CheckChangeValueByKeyWithAnotherDegree() { var tree = Setup(4); tree.ChangeValueByKey("5", "ololo"); - Assert.IsTrue(tree.GetValue("5") == "ololo"); + Assert.AreEqual("ololo", tree.GetValue("5")); } [TestCase] @@ -146,10 +145,10 @@ public void CheckDeleteFromRootWithAnotherDegree() { if (i == 8) { - Assert.IsFalse(tree.IsExists(i.ToString())); + Assert.IsFalse(tree.Exists(i.ToString())); continue; } - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -162,10 +161,10 @@ public void CheckDeleteFromLeafWithMergeLefsWithAnotherDegree() { if (i == 2) { - Assert.IsFalse(tree.IsExists(i.ToString())); + Assert.IsFalse(tree.Exists(i.ToString())); continue; } - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } @@ -178,10 +177,10 @@ public void CheckDeleteFromLeafWithChangeValueInLeafsWithAnotherDegree() { if (i == 10) { - Assert.IsFalse(tree.IsExists(i.ToString())); + Assert.IsFalse(tree.Exists(i.ToString())); continue; } - Assert.IsTrue(tree.IsExists(i.ToString())); + Assert.IsTrue(tree.Exists(i.ToString())); } } } diff --git a/hw3B-tree/hw3B-tree/BTree.cs b/hw3B-tree/hw3B-tree/BTree.cs index 43aa43f..b29e31b 100644 --- a/hw3B-tree/hw3B-tree/BTree.cs +++ b/hw3B-tree/hw3B-tree/BTree.cs @@ -36,7 +36,7 @@ public BTree(int minimumDegreeOfTree) { if (minimumDegreeOfTree < 2) { - throw new ArgumentException("Минимальная степень дерева выбрана неправильна!"); + throw new ArgumentException("Минимальная степень дерева выбрана неправильно!"); } MinimumDegreeOfTree = minimumDegreeOfTree; } @@ -78,7 +78,7 @@ private Node FindNode(string key) /// checking for the presence of a key in a tree /// /// true - if key is in tree, false - if key isn't in tree - public bool IsExists(string key) + public bool Exists(string key) { var node = FindNode(key); if (node == null) @@ -98,7 +98,7 @@ public bool IsExists(string key) /// /// get value by key /// - /// return value, if we find key and return null,if we don't find key + /// return value, if we find key and return null, if we don't find key public string GetValue(string key) { var node = FindNode(key); @@ -251,14 +251,7 @@ public void Delete(string key) Remove(key, ref runner); if (root.CountKeys == 0) { - if (root.Leaf) - { - root = null; - } - else - { - root = root.Sons[0]; - } + root = root.Leaf ? null : root.Sons[0]; } } @@ -342,7 +335,6 @@ private void DeleteFromNotLeaf(int index, Node node) } else { - cursor = node;//////// Merge(index, ref cursor); cursor = node.Sons[index]; Remove(key, ref cursor);