问题遇到的现象和发生背景
本来应该是直接输出字母的,但不知道为啥会输出乱码,而且把节点类型改成int,输入整型数据的话结果就是正常的
问题相关代码,请勿粘贴截图
package 实验三;
public class BinaryTreeTest {
public static void main(String[] args) {
BiTree bitree=new BiTree();
Node root=new Node("A");
Node node2=new Node("B");
Node node3=new Node("C");
Node node4=new Node("D");
Node node5=new Node("E");
Node node6=new Node("F");
Node node7=new Node("G");
Node node8=new Node("H");
Node node9=new Node("I");
Node node10=new Node("J");
Node node11=new Node("k");
bitree.setRoot(root);
root.setLeft(node2);
root.setRight(node3);
node2.setLeft(node4);
node2.setRight(node5);
node4.setRight(node8);
System.out.println("前序遍历结果为");
bitree.preOrder();
System.out.println("中序遍历结果为");
bitree.infixOrder();
System.out.println("后序遍历结果为");
bitree.postOrder();
}
}
class BiTree{
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public void preOrder() {
if(this.root!=null) {
this.root.preOrder();
}else {
System.out.println("二叉树为空,无法进行遍历");
}
}
public void infixOrder() {
if(this.root!=null) {
this.root.infixOrder();
}else {
System.out.println("二叉树为空,无法进行遍历");
}
}
public void postOrder() {
if(this.root!=null) {
this.root.postOrder();
}else {
System.out.println("二叉树为空,无法进行遍历");
}
}
}
class Node{
private String ABC;
private Node left;
private Node right;
public Node(String aBC) {
super();
ABC = aBC;
}
public String getABC() {
return ABC;
}
public void setABC(String aBC) {
ABC = aBC;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public void preOrder() {
System.out.println(this);
if(this.left!=null) {
this.left.preOrder();
}
if(this.right!=null) {
this.right.preOrder();
}
}
public void infixOrder() {
if(this.left!=null) {
this.left.infixOrder();
}
System.out.println(this);
if(this.right!=null) {
this.right.infixOrder();
}
}
public void postOrder() {
if(this.left!=null) {
this.left.postOrder();
}
if(this.right!=null) {
this.right.postOrder();
}
System.out.println(this);
}
}