Exemple #1
0
 public static IEnumerable<TreeNode> BinaryNodeToTreeChildren(Node binaryTree)
 {
     Node binNode = binaryTree;
     while (binNode != null)
     {
         yield return BinaryTreeToTree(binNode.Left);
         binNode = binNode.Right;
     }
 }
Exemple #2
0
        // 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();
        }
Exemple #3
0
 // 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();
 }
Exemple #4
0
 public Node(Node left, Node right)
 {
     this.Left = left;
     this.Right = right;
 }
Exemple #5
0
 // convert a binary tree to an arbitrary tree
 public static TreeNode BinaryTreeToTree(Node binaryTree)
 {
     if (binaryTree == null) return null;
     return new TreeNode(BinaryNodeToTreeChildren(binaryTree));
 }