package tree;
import java.util.ArrayDeque;
import java.util.Stack;
class Node{
String data;
Node left;
Node right;
public Node(String t) {
this.data = t;
}
}
//顺序存储二叉树,完全二叉树
class arrayTree{
Node[] at;
//bt为完全二叉树
public arrayTree(BinaryTree bt){
ArrayDeque<Node> quene = new ArrayDeque<Node>();
int count = 0;
Node temp = bt.root;
quene.add(temp);
while(!quene.isEmpty()) {
temp = quene.poll();
count++;
if(temp.left != null) {
quene.add(temp.left);
}
if(temp.right != null) {
quene.add(temp.right);
}
}
at = new Node[count];
count = 0;
temp = bt.root;
quene.add(temp);
while(!quene.isEmpty()) {
temp = quene.poll();
at[count++] = temp;
if(temp.left != null) {
quene.add(temp.left);
}
if(temp.right != null) {
quene.add(temp.right);
}
}
}
//层次遍历
public void levelOrder(){
for(int i=0; i<at.length; i++) {
System.out.print(at[i].data + " ");
}
}
public void preOrder() {
this.preOrder(0);
}
//前序遍历
public void preOrder(int n) {
if(this.at == null || this.at.length == 0) {
return ;
}
System.out.print(at[n].data + " ");
if((2 * n + 1) < at.length) {
preOrder(2 * n + 1);
}
if((2 * n + 2) < at.length) {
preOrder(2 * n + 2);
}
}
public void inOrder() {
this.inOrder(0);
}
//中序遍历
public void inOrder(int n) {
if(this.at == null || this.at.length == 0) {
return ;
}
if((2 * n + 1) < at.length) {
inOrder(2 * n + 1);
}
System.out.print(at[n].data + " ");
if((2 * n + 2) < at.length) {
inOrder(2 * n + 2);
}
}
public void postOrder() {
this.postOrder(0);
}
//后序遍历
public void postOrder(int n) {
if(this.at == null || this.at.length == 0) {
return ;
}
if((2 * n + 1) < at.length) {
inOrder(2 * n + 1);
}
if((2 * n + 2) < at.length) {
inOrder(2 * n + 2);
}
System.out.print(at[n].data + " ");
}
}
public class BinaryTree {
Node root;
//递归前序遍历
public void preOrderRec(Node temp) {
if(temp == null) {
System.out.println("BinaryTree is empty.");
}else {
System.out.print(temp.data + " ");
if(temp.left != null) {
preOrderRec(temp.left);
}
if(temp.right != null) {
preOrderRec(temp.right);
}
}
}
//非递归前序遍历算法一,压栈的是右子节点,
public void preOrderNotRec1() {
Node temp = root;
Stack<Node> stack = new Stack<Node>();
stack.push(temp);
while(!stack.isEmpty()) {
temp = stack.pop();
System.out.print( temp.data + " ");
if(temp.right != null) {
stack.push(temp.right);
}
if(temp.left != null) {
stack.push(temp.left);
}
}
}
//非递归前序遍历算法二,压栈的是每个二叉子树的根节点,利用这个根节点来找右子节点
public void preOrderNotRec2() {
Node temp = root;
Stack<Node> stack = new Stack<Node>();
while(!stack.isEmpty() || temp != null) {
if(temp != null) {
System.out.print( temp.data + " ");
stack.push(temp);
temp = temp.left;
}else {
temp = stack.pop();
temp = temp.right;
}
}
}
//递归中序遍历
public void inOrderRec(Node temp) {
if(temp == null) {
System.out.println("BinaryTree is empty.");
}else {
if(temp.left != null) {
inOrderRec(temp.left);
}
System.out.print(temp.data + " ");
if(temp.right != null) {
inOrderRec(temp.right);
}
}
}
//非递归中序遍历
public void inOrderNotRec() {
Stack<Node> stack = new Stack<Node>();
Node temp = root;
while(!stack.isEmpty() || temp != null) {
if(temp != null) {
stack.push(temp);
temp = temp.left;
}else {
temp = stack.pop();
System.out.print(temp.data + " ");
temp = temp.right;
}
}
}
//递归后序遍历
public void postOrderRec(Node temp) {
if(temp == null) {
System.out.println("BinaryTree is empty.");
}else {
if(temp.left != null) {
postOrderRec(temp.left);
}
if(temp.right != null) {
postOrderRec(temp.right);
}
System.out.print(temp.data + " ");
}
}
//非递归后序遍历算法一
public void postOrderNotRec1() {
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
Node temp = root;
s1.push(temp);
while(!s1.isEmpty()) {
temp = s1.pop();
s2.push(temp);
if(temp.left != null) {
s1.push(temp.left);
}
if(temp.right != null) {
s1.push(temp.right);
}
}
while(!s2.isEmpty()) {
System.out.print(s2.pop().data + " ");
}
}
//非递归后序遍历算法二
public void postOrderNotRec2() {
Stack<Node> s1 = new Stack<Node>();
//用另一个栈记录是从左子树返回还是从右子树返回
Stack<Boolean> s2 = new Stack<Boolean>();
Node temp = root;
boolean flag;
while(!s1.isEmpty() || temp != null) {
if(temp != null) {
s1.push(temp);
s2.push(false);
temp = temp.left;
}else {
temp = s1.pop();
flag = s2.pop();
//false代表从左子树返回,此时需要先到右子树,并将tag改为true
if(!flag) {
s1.push(temp);
s2.push(true);
temp = temp.right;
}else {//从右子树返回说明该节点的左右子树的遍历过了,直接输出
System.out.print(temp.data + " ");
//此时temp位于子二叉树的根节点,不为空,将其手动置空,否则出现死循环
temp = null;
}
}
}
}
//层次遍历
public void levelOrder() {
ArrayDeque<Node> quene = new ArrayDeque<Node>();
Node temp = this.root;
quene.add(temp);
while(!quene.isEmpty()) {
temp = quene.poll();
System.out.print(temp.data + " ");
if(temp.left != null) {
quene.add(temp.left);
}
if(temp.right != null) {
quene.add(temp.right);
}
}
}
//前序遍历查找
public Node preSearch(Node temp, String value) {
if(temp.data.equals(value)) {
return temp;
}
Node res = null;
if(temp.left != null) {
res = preSearch(temp.left, value);
}
if(res != null) {
return res;
}
if(temp.right != null) {
res = preSearch(temp.right, value);
}
return res;
}
//中序遍历查找
public Node inSearch(Node temp, String value) {
Node res = null;
if(temp.left != null) {
res = inSearch(temp.left, value);
}
if(res != null) {
return res;
}
if(temp.data.equals(value)) {
return temp;
}
if(temp.right != null) {
res = inSearch(temp.right, value);
}
return res;
}
//后序遍历查找
public Node postSearch(Node temp, String value) {
Node res = null;
if(temp.left != null) {
res = postSearch(temp.left, value);
}
if(res != null) {
return res;
}
if(temp.right != null) {
res = postSearch(temp.right, value);
}
if(res != null) {
return res;
}
if(temp.data.equals(value)) {
return temp;
}
return res;
}
//层次遍历查找
public Node levelSearch(String value) {
ArrayDeque<Node> quene = new ArrayDeque<Node>();
Node temp = this.root;
quene.add(temp);
while(!quene.isEmpty()) {
temp = quene.poll();
if(temp.data.equals(value)) {
return temp;
}
if(temp.left != null) {
quene.add(temp.left);
}
if(temp.right != null) {
quene.add(temp.right);
}
}
return null;
}
//二叉树的删除
//1.如果要删除的是根节点,置空二叉树
//2.如果是叶子节点直接删除
//3.如果不是叶子节点删除子树
public void delNode1(Node temp, String value) {
if(this.root == null) {
System.out.println("empty tree.");
return ;
}
if(this.root.data.equals(value)) {
this.root = null;
}
if(temp.left != null && temp.left.data.equals(value)) {
temp.left = null;
return ;
}
if(temp.right != null && temp.right.data.equals(value)) {
temp.right = null;
return ;
}
if(temp.left != null) {
delNode1(temp.left,value);
}
if(temp.right != null) {
delNode1(temp.right, value);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree bt = new BinaryTree();
Node A = new Node("A");
Node B = new Node("B");
Node C = new Node("C");
Node D = new Node("D");
Node E = new Node("E");
Node F = new Node("F");
Node G = new Node("G");
Node H = new Node("H");
Node I = new Node("I");
ThreadedBinaryTree tbt = new ThreadedBinaryTree();
bt.root = A;
A.left = B;
A.right = C;
C.left = I;
B.left = D;
B.right = E;
D.left = F;
D.right = G;
E.right = H;
bt.postOrderNotRec1();
}
}
//前序 A B D E G H C F
//中序 D B G E H A C F
//后序 D G H E B F C A
Java二叉树相关全部代码
最新推荐文章于 2023-07-19 12:55:04 发布