创建二叉树
链式存储二叉树
二叉数的五种形态
package BinaryTree;
public class 创建二叉树 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个树(空树),具体请看二叉树的五种形态
BinaryTree binTree=new BinaryTree();
//创建一个根节点
TreeNode root=new TreeNode(1);
//把根节点赋值给树
binTree.setRoot(root);
//创建两个节点
TreeNode rootL=new TreeNode(2);
//把新创建的节点设置为根节点的子节点
root.setlNode(rootL);
//创建两个节点
TreeNode rootR=new TreeNode(3);
//把新创建的节点设置为根节点的子节点
root.setrNode(rootR);
}
}
package BinaryTree;
public class TreeNode {
int value;//节点的权
TreeNode lNode;//左儿子
TreeNode rNode;//右儿子
public TreeNode(int value){
this.value=value;
}
//设置左儿子
public void setlNode(TreeNode lNode){
this.lNode=lNode;
}
//设置右儿子
public void setrNode(TreeNode rNode){
this.rNode=rNode;
}
}
package BinaryTree;
public class BinaryTree {
TreeNode root;
//设置根节点
public TreeNode getRoot() {
return root;
}
//获取根节点
public void setRoot(TreeNode root) {
this.root = root;
}
}
遍历二叉树
有点错误,但不知道在哪了
package BinaryTree;
public class 创建二叉树 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个树(空树),具体请看二叉树的五种形态
BinaryTree binTree=new BinaryTree();
//创建一个根节点
TreeNode root=new TreeNode(1);
//把根节点赋值给树
binTree.setRoot(root);
//创建两个节点
TreeNode rootL=new TreeNode(2);
//把新创建的节点设置为根节点的子节点
root.setlNode(rootL);
//创建两个节点
TreeNode rootR=new TreeNode(3);
//把新创建的节点设置为根节点的子节点
root.setrNode(rootR);
//为第二层的左节点创建两个子节点
rootL.setlNode(new TreeNode(4));
rootL.setrNode(new TreeNode(5));
//为第二层的右节点创建两个子节点
rootR.setlNode(new TreeNode(6));
rootR.setrNode(new TreeNode(7));
//前序遍历
binTree.fronShow();
System.out.println();
//中序遍历
binTree.midShow();
System.out.println();
//后续遍历
binTree.afterShow();
System.out.println();
//前序查找
TreeNode result=binTree.frontSearch(3);
System.out.println(result==rootR);
}
}
```java
package BinaryTree;
public class TreeNode {
int value;//节点的权
TreeNode lNode;//左儿子
TreeNode rNode;//右儿子
public TreeNode(int value){
this.value=value;
}
//设置左儿子
public void setlNode(TreeNode lNode){
this.lNode=lNode;
}
//设置右儿子
public void setrNode(TreeNode rNode){
this.rNode=rNode;
}
//前序遍历
public void frontShow() {
// TODO Auto-generated method stub
//先遍历当前节点的内容
System.out.print(value+" ");
//左节点
if(lNode!=null){
lNode.frontShow();
}
//右节点
if(rNode!=null){
rNode.frontShow();
}
}
public void midShow() {
// TODO Auto-generated method stub
//左节点
if(lNode!=null){
lNode.midShow();
}
System.out.print(value+" ");
if(rNode!=null){
rNode.midShow();
}
}
public void afterShow() {
// TODO Auto-generated method stub
if(lNode!=null){
lNode.afterShow();
}
if(rNode!=null){
rNode.afterShow();
}
System.out.print(value+" ");
}
//前序查找
public TreeNode frontSearch(int i){
TreeNode target=null;
//对比当前节点的值
if(this.value==1){
return this;
//当前节点的值不是要查找的节点
}else{
//查找左儿子
if(lNode!=null){
//可能查找,也可能找不到,查不到,target还是一个null
target=lNode.frontSearch(i);
}
//如果不为空,说明在左儿子中已经找到
if(target!=null){
return target;
}
//查找右儿子
if(rNode!=null){
target=rNode.frontSearch(i);
}
}
//都找不到就返回
return null;
}
}
package BinaryTree;
public class BinaryTree {
TreeNode root;
//设置根节点
public TreeNode getRoot() {
return root;
}
//获取根节点
public void setRoot(TreeNode root) {
this.root = root;
}
public void fronShow() {
// TODO Auto-generated method stub
root.frontShow();
}
public void midShow() {
// TODO Auto-generated method stub
root.midShow();
}
public void afterShow() {
// TODO Auto-generated method stub
root.afterShow();
}
public TreeNode frontSearch(int i) {
// TODO Auto-generated method stub
return root.frontSearch(i);
}
}
## 删除节点
```java
package BinaryTree;
public class 创建二叉树 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个树(空树),具体请看二叉树的五种形态
BinaryTree binTree=new BinaryTree();
//创建一个根节点
TreeNode root=new TreeNode(1);
//把根节点赋值给树
binTree.setRoot(root);
//创建两个节点
TreeNode rootL=new TreeNode(2);
//把新创建的节点设置为根节点的子节点
root.setlNode(rootL);
//创建两个节点
TreeNode rootR=new TreeNode(3);
//把新创建的节点设置为根节点的子节点
root.setrNode(rootR);
//为第二层的左节点创建两个子节点
rootL.setlNode(new TreeNode(4));
rootL.setrNode(new TreeNode(5));
//为第二层的右节点创建两个子节点
rootR.setlNode(new TreeNode(6));
rootR.setrNode(new TreeNode(7));
//前序遍历
binTree.frontShow();
System.out.println();
//中序遍历
binTree.midShow();
System.out.println();
//后续遍历
binTree.afterShow();
System.out.println();
//前序查找
TreeNode result=binTree.frontSearch(3);
System.out.println(result==rootR);
//删除一个子树
binTree.delete(4);
binTree.frontShow();
}
}
package BinaryTree;
public class TreeNode {
int value;//节点的权
TreeNode lNode;//左儿子
TreeNode rNode;//右儿子
public TreeNode(int value){
this.value=value;
}
//设置左儿子
public void setlNode(TreeNode lNode){
this.lNode=lNode;
}
//设置右儿子
public void setrNode(TreeNode rNode){
this.rNode=rNode;
}
//前序遍历
public void frontShow() {
// TODO Auto-generated method stub
//先遍历当前节点的内容
System.out.print(value+" ");
//左节点
if(lNode!=null){
lNode.frontShow();
}
//右节点
if(rNode!=null){
rNode.frontShow();
}
}
public void midShow() {
// TODO Auto-generated method stub
//左节点
if(lNode!=null){
lNode.midShow();
}
System.out.print(value+" ");
if(rNode!=null){
rNode.midShow();
}
}
public void afterShow() {
// TODO Auto-generated method stub
if(lNode!=null){
lNode.afterShow();
}
if(rNode!=null){
rNode.afterShow();
}
System.out.print(value+" ");
}
//前序查找
public TreeNode frontSearch(int i){
TreeNode target=null;
//对比当前节点的值
if(this.value==1){
return this;
//当前节点的值不是要查找的节点
}else{
//查找左儿子
if(lNode!=null){
//可能查找,也可能找不到,查不到,target还是一个null
target=lNode.frontSearch(i);
}
//如果不为空,说明在左儿子中已经找到
if(target!=null){
return target;
}
//查找右儿子
if(rNode!=null){
target=rNode.frontSearch(i);
}
}
//都找不到就返回
return null;
}
//删除一棵子树
public void delete(int i) {
// TODO Auto-generated method stub
TreeNode parent=this;
//判断左儿子
if(parent.lNode!=null&&parent.lNode.value==i){
parent.lNode=null;
return;
}
//判断右儿子
if(parent.rNode!=null&&parent.rNode.value==i){
parent.rNode=null;
return;
}
//如果都不是,递归检查并删除左儿子
parent=lNode;
if(parent!=null){
parent.delete(i);
}
//递归检查并删除右儿子
parent=rNode;
if(parent!=null){
parent.delete(i);
}
}
}
package BinaryTree;
public class BinaryTree {
TreeNode root;
//设置根节点
public TreeNode getRoot() {
return root;
}
//获取根节点
public void setRoot(TreeNode root) {
this.root = root;
}
public void frontShow() {
// TODO Auto-generated method stub
if(root!=null){
root.frontShow();
}
}
public void midShow() {
// TODO Auto-generated method stub
if(root!=null){
root.midShow();
}
}
public void afterShow() {
// TODO Auto-generated method stub
if(root!=null){
root.afterShow();
}
}
public TreeNode frontSearch(int i) {
// TODO Auto-generated method stub
return root.frontSearch(i);
}
public void delete(int i) {
// TODO Auto-generated method stub
if(root.value==i){
root=null;
}else{
root.delete(i);
}
}
}