public void TreeComplite(TreeAlgorithm tree, int[] values) { for (int i = 0; i < values.Length; i++) { tree.Add(values[i]); } }
public void AddWithBlackUncleAndRedParent() { //должен быть совершен поворот относительно родителя //родитель должен стать черным,а новый ,брат - красным //для сохранения свойства "Оба потомка каждого красного узла — чёрные." //и свойства "Все пути от любого данного узла до листовых узлов содержат одинаковое число чёрных узлов." TreeAlgorithm tree = new TreeAlgorithm(); var newNode = 30; //изначально имеет касного родителя и черного дядю tree.Add(56); //root black tree.Add(12); //black tree.Add(65); //parent red tree.Add(16); //red //добавление нового узла tree.Add(newNode); var result = tree.Root; //проверка Assert.AreEqual(16, result.Left.Value); Assert.AreEqual(Color.B, result.Left.Colour); //цвет родителя Assert.AreEqual(12, result.Left.Left.Value); Assert.AreEqual(Color.R, result.Left.Left.Colour); //цвет брата Assert.AreEqual(30, result.Left.Right.Value); Assert.AreEqual(Color.R, result.Left.Right.Colour); //цвет узла }
public static TreeNode GetRootTreeNode(TreeAlgorithm alg, List <DataRow> data, List <string> columns, string labelColumn, int maxDepth) { if (alg == TreeAlgorithm.ID3) { return(new TreeNodeID3(data, columns, labelColumn, 0, maxDepth)); } throw new NotImplementedException($"{alg} is not implemented yet!"); }
public void AddRoot() { TreeAlgorithm tree = new TreeAlgorithm(); tree.Add(56); var expectedColor = Color.B; var resultColor = tree.FindKey(56).Colour; Assert.AreEqual(expectedColor, resultColor); }
public StateTree(TreeAlgorithm <T> algorithm, StateManager <T> manager, T root) { this.Algorithm = algorithm; this.Manager = manager; var node = new Node(); node.State = root; node.Avaliation = manager.Avaliation(root); this.root = node; }
public void DeleteNonexistentNodeTest() { TreeAlgorithm tree = new TreeAlgorithm(); int[] values = { 56, 12, 65, 16, 30 }; TreeComplite(tree, values); var result = tree.Delete1(70);; Assert.AreEqual(double.NaN, result); }
public void DeleteTest() { TreeAlgorithm tree = new TreeAlgorithm(); int[] values = { 22, 4, 61, 25, 66, 27, 10, 9, 7, 43, 36, 75, 46, 11 }; TreeComplite(tree, values); //удаление всего дерева полностью for (int i = 0; i < values.Length; i++) { tree.Delete1(values[i]); } Assert.IsNull(tree.Root); }
public void DeleteWithRedPrevTest() { TreeAlgorithm tree = new TreeAlgorithm(); int[] values = { 56, 12, 65, 16, 30 }; TreeComplite(tree, values); tree.Delete1(56); var result = tree.Root; Assert.AreEqual(30, result.Value); Assert.AreEqual(16, result.Left.Value); Assert.IsNull(result.Left.Right);//старое местоположение узла 30 теперь должно быть пустым Assert.IsNull(tree.FindKey(56)); }
public void DeleteWithRedPrevChildTest() { TreeAlgorithm tree = new TreeAlgorithm(); int[] values = { 56, 12, 65, 16, 30, 20 }; TreeComplite(tree, values); tree.Delete1(56); var result = tree.Root; Assert.AreEqual(30, result.Value); Assert.AreEqual(16, result.Left.Value); Assert.AreEqual(Color.R, result.Left.Colour); //цвет узла 16(родителя) lдолжен поменяться на красный Assert.AreEqual(20, result.Left.Right.Value); //на месте узла 30 теперь должен быть его потомок - узел 20 Assert.IsNull(tree.FindKey(56)); }
public void DeleteWithoutChildPrevTest() { TreeAlgorithm tree = new TreeAlgorithm(); int[] values = { 56, 12, 65, 16, 30, 20 }; TreeComplite(tree, values); tree.Delete1(16); var result = tree.Root; Assert.AreEqual(56, result.Value); Assert.AreEqual(20, result.Left.Value); //после поворота должен стать потомком корневого узла Assert.AreEqual(Color.R, result.Left.Colour); Assert.AreEqual(12, result.Left.Left.Value); // узел 12 должен стать потомком узла 20 Assert.AreEqual(Color.B, result.Left.Left.Colour); Assert.AreEqual(30, result.Left.Right.Value); // узел 30 должен стать потомком узла 20 Assert.IsNull(tree.FindKey(16)); }
public void MinTest() { if (tree.Root == null) { TreeComplite(); } TreeAlgorithm tree1 = new TreeAlgorithm(); tree1.Add(-69); tree1.Add(-15); tree1.Add(-36); tree1.Add(-17); TreeAlgorithm tree2 = new TreeAlgorithm(); Assert.AreEqual(4, tree.Min().Value); Assert.AreEqual(-69, tree1.Min().Value); Assert.IsNull(tree2.Max()); }
public void MaxTest() { if (tree.Root == null) { TreeComplite(); } TreeAlgorithm tree1 = new TreeAlgorithm(); tree1.Add(-23); tree1.Add(-36); tree1.Add(-43); tree1.Add(-2); TreeAlgorithm tree2 = new TreeAlgorithm(); Assert.AreEqual(66, tree.Max().Value); Assert.AreEqual(-2, tree1.Max().Value); Assert.IsNull(tree2.Max()); }
public void AddWithRedUncleAndRedParent() { //должен помен¤тьс¤ цвет у родител¤ и д¤ди на черный //дл¤ сохранени¤ свойства "ќба потомка каждого красного узла Ч чЄрные." TreeAlgorithm tree = new TreeAlgorithm(); tree.Add(56); //root black tree.Add(12); //red tree.Add(65); //red tree.Add(16); var result = tree.Root; Assert.AreEqual(56, result.Value); Assert.AreEqual(12, result.Left.Value); Assert.AreEqual(Color.B, result.Left.Colour); //цвет родител¤ Assert.AreEqual(65, result.Right.Value); Assert.AreEqual(Color.B, result.Left.Colour); //цвет д¤ди Assert.AreEqual(16, result.Left.Right.Value); }
public void AddWithRedParrent() { //Добавление узла к черному родителю TreeAlgorithm tree = new TreeAlgorithm(); var newNode1 = 12; var newNode2 = 65; //mainTree tree.Add(56);//root black Test1 //добавление новых узлов tree.Add(newNode1); tree.Add(newNode2); var result = tree.Root; Assert.AreEqual(12, result.Left.Value); Assert.AreEqual(Color.R, result.Left.Colour); //цвет первого узла Assert.AreEqual(65, result.Right.Value); Assert.AreEqual(Color.R, result.Left.Colour); //цвет второго узла }
public void AddWithRedUncleAndRedParent() { //должен поменяться цвет у родителя и дяди на черный //для сохранения свойства "Оба потомка каждого красного узла — чёрные." TreeAlgorithm tree = new TreeAlgorithm(); var newNode = 16; //mainTree tree.Add(56); //root black Test1 tree.Add(12); //red Test2 tree.Add(65); //red Test2 //Добавление нового узла tree.Add(newNode); var result = tree.Root; Assert.AreEqual(56, result.Value); Assert.AreEqual(12, result.Left.Value); Assert.AreEqual(Color.B, result.Left.Colour); //цвет родителя Assert.AreEqual(65, result.Right.Value); Assert.AreEqual(Color.B, result.Left.Colour); //цвет дяди Assert.AreEqual(16, result.Left.Right.Value); }
public DecisionTree(TreeAlgorithm alg, int maxDepth = 0) { MaxDepth = maxDepth; }