private void button2_Click(object sender, EventArgs e) { Prufer prufer = new Prufer(); if (TNode.CountVertices(root) < 3) { lbPrufer.Text = "Drzewo musi zawierać więcej niz 2 węzły"; lbPrufer.ForeColor = Color.Red; return; } int[] result = prufer.encode(root); string txt = String.Join(",", result); lbPrufer.ForeColor = Color.Black; lbPrufer.Text = txt; }
public int[] encode(TNode tree) { int n = TNode.CountVertices(tree); if (n <= 2) { return(null); } TNode treeToDecode = tree; int[] result = new int[n - 2]; for (int x = 0; x < n - 2; x++) { TNode lowestNode = treeToDecode.FindLowestLeaf(); TNode nearestNeighbor = lowestNode.Parent == null ? lowestNode.Children[0] : lowestNode.Parent; result[x] = (int)nearestNeighbor.label; if (nearestNeighbor.Parent == lowestNode) { nearestNeighbor.Parent = null; } else { nearestNeighbor.RemoveChild(lowestNode); } if (lowestNode.Parent != null) { lowestNode.Parent.RemoveChild(lowestNode); } else if (lowestNode == treeToDecode) { treeToDecode = treeToDecode.Children[0]; } } return(result); }