二叉树基本操作
public class BinaryTree {
public static void main(String[] args) {
TreeNode[] arr = new TreeNode[4];
arr[0] = new TreeNode(1, "1");
arr[1] = new TreeNode(0, "2");
arr[2] = new TreeNode(3, "3");
arr[3] = new TreeNode(4, "4");
Tree tree = new Tree(arr[0]);
tree.create(arr);
tree.treepreorder();
tree.treeinfixorder();
}
}
//创建二叉树
class Tree {
private TreeNode head;
public Tree(TreeNode head) {
this.head = head;
}
//创建树
public void create(TreeNode[] arr) {
if (head != null) {
TreeNode treeNode = head;
head.create(arr, treeNode);
}
}
//前序遍历
public void treepreorder() {
if (head != null) {
this.head.preorder();
}
}
//中序遍历
public void treeinfixorder() {
if (head != null) {
this.head.infixorder();
}
}
//后序遍历
public void treepostxorder() {
if (head != null) {
this.head.postorder();
}
}
//先序查找
public TreeNode searchpre(int num) {
if (head != null) {
return head.searchpre(num);
} else {
return null;
}
}
//中序遍历
public TreeNode searchord(int num) {
if (head != null) {
return head.searchord(num);
} else {
return null;
}
}
//后序遍历
public TreeNode searchpost(int num) {
if (head != null) {
return head.searchpost(num);
} else {
return null;
}
}
//删除结点
public void delNode(int num) {
//判断根结点是否是要删除结点
if (head != null) {
if (head.getId() != num) {
head.delNode(num);
} else {
head = null;
}
} else {
System.out.println("空树,不可以删除");
}
}
}
//创建结点
class TreeNode {
private int id;
private String name;
private TreeNode left;
private TreeNode right;
private static int i = 0;
public TreeNode() {
}
public TreeNode(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
//创建二叉树
public TreeNode create(TreeNode[] arr, TreeNode temp) {
if (arr == null || arr.length == 0) {
return null;
}
if (i < arr.length) {
temp = arr[i++];
if(temp.id==0){
return null;
}else{
temp.left = create(arr, temp.left);
temp.right = create(arr, temp.right);
}
}
return temp;
}
//前序遍历
public void preorder() {
System.out.println(" id = " + this.id + " name = " + this.name);
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(" id = " + this.id + " name = " + this.name);
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(" id = " + this.id + " name = " + this.name);
}
//先序查找
public TreeNode searchpre(int num) {
//比较当前结点是否相等
if (this.id == num) {
return this;
}
//当前结点不相等,向左递归
TreeNode temp = null;
if (this.left != null) {
temp = this.left.searchpre(num);
}
if (temp != null) {
return temp;
}
//左子树不满足,向右递归
if (this.right != null) {
temp = this.right.searchpre(num);
}
return temp;
}
//中序查找
public TreeNode searchord(int num) {
//判断当前结点左子树是否满足
TreeNode temp = null;
if (this.left != null) {
temp = this.left.searchord(num);
}
if (temp != null) {
return this;
}
//判断当前结点是否满足
if (this.id == num) {
return this;
}
//判断当前结点右结点是否满足
if (this.right != null) {
temp = this.right.searchord(num);
}
return temp;
}
//后序遍历
public TreeNode searchpost(int num) {
//判断左结点是否满足
TreeNode temp = null;
if (this.left != null) {
temp = this.left.searchpost(num);
}
if (temp != null) {
return temp;
}
//判断右结点是否满足
if (this.right != null) {
temp = this.right.searchpost(num);
}
if (temp != null) {
return temp;
}
//判断当前结点是否满足
if (this.id == num) {
return this;
}
return temp;
}
//删除结点
public void delNode(int num) {
//判断左结点是否满足
if (this.left != null && this.left.id == num) {
this.left = null;
return;
}
//判断右结点是否满足
if (this.right != null && this.right.id == num) {
this.right = null;
return;
}
//若都为满足,则继续递归
if (this.left != null) {
this.left.delNode(num);
}
if (this.right != null) {
this.right.delNode(num);
}
}
}