题目:输入两棵二叉树A和B,判断B是不是A的子结构。二叉树结点的定义如下:
class BTree {
String value;
BTree lchild, rchild;
public BTree() {
}
public BTree(String value) {
this.value = value;
lchild = null;
rchild = null;
}
public BTree getLchild() {
return lchild;
}
public void setLchild(BTree lchild) {
this.lchild = lchild;
}
public BTree getRchild() {
return rchild;
}
public void setRchild(BTree rchild) {
this.rchild = rchild;
}
}
具体实现:
package Tree;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class BineryTree1 {
private BTree root;
public BineryTree1() {
}
public BineryTree1(BTree root) { // 构造函数
this.root = root;
}
// 创建二叉树
public void createBiTree(String s) {
Scanner scn = null;
try {
scn = new Scanner(new File(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.root = createBiTree(root, scn);
}
private BTree createBiTree(BTree node, Scanner scn) {
String temp = scn.next();
if (temp.trim().equals("#")) {
return null;
} else {
node = new BTree(temp);
node.setLchild(createBiTree(node.getLchild(), scn));
node.setRchild(createBiTree(node.getRchild(), scn));
return node;
}
}
// 遍历二叉树
// 前序递归遍历二叉树
public void preOrderTree(BTree node) {
if (node == null)
return;
System.out.print(node.value + " ");
preOrderTree(node.lchild);
preOrderTree(node.rchild);
}
// 用栈非递归先序遍历
@SuppressWarnings("unused")
private void preOderTraverse() {
Stack<BTree> stack = new Stack<BTree>();
BTree node = root;
while (node != null | !stack.isEmpty()) {
while (node != null) {
System.out.print(node.value + " ");
stack.push(node);
node = node.getLchild();
}
node = stack.pop();
node = node.getRchild();
}
}
// 中序递归遍历二叉树
public void inOrderTree(BTree node) {
if (node == null)
return;
inOrderTree(node.lchild);
System.out.print(node.value + " ");
inOrderTree(node.rchild);
}
// 用栈来非递归中序遍历二叉树
public void inOrderTraverse() {
Stack<BTree> stack = new Stack<BTree>();
BTree node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLchild();
}
node = stack.pop();
System.out.print(node.value + " ");
node = node.getRchild();
}
}
// 后序递归遍历二叉树
public void postOrderTree(BTree node) {
if (node == null)
return;
postOrderTree(node.lchild);
postOrderTree(node.rchild);
System.out.print(node.value + " ");
}
// 非递归后序遍历二叉树
public void postOderTraverse() {
Stack<BTree> stack = new Stack<BTree>();
BTree node = root;
BTree n = root;
while (node != null) {
// 左子树入栈
for (; node.getLchild() != null; node = node.getLchild()) {
stack.push(node);
}
// 当节点无右子树或者右子树已经输出
while (node != null && (node.getRchild() == null)
|| (node.getRchild() == n)) {
System.out.print(node.value + " ");
n = node;// 记录已经输出的节点
if (stack.isEmpty())
return;
node = stack.pop();
}// 处理右子树
stack.push(node);
node = node.getRchild();
}
}
// 层次遍历二叉树,用队列
public void levelTreverse() {
Queue<BTree> queue = new LinkedList<BTree>();
BTree node = root;
queue.add(node);
while (!queue.isEmpty()) {
node = queue.remove();
if (node != null) {
System.out.print(node.value + " ");
queue.add(node.getLchild());
queue.add(node.getRchild());
}
}
}
public static boolean HasSuntree(BTree tree1, BTree tree2) {
boolean result = false;
if (tree1 != null && tree2 != null) {
if (tree1.value == tree2.value)
result = DosTreeHaveTree2(tree1, tree2);
if (!result)
result = DosTreeHaveTree2(tree1.lchild, tree2);
if (!result)
result = DosTreeHaveTree2(tree1.rchild, tree2);
}
return result;
}
private static boolean DosTreeHaveTree2(BTree tree1, BTree tree2) {
if (tree2 == null)
return true;
if (tree1 == null)
return false;
if (tree1.value != tree2.value)
return false;
return DosTreeHaveTree2(tree1.lchild, tree2.lchild)
&& DosTreeHaveTree2(tree1.rchild, tree2.rchild);
}
public static void main(String[] args) {
BineryTree1 tree = new BineryTree1();
String s = "F:\\input.txt";
tree.createBiTree(s);
BineryTree1 tree2 = new BineryTree1();
String s1 = "F:\\inpu.txt";
tree2.createBiTree(s1);
System.out.println(HasSuntree(tree.root, tree2.root));
}
}