乐趣区

关于java:JAVA二叉树的基本操作

目录
记录二叉树的基本操作 DEMO
1、创立一个二叉树类
2、而后创立二叉树的节点
记录二叉树的基本操作 DEMO
1、创立一个二叉树类
这里束缚了泛型只能为实现了 Comparable 这个接口的类型。

/**
 * @author JackHui
 * @version BinaryTree.java, 2020 年 03 月 05 日 12:45
 */
public class BinaryTree<T extends Comparable> {
 
    // 树根
    BinaryTreeNode root;
 
    public boolean deleteData(T data) {if (root.data.equals(data)) {
            root = null;
            return true;
        }
        return root.deleteNode(data);
    }
 
    public T frontSearch(T data) {return (T) root.frontSearch(data);
    }
 
    public T midSearch(T data) {return (T) root.midSearch(data);
    }
 
    public T rearSearch(T data) {return (T) root.rearSearch(data);
    }
 
    public void frontEach() {this.root.frontEach();
    }
 
    public void midEach() {this.root.midEach();
    }
 
    public void rearEach() {this.root.rearEach();
    }
 
    public BinaryTreeNode getRoot() {return root;}
 
    public void setRoot(BinaryTreeNode root) {this.root = root;}
}

2、而后创立二叉树的节点

package binarytree;
 
/**
 * @author JackHui
 * @version BinaryTreeNode.java, 2020 年 03 月 06 日 10:24
 */
public class BinaryTreeNode<T extends Comparable> {
    T data;
    BinaryTreeNode lChild;
    BinaryTreeNode rChild;
 
    public BinaryTreeNode(T data) {this.data = data;}
 
    // 先序遍历
    public void frontEach() {System.out.print(this.data + "\t");
        if (lChild != null) {lChild.frontEach();
        }
        if (rChild != null) {rChild.frontEach();
        }
    }
 
    // 中序遍历
    public void midEach() {if (lChild != null) {lChild.frontEach();
        }
        System.out.print(this.data + "\t");
        if (rChild != null) {rChild.frontEach();
        }
    }
 
    // 后序遍历
    public void rearEach() {if (lChild != null) {lChild.frontEach();
        }
        if (rChild != null) {rChild.frontEach();
        }
        System.out.print(this.data + "\t");
    }
 
    // 先序查找
    public T frontSearch(T data) {
        T target = null;
        System.out.println("[先序遍历]以后遍历到的元素:" + this.data + "\t 查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {return data;} else {if (lChild != null && (target = (T) lChild.frontSearch(data)) != null) {return target;}
            if (rChild != null && (target = (T) rChild.frontSearch(data)) != null) {return target;}
        }
        return target;
    }
 
    // 中序查找
    public T midSearch(T data) {
        T target = null;
        if (lChild != null && (target = (T) lChild.midSearch(data)) != null) {return target;}
        System.out.println("[中序遍历]以后遍历到的元素:" + this.data + "\t 查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {return data;} else {if (rChild != null && (target = (T) rChild.midSearch(data)) != null) {return target;}
        }
        return target;
    }
 
    // 后序查找
    public T rearSearch(T data) {
        T target = null;
        if (lChild != null && (target = (T) lChild.rearSearch(data)) != null) {return target;}
        if (rChild != null && (target = (T) rChild.rearSearch(data)) != null) {return target;}
        System.out.println("[后续遍历]以后遍历到的元素:" + this.data + "\t 查找的元素:" + data + "\t" + (this.data.compareTo(data) == 0 ? "查找到元素:" + data : ""));
        if (this.data.compareTo(data) == 0) {return data;}
        return target;
    }
 
    // 依据值删除节点
    public boolean deleteNode(T data) {System.out.println("[节点删除]以后遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data);
        // 判断左子树是否匹配
        if (this.lChild != null && (this.lChild.data.compareTo(data) == 0)) {System.out.println("[节点删除]以后遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t 节点删除胜利!");
            this.lChild = null;
            return true;
        } else if (this.rChild != null && (this.rChild.data.compareTo(data) == 0)) {System.out.println("[节点删除]以后遍历到的父节点:" + this.data + "\t" + "匹配的节点数据:" + data + "\t 节点删除胜利!");
            this.rChild = null;
            return true;
        }
        if (this.lChild != null && this.lChild.deleteNode(data)) {return true;}
        if (this.rChild != null && this.rChild.deleteNode(data)) {return true;}
        return false;
    }
 
    public T getData() {return data;}
 
    public void setData(T data) {this.data = data;}
 
    public BinaryTreeNode getlChild() {return lChild;}
 
    public void setlChild(BinaryTreeNode lChild) {this.lChild = lChild;}
 
    public BinaryTreeNode getrChild() {return rChild;}
 
    public void setrChild(BinaryTreeNode rChild) {this.rChild = rChild;}
}

到此这篇对于 JAVA 二叉树的基本操作 DEMO 的文章就介绍到这了

退出移动版