static void Main(string[] args) { int[] values = { 16, 8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15, 24, 20, 18, 17, 19, 22, 21, 23, 28, 26, 25, 27, 30, 29, 31 }; var ltree = new TreeNode(); foreach (var item in values) { ltree.AddItem(item); } var node = ltree.GetNodeByValue(28); if (node != null) { Console.WriteLine($"\nFound an element with the value {node.Value}"); } ltree.PrintTree(); ltree.RemoveItem(8); ltree.RemoveItem(20); ltree.RemoveItem(28); Console.Clear(); ltree.PrintTree(); Console.ReadKey(); }
private static void Main() { int[] values = { 16, 8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15, 24, 20, 18, 17, 19, 22, 21, 23, 28, 26, 25, 27, 30, 29, 31 }; var lNode = new TreeNode(); foreach (var item in values) { lNode.AddItem(item); } lNode.PrintTree(); OutConsole("BFS: ", lNode.BreadthFirstSearch()); OutConsole("DFS: ", lNode.DeepFirstSearch()); Console.ReadKey(); }
public void Proc() { int itemCount = int.Parse(Reader.ReadLine()); this.NodeList = new TreeNode[itemCount]; for (int i = 0; i < itemCount - 1; i++) { int[] inpt = Reader.ReadLine().Split(' ').Select(a => int.Parse(a) - 1).ToArray(); inpt.ToList().ForEach(a => { if (!RoadDic.ContainsKey(a)) { RoadDic.Add(a, new List <int>()); } }); RoadDic[inpt[0]].Add(inpt[1]); RoadDic[inpt[1]].Add(inpt[0]); } NodeList[0] = new TreeNode(0); Queue <int> tsk = new Queue <int>(); RoadDic[0].ForEach(a => { NodeList[a] = new TreeNode(a); NodeList[0].AddItem(NodeList[a]); tsk.Enqueue(a); }); for (int i = 0; i < itemCount; i++) { if (tsk.Count() == 0) { break; } int id = tsk.Dequeue(); TreeNode current = this.NodeList[id]; if (!RoadDic.ContainsKey(current.ID)) { return; } foreach (int key in this.RoadDic[current.ID]) { if (key == current.Parent.ID) { continue; } NodeList[key] = new TreeNode(key); current.AddItem(NodeList[key]); tsk.Enqueue(key); } } int sunukeMinDepth = this.NodeList[itemCount - 1].Depth - (this.NodeList[itemCount - 1].Depth - 1) / 2; TreeNode sunukeRoot = this.NodeList[itemCount - 1]; while (sunukeRoot.Depth != sunukeMinDepth) { sunukeRoot = sunukeRoot.Parent; } int sunukeCount = sunukeRoot.SubNodesCount + 1; int fenecCount = itemCount - sunukeCount; string ans = "Fennec"; if (sunukeCount >= fenecCount) { ans = "Snuke"; } Console.WriteLine(ans); }