java – 链表实现二叉树
1. 首先创建树
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 value){
return root.frontSearch(value);
}
//中序查找
public TreeNode midSearch(int value){
return root.midSearch(value);
}
//后序查找
public TreeNode afterSearch(int value){
return root.afterSearch(value);
}
//删除子树
public void delete(int value){
if(root.value==value){
root=null;
}else{
root.delete(value);
}
}
}
2. 再创建节点
public class TreeNode {
//节点的权
int value;
//左右节点
TreeNode leftNode;
TreeNode rightNode;
public void setLeftNode(TreeNode leftNode) {
this.leftNode = leftNode;
}
public void setRightNode(TreeNode rightNode) {
this.rightNode = rightNode;
}
public TreeNode(int value){
this.value = value;
}
//前序遍历
public void frontShow(){
//当前节点
System.out.println(value);
//左子节点
if(leftNode!=null){
leftNode.frontShow();
}
//右子节点
if(rightNode!=null){
rightNode.frontShow();
}
}
//中序遍历
public void midShow(){
//左子节点
if(leftNode!=null){
leftNode.midShow();
}
//当前节点
System.out.println(value);
//右子节点
if(rightNode!=null){
rightNode.midShow();
}
}
//后序遍历
public void afterShow(){
//左子节点
if(leftNode!=null){
leftNode.afterShow();
}
//右子节点
if(rightNode!=null){
rightNode.afterShow();
}
//当前节点
System.out.println(value);
}
//前序查找
public TreeNode frontSearch(int value){
TreeNode result = null;
if(this.value==value){
return this;
}
if(leftNode!=null){
result = leftNode.frontSearch(value);
}
if(rightNode!=null){
result = rightNode.frontSearch(value);
}
return result;
}
//中序查找
public TreeNode midSearch(int value){
TreeNode result = null;
if(leftNode!=null){
result = leftNode.midSearch(value);
}
if(this.value==value){
return this;
}
if(rightNode!=null){
result = rightNode.midSearch(value);
}
return result;
}
//后序查找
public TreeNode afterSearch(int value){
TreeNode result = null;
if(leftNode!=null){
result = leftNode.afterSearch(value);
}
if(rightNode!=null){
result = rightNode.afterSearch(value);
}
if(this.value==value){
return this;
}
return result;
}
//删除子树
public void delete(int value){
TreeNode parent = this;
if(parent.leftNode!=null&&parent.leftNode.value==value){
parent.leftNode=null;
return;
}
if(parent.rightNode!=null&&parent.rightNode.value==value){
parent.rightNode=null;
return;
}
parent=parent.leftNode;
if(parent!=null){
parent.delete(value);
}
parent=parent.rightNode;
if(parent!=null){
parent.delete(value);
}
}
}
3.写一个测试类
public class TestBinaryTree {
public static void main(String[] args) {
//创建一棵树
BinaryTree binaryTree = new BinaryTree();
//创建一个根节点
TreeNode root = new TreeNode(1);
//把根节点赋给树
binaryTree.setRoot(root);
//创建两个子节点
TreeNode leftnode = new TreeNode(2);
TreeNode rightnode = new TreeNode(3);
//把两个节点摄制为根节点的子节点
root.setLeftNode(leftnode);
root.setRightNode(rightnode);
//为第二层创建子节点
leftnode.setLeftNode(new TreeNode(4));
leftnode.setRightNode(new TreeNode(5));
rightnode.setLeftNode(new TreeNode(6));
rightnode.setRightNode(new TreeNode(7));
//前序遍历树
binaryTree.frontShow();
//中序遍历
binaryTree.midShow();
//后序遍历
binaryTree.afterShow();
//前序查找
TreeNode frontresult = binaryTree.frontSearch(7);
System.out.println(frontresult);
//中序查找
TreeNode midresult = binaryTree.midSearch(7);
System.out.println(midresult);
//后序查找
TreeNode afteresult = binaryTree.afterSearch(7);
System.out.println(afteresult);
//删除子树
binaryTree.delete(3);
binaryTree.frontShow();
}
}