//树的节点
class TreeNode{
int value;
TreeNode LeftNode;
TreeNode RightNode;
public TreeNode(int value) {
this.value = value;
}
//设置左儿子
public void setLeftNode(TreeNode LeftNode) {
this.LeftNode = LeftNode;
}
//设置右儿子
public void setRightNode(TreeNode RightNode) {
this.RightNode = RightNode;
}
//前序遍历
public void frontShow() {
System.out.print(value);
if (LeftNode != null) {
LeftNode.frontShow();
}
if (RightNode != null) {
RightNode.frontShow();
}
}
//中序遍历
public void midShow() {
if (LeftNode != null) {
LeftNode.midShow();
}
System.out.print(value);
if (RightNode != null) {
RightNode.midShow();
}
}
//后序遍历
public void afterShow() {
if (LeftNode != null) {
LeftNode.afterShow();
}
if (RightNode != null) {
RightNode.afterShow();
}
System.out.print(value);
}
//前序查找
public TreeNode frontSearch(int i) {
TreeNode target = null;
if (this.value == i) {
return this;
} else {
if (LeftNode != null) {
target = LeftNode.frontSearch(i);
}
if (target != null) {
return target;
}
if (RightNode != null) {
target = RightNode.frontSearch(i);
}
}
return target;
}
//删除一颗子树
public void delete(int i) {
TreeNode parent = this;
if (parent.LeftNode != null && parent.LeftNode.value == i) {
parent.LeftNode = null;
return;
}
if (parent.RightNode != null && parent.RightNode.value == i) {
parent.RightNode = null;
return;
}
parent = LeftNode;
if (parent != null) {
parent.delete(i);
}
parent = RightNode;
if (parent != null) {
parent.delete(i);
}
}
}
public class BinaryTree {
TreeNode root;
//设置根节点
public void setRoot(TreeNode root) {
this.root = root;
}
//返回根节点
public TreeNode getRoot() {
return root;
}
//前序遍历
public void frontShow() {
if (root != null) {
root.frontShow();
}
}
//中序遍历
public void midShow() {
if (root != null) {
root.midShow();
}
}
//后序遍历
public void afterShow() {
if (root != null) {
root.afterShow();
}
}
//前序查找
public TreeNode frontSearch(int i) {
return root.frontSearch(i);
}
//删除一颗子树
public void delete(int i) {
if (root.value == i) {
root = null;
} else {
root.delete(i);
}
}
}
前序遍历
1.先输出根节点
2.在看左子树递归输出
3.再看右子树递归输出
前序查找
1.如果查找的节点是根节点那么直接返回
2.左子树不为空查左子树
3.左子树没有且右子树不为空查右子数
删除子树
1.正好是根节点直接设为null
2.将这个节点设置成parent
3.左节点不为空的情况下恰好是i那么设为null
4.右节点不为空的情况下恰好是i那么设为null
5.在分别递归查找进行删除
测试
public static void main(String args[]) {
BinaryTree binTree = new BinaryTree();
TreeNode root = new TreeNode(1);
binTree.setRoot(root);
TreeNode rootL1 = new TreeNode(2);
TreeNode rootR1 = new TreeNode(3);
root.setLeftNode(rootL1);
root.setRightNode(rootR1);
TreeNode rootL2 = new TreeNode(4);
TreeNode rootR2 = new TreeNode(5);
TreeNode rootL3 = new TreeNode(6);
TreeNode rootR3 = new TreeNode(7);
rootL1.setLeftNode(rootL2);
rootL1.setRightNode(rootR2);
rootR1.setLeftNode(rootL3);
rootR1.setRightNode(rootR3);
System.out.print("前序遍历:");
binTree.frontShow();
System.out.println(" ");
System.out.print("中序遍历:");
binTree.midShow();
System.out.println(" ");
System.out.print("后序遍历:");
binTree.afterShow();
System.out.println(" ");
System.out.print("前序查找:");
TreeNode result = binTree.frontSearch(6);
System.out.println(result);
binTree.delete(3);
System.out.print("删除3之后的前序遍历:");
binTree.frontShow();
}