二叉树抽象数据结构定义
public class BinaryTreeNode {
private String value;
private BinaryTreeNode leftChild;
private BinaryTreeNode rightChild;
public BinaryTreeNode(String value) {
this.value = value;
}
}
以广义表的形式创建二叉树
public BinaryTreeNode createTree() {
int k = 0;
String endVale = "#";
Scanner scanner = new Scanner(System.in);
String value = scanner.next();
BinaryTreeNode root = null;
BinaryTreeNode p = null;
Stack<BinaryTreeNode> stack = new Stack<>();
while (value != endVale) {
switch (value) {
case "(":
k = 1;
stack.push(p);
break;
case ",":
k = 2;
break;
case ")":
stack.pop();
break;
default:
p = new BinaryTreeNode(value);
if (root == null) {
root = p;
} else if (k == 1) {
stack.peek().leftChild = p;
} else {
stack.peek().rightChild = p;
}
}
value = scanner.next();
}
return root;
}
二叉树的先序遍历
- 递归先序遍历
public void preOrder(BinaryTreeNode root) {
if (root != null) {
System.out.println(root.value);
preOrder(root.leftChild);
preOrder(root.rightChild);
}
}
- 非递归先序遍历
public void preOrderWithNoCur(BinaryTreeNode root) {
BinaryTreeNode p = root;
Stack<BinaryTreeNode> stack = new Stack<>();
while (p != null) {
System.out.println(p.value);
if (p.rightChild != null) {
stack.push(p.rightChild);
}
if (p.leftChild != null) {
p = p.leftChild;
} else if (!stack.isEmpty()) {
p = stack.pop();
} else {
p = null;
}
}
}
public void preOrderWithNoCur2(BinaryTreeNode root) {
BinaryTreeNode p = null;
Stack<BinaryTreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
p = stack.pop();
System.out.println(p.value);
if (p.rightChild != null) {
stack.push(p.rightChild);
}
if (p.leftChild != null) {
stack.push(p.leftChild);
}
}
}
二叉树的中序遍历
- 递归中序遍历
public void inOrder(BinaryTreeNode root) {
if (root != null) {
inOrder(root.leftChild);
System.out.println(root.value);
inOrder(root.rightChild);
}
}
- 非递归中序遍历
public void inOrderWithNoCur(BinaryTreeNode root) {
Stack<BinaryTreeNode> stack = new Stack<>();
BinaryTreeNode p = root;
do {
while (p != null) {
stack.push(p);
p = p.leftChild;
}
if (!stack.isEmpty()) {
BinaryTreeNode top = stack.pop();
System.out.println(top.value);
p = top.rightChild;
}
} while (p != null || !stack.isEmpty());
}
二叉树的后续遍历
- 递归后续遍历
public void postOrder(BinaryTreeNode root) {
if (root != null) {
postOrder(root.leftChild);
postOrder(root.rightChild);
System.out.println(root.value);
}
}
- 非递归后续遍历
public class StackEle {
private BinaryTreeNode binaryTreeNode;
private char child;
public StackEle(BinaryTreeNode binaryTreeNode, char child) {
this.binaryTreeNode = binaryTreeNode;
this.child = child;
}
public BinaryTreeNode getBinaryTreeNode() {
return binaryTreeNode;
}
public void setBinaryTreeNode(BinaryTreeNode binaryTreeNode) {
this.binaryTreeNode = binaryTreeNode;
}
public char getChild() {
return child;
}
public void setChild(char child) {
this.child = child;
}
}
public void postOrderWithNoCur(BinaryTreeNode root) {
BinaryTreeNode p = root;
Stack<StackEle> stack = new Stack<>();
do {
while (p != null) {
StackEle temp = new StackEle(p, 'L');
stack.push(temp);
p = p.leftChild;
}
int contin = 1;
while (contin != 0 && !stack.isEmpty()) {
StackEle stackEle = stack.pop();
p = stackEle.getBinaryTreeNode();
if (stackEle.getChild() == 'L') {
stackEle.setChild('R');
stack.push(stackEle);
p = p.rightChild;
contin = 0;
} else {
System.out.println(stackEle.getBinaryTreeNode().value);
}
}
} while (!stack.isEmpty());
}
二叉树的层次遍历
public void levelOrder(BinaryTreeNode root) {
Queue<BinaryTreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()) {
BinaryTreeNode q = queue.poll();
System.out.println(q.value);
if (q.leftChild != null) {
queue.offer(q.leftChild);
}
if (q.rightChild != null) {
queue.offer(q.rightChild);
}
}
}
计算二叉树节点的个数
public int size(BinaryTreeNode root) {
if (root == null) {
return 0;
}
return 1 + size(root.leftChild) + size(root.rightChild);
}
计算二叉树的高度
public int height(BinaryTreeNode root) {
if (root == null) {
return 0;
}
int i = height(root.leftChild);
int j = height(root.rightChild);
return i > j ? i + 1 : j + 1;
}
判断两个二叉树是否相等
public boolean equal(BinaryTreeNode one, BinaryTreeNode another) {
if (one == null && another == null) {
return true;
} else if (one != null && another != null &&
equal(one.leftChild, another.leftChild) && equal(one.rightChild, another.rightChild)) {
return true;
} else {
return false;
}
}
以广义表的形式打印二叉树
public void printBTree(BinaryTreeNode root) {
if (root != null) {
System.out.print(root.value);
if (root.leftChild != null || root.rightChild != null) {
System.out.print("(");
printBTree(root.leftChild);
System.out.print(",");
if (root.rightChild != null) {
printBTree(root.rightChild);
}
System.out.print(")");
}
}
}