前言
一、二叉树
1.结点创建
(1)包括左孩子和右孩子;
代码如下(示例):
class TreeNode{
private int no;
private String node;
private TreeNode left;
private TreeNode right;
public TreeNode(int no, String node) {
this.no = no;
this.node = node;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
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;
}
@Override
public String toString() {
return "TreeNode{" +
"no=" + no +
", node='" + node + '\'' +
'}';
}
}
2.二叉树创建
代码如下(示例):
class BinaryTree{
private TreeNode root;
public BinaryTree(TreeNode root) {
this.root = root;
}
//前序遍历
public void preOrder(){
if(this.root != null){
this.root.preOrder();
}else {
System.out.println("二叉树为空");
}
}
//中序遍历
public void infixOrder(){
if(this.root != null){
this.root.infixOrder();
}else {
System.out.println("二叉树为空");
}
}
//后序遍历
public void postOrder(){
if(this.root != null){
this.root.postOrder();
}else {
System.out.println("二叉树为空");
}
}
// 前序查找
public TreeNode preOrderSearch(int no){
if(root != null){
return root.preOrederSearch(no);
}else {
return null;
}
}
// 中序查找
public TreeNode infixOrderSearch(int no){
if(root != null){
return root.infixOrderSearch(no);
}else {
return null;
}
}
// 后续查找
public TreeNode postOrderSearch(int no){
if(root != null){
return root.postOrderSearch(no);
}else {
return null;
}
}
// 删除(规定:如果是叶子节点,则直接删除;如果不是叶子节点,则直接删除那颗子树)
public void del1(int no){
if(root == null || root.getNo() == no){
root = null;
return;
}else {
this.root.del1(no);
}
}
//删除
public void del2(int no){
if(this.root == null){
return;
}else if(this.root.getLeft() != null && this.root.getNo() == no){
TreeNode temp = this.root.getRight();
this.root = this.root.getLeft();
this.root.setRight(temp);
}else if(this.root.getRight() != null && this.root.getNo() == no){
this.root = this.root.getRight();
}else {
this.root.del2(no);
}
}
}
二、功能实现
1.遍历(前序、中序、后序)
代码如下(示例):
// 前序遍历
public void preOrder(){
System.out.println(this);
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(this);
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(this);
}
2.查找(前序、中序、后序)
代码如下(示例):
// 前序查找
public TreeNode preOrederSearch(int no){
TreeNode resNode = null;
System.out.println("进入前序遍历查找:");
if(this.no == no){
return this;
}
if(this.left != null){
resNode = this.left.preOrederSearch(no);
}
if(resNode != null){
return resNode;
}
if(this.right != null){
resNode = this.right.preOrederSearch(no);
}
if(resNode != null){
return resNode;
}
return resNode;
}
//中序查找
public TreeNode infixOrderSearch(int no){
TreeNode resNode = null;
if(this.left != null){
resNode = this.left.infixOrderSearch(no);
}
if(resNode != null){
return resNode;
}
System.out.println("进入中序遍历查找:");
if(this.no == no){
return this;
}
if(this.right != null){
resNode = this.right.infixOrderSearch(no);
}
if(resNode != null){
return resNode;
}else {
if(this.no == no){
return this;
}
}
return resNode;
}
//后序查找
public TreeNode postOrderSearch(int no){
TreeNode resNode = null;
if(this.left != null){
resNode = this.left.postOrderSearch(no);
}
if(resNode != null){
return resNode;
}
if(this.right != null){
resNode = this.right.postOrderSearch(no);
}
if(resNode != null){
return resNode;
}
System.out.println("进入后序遍历查找:");
if(this.no == no){
return this;
}
return resNode;
}
3.删除
代码如下(示例):
// 删除节点(规定:如果是叶子节点,则直接删除;如果不是叶子节点,则直接删除那颗子树)
public void del1(int no){
if(this.left != null && this.left.no == no){
this.setLeft(null);
}
if(this.right != null && this.right.no == no){
this.setRight(null);
}
if(this.left != null){
this.left.del1(no);
}
if(this.right != null){
this.right.del1(no);
}
}
// 删除节点(规定:非空叶子节点删除时若是只有一个子节点,则让子节点代替他,若是有两个则让左子节点代替他
public void del2(int no){
if(this.left.left != null && this.left.no == no){
TreeNode temp = this.left.getRight();
this.setLeft(this.left.left);
this.left.setRight(temp);
}
if(this.left.right != null && this.left.no == no){
this.setLeft(this.left.right);
}
if(this.left.no == no){
this.setLeft(null);
}
if(this.right.left != null && this.right.no == no){
TreeNode temp = this.right.getRight();
this.setRight(this.right.left);
this.right.setRight(temp);
}
if(this.right.right != null && this.right.no == no){
this.setRight(this.right.right);
}
if(this.right.no == no){
this.setRight(null);
}
}