二叉树的定义:
二叉树是每个节点最多只有两个字数的树结构,在图的定义中二叉树是一个连通的无环图,并且每个顶点的度不大于3(根节点的度不大于2)。
二叉树的基本类型:
二叉树的实现:
- 节点结构设计
class Node{
private Object data;
private Node leftChild;
private Node rightChild;
public Node(Object data){
this.data = data;
this.leftChild =null;
this.rightChild = null;
}
public Object getData(){
return this.data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
}
2.创建二叉树
public Node createBTree(char[] str){
if(str[i]== '0'){
i++;
return null;
}else {
Node node = new Node(str[i++]);
node.setLeftChild(createBTree(str));
node.setRightChild(createBTree(str));
return node;
}
}
3.遍历二叉树
//先序递归遍历
public void preOrder(Node node){
if(node != null){
System.out.print(node.getData());
preOrder(node.getLeftChild());
preOrder(node.getRightChild());
}
}
//先序非递归遍历
public void nPreOrder(Node node){
Stack<Node> stack = new Stack<Node>();
while(node != null || !stack.isEmpty()){
while(node != null){
System.out.print(node.getData());
stack.push(node);
node = node.getLeftChild();
}
node = stack.pop();
node = node.getRightChild();
}
}
//中序递归遍历
public void inOrder(Node node){
if(node != null){
inOrder(node.getLeftChild());
System.out.print(node.getData());
inOrder(node.getRightChild());
}
}
//中序非递归遍历
public void nInOrder(Node node){
Stack<Node> stack = new Stack<Node>();
while(node != null || !stack.isEmpty()){
while(node != null){
stack.push(node);
node = node.getLeftChild();
}
node = stack.pop();
System.out.print(node.getData());
node = node.getRightChild();
}
}
//后序递归遍历
public void pastOrder(Node node){
if(node != null){
pastOrder(node.getLeftChild());
pastOrder(node.getRightChild());
System.out.print(node.getData());
}
}
//后序非递归遍历
public void nPastOrder(Node node){
Stack<Node> stack = new Stack<Node>();
Node preNode = null;
while(node != null || !stack.isEmpty()){
while(node != null){
stack.push(node);
node = node.getLeftChild();
}
node = stack.peek();
if(node.getRightChild() == null || node.getRightChild() == preNode){
System.out.print(node.getData());
node = stack.pop();
preNode = node;
node = null;
}else{
node = node.getRightChild();
}
}
}
4.测试及输出结果
public static void main(String[] args) {
String str = "ABC00DE00F00G0H00";
BTreeTest bt = new BTreeTest();
root = bt.createBTree(str.toCharArray());
System.out.print("先序递归遍历:");
bt.preOrder(root);
System.out.println();
System.out.print("先序非递归遍历:");
bt.nPreOrder(root);
System.out.println();
System.out.print("中序递归遍历:");
bt.inOrder(root);
System.out.println();
System.out.print("中序非递归遍历:");
bt.nInOrder(root);
System.out.println();
System.out.print("后序递归遍历:");
bt.pastOrder(root);
System.out.println();
System.out.print("后序非递归遍历:");
bt.nPastOrder(root);
}
/**
*输出结果:
*先序递归遍历:ABCDEFGH
*先序非递归遍历:ABCDEFGH
*中序递归遍历:CBEDFAGH
*中序非递归遍历:CBEDFAGH
*后序递归遍历:CEFDBHGA
*后序非递归遍历:CEFDBHGA
*/