public BFSBinaryTree(Node node) { int max = maxLevel(node, 0); nodes = new List<Node>[max]; for (int i = 0; i < max; i++) nodes[i] = new List<Node>(); queue.Enqueue(node); bfs(node, 0); }
public void test() { Node node = new Node(); node.Value = 5; node.Left = new Node(); node.Left.Value = 1; node.Right = new Node(); node.Right.Value = 6; Node res = find(node, 1, 6); AssertHelper.assert(res != null && res.Value == 5, "True"); }
public Node find(Node node, int value, int value2) { while (node != null) { if (value > node.Value && value2 > node.Value) node = node.Right; else if (value < node.Value && value2 < node.Value) node = node.Left; else return node; } return null; }
public void bfs(Node node) { queue.Enqueue(node); while (queue.Count > 0) { Node n = queue.Dequeue(); if (n.Left != null) { queue.Enqueue(n.Left); } if (n.Right != null) { queue.Enqueue(n.Right); } Console.Write(n.Value + " "); } }
private void bfs(Node node, int level) { if (node == null) return; while (queue.Count > 0) { Node n = queue.Dequeue(); nodes[level].Add(node); } if (node.Left != null) queue.Enqueue(node.Left); if (node.Right != null) queue.Enqueue(node.Right); bfs(node.Left, level + 1); bfs(node.Right, level + 1); }
public static void test() { Node node = new Node(); node.Value = 5; node.Left = new Node(); node.Left.Value = 1; node.Left.Left = new Node(); node.Left.Left.Value = 2; node.Left.Right = new Node(); node.Left.Right.Value = 3; node.Right = new Node(); node.Right.Value = 6; node.Right.Left = new Node(); node.Right.Left.Value = 7; node.Right.Right = new Node(); node.Right.Right.Value = 10; BFSBinaryTree bfs = new BFSBinaryTree(node); bfs.bfs(node); //var v = bfs.levels(); }
private int maxLevel(Node node, int level) { if (node == null) return level; int left = maxLevel(node.Left, level + 1); int right = maxLevel(node.Right, level + 1); return Math.Max(left, right); }