public static IEnumerable<TreeNode> BinaryNodeToTreeChildren(Node binaryTree) { Node binNode = binaryTree; while (binNode != null) { yield return BinaryTreeToTree(binNode.Left); binNode = binNode.Right; } }
// print a binary tree as arbitrary tree public static string TreeString(Node node) { var sb = new StringBuilder(); Action<Node> f = null; f = n => { sb.Append("{"); for (Node child = n.Left; child != null; child = child.Right) { f(child); } sb.Append("}"); }; // build a new binary tree, // whose root has the original binary tree as left node, and null right node. f(new Node(node, null)); return sb.ToString(); }
// repr a binary tree as string. public static string BinaryTreeString(Node node) { var sb = new StringBuilder(); Action<Node> f = null; f = n => { if (n == null) { sb.Append("x"); } else { sb.Append("("); f(n.Left); f(n.Right); sb.Append(")"); } }; f(node); return sb.ToString(); }
public Node(Node left, Node right) { this.Left = left; this.Right = right; }
// convert a binary tree to an arbitrary tree public static TreeNode BinaryTreeToTree(Node binaryTree) { if (binaryTree == null) return null; return new TreeNode(BinaryNodeToTreeChildren(binaryTree)); }