2017-12-27 1312 views
1

如何發送(node.data)SortTree類到TreePrinter然後用於打印樹。如何打印二叉樹?

import javax.swing.tree.TreeNode; 


public class SortTree { 

static Node root; 
TreePrinter type =new TreePrinter(); 

class Node<A extends Comparable>{ 

int data; 
Node left, right; 

Node(int d) { 
    data = d; 
    left = null; 
right = null; 
} 
} 

Node sortedArrayToBST(int arr[], int start, int end) { 
if (start > end) { 
    return null; 
} 

int mid = (start + end)/2; 
Node node = new Node(arr[mid]); 
node.left = sortedArrayToBST(arr, start, mid - 1); 
node.right = sortedArrayToBST(arr, mid + 1, end);  
return node; 
} 
void preOrder(Node node) { 
if (node == null) { 
    return; 
} 
//System.out.print(node.data + " "); 
preOrder(node.left); 
preOrder(node.right); 
} 
} 

這是TreePrinter類:分類爲序幫忙送(node.data)treeprinter類,然後鍵入樹

import java.io.IOException; 
import java.io.OutputStreamWriter; 

public class TreePrinter { 

public static class Node<T extends Comparable<T>> { 
T value; 
Node<T> left, right; 

public void insertToTree(T v) { 
if (value == null) { 
    value = v; 
    return; 
} 
if (v.compareTo(value) < 0) { 
    if (left == null) { 
     left = new Node<T>(); 
    } 
    left.insertToTree(v); 
} else { 
    if (right == null) { 
     right = new Node<T>(); 
    } 
    right.insertToTree(v); 
} 
} 

public void printTree(OutputStreamWriter out) throws IOException { 
if (right != null) { 
    right.printTree(out, true, ""); 
} 
printNodeValue(out); 
if (left != null) { 
    left.printTree(out, false, ""); 
} 
} 

private void printNodeValue(OutputStreamWriter out) throws IOException { 
if (value == null) { 
    out.write("<null>"); 
} else { 
    out.write(value.toString()); 
} 
out.write('\n'); 
} 

private void printTree(OutputStreamWriter out, boolean isRight, String indent) throws IOException { 
if (right != null) { 
    right.printTree(out, true, indent + (isRight ? "  " : " |  ")); 
} 
out.write(indent); 
if (isRight) { 
    out.write("┌"); 
} else { 
    out.write("└"); 
} 
out.write("────"); 
printNodeValue(out); 
if (left != null) { 
    left.printTree(out, false, indent + (isRight ? " |  " : "  ")); 
} 
}}} 

節點:

回答

0

我建議你實現一個toString,而不是這TreePrinter。

我稍微更改了Node類,將它移到了SortTree之外,導致代碼可用於https://github.com/johanwitters/stackoverflow-tree-printer

節點的實現是在這裏:

package com.johanw.stackoverflow.tree; 

import com.johanw.stackoverflow.util.Helper; 

public class Node<A extends Comparable>{ 
    private static int AMOUNT_INDENT = 3; 

    private int data; 
    private Node left, right; 

    public Node(int d) { 
     data = d; 
     left = null; 
     right = null; 
    } 

    public void setLeft(Node left) { 
     this.left = left; 
    } 

    public void setRight(Node right) { 
     this.right = right; 
    } 

    public int getData() { 
     return data; 
    } 

    public Node getLeft() { 
     return left; 
    } 

    public Node getRight() { 
     return right; 
    } 

    public void indent(StringBuilder builder, int indent) { 
     builder.append(Helper.repeat(indent * (AMOUNT_INDENT + 1), " ")); 
    } 

    public void newLine(StringBuilder builder) { 
     builder.append(System.lineSeparator()); 
    } 

    public String toString(int indent) { 
     StringBuilder builder = new StringBuilder(); 
     builder.append(data); 
     newLine(builder); 
     if (left != null) { 
      indent(builder, indent); 
      builder.append("└" + Helper.repeat(AMOUNT_INDENT, "─") + left.toString(indent + 1)); 
     } 
     if (right != null) { 
      indent(builder, indent); 
      builder.append("└" + Helper.repeat(AMOUNT_INDENT, "─") + right.toString(indent + 1)); 
     } 
     return builder.toString(); 
    } 

    @Override 
    public String toString() { 
     return toString(0); 
    } 
} 

下面的單元測試給出了給定的樹結構的底部輸出:

public class TestSortTree { 
    @Test 
    public void test() { 
     Node node = new Node(1); 
     Node left = new Node(2); 
     Node leftLeft = new Node(22); 
     Node leftRight = new Node(23); 
     Node leftRightLeft = new Node(24); 
     left.setLeft(leftLeft); 
     leftRight.setLeft(new Node(39)); 
     left.setRight(leftRight); 
     node.setLeft(left); 
     node.setRight(new Node(3)); 
     System.out.println(node.toString()); 
    } 
} 

enter image description here

我希望這有助於

0

爲了能夠得到點頭e.data TreePrinter。我將添加以下代碼

class TreePrinter<T extends Comparable<T>>{ 
    Node<T extends Comparable<T>> root; 

    public TreePrinter(){ 
     root = new Node<T extends Comparable<T>>(); 
    } 

    public void insertToTree(T v) { 
     root.insertToTree(v); 
    } 

現在會很容易打印使用的算法從這個職位的樹:

How to print binary tree diagram?

我會通過進行以下修改將數據發送到Node sortedArrayToBST(int arr[], int start, int end)SortTree

void sortedArrayToBST(int arr[], int start, int end) { 
    if (start > end) { 
     return; 
    } 

    int mid = (start + end)/2; 
    type.insertToTree(arr[mid]); 
    node.left = sortedArrayToBST(arr, start, mid - 1); 
    node.right = sortedArrayToBST(arr, mid + 1, end); 
} 

我還沒有試過運行此所以可能需要一些調試,但我認爲這個基本想法是可靠的。希望這可以幫助!