一、二叉树:
二叉树是每个结点最多有两个子树的树结构,通常子树被称为“左子树”和“右子树”。
二叉树结构定义如下:
public class TreeEntity {
private int data;
private TreeEntity left;
private TreeEntity right;
public int getData() {
return data;
}
public TreeEntity getLeft() {
return left;
}
public TreeEntity getRight() {
return right;
}
public void setData(int data) {
this.data = data;
}
public void setLeft(TreeEntity left) {
this.left = left;
}
public void setRight(TreeEntity right) {
this.right = right;
}
public TreeEntity(int data, TreeEntity left, TreeEntity right) {
this.data = data;
this.left = left;
this.right = right;
}
public TreeEntity(int data) {
this(data, null, null);
}
}
二、二叉搜索树:
又叫二叉排序树、二叉查找树,其定义:
若其左子树存在,左子树中每一个值都不大于该节点值;
若其右子树存在,右子树中每一个值都不小于该节点值;
每个子树都是二叉排序树。
判断一棵树是否为二叉排序树,两种方式实现,代码如下:
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/*二叉排序树:
若其左子树存在,左子树中每一个值都不大于该节点值;
若其右子树存在,右子树中每一个值都不小于该节点值;
每个子树都是二叉排序树
*/
//判断一棵树是否为二叉排序树
public class BinarySearchTree {
//方法一:利用栈逐个遍历
public static boolean isValidBST(TreeEntity root) {
if (root == null) {
return true;
}
Stack<TreeEntity> stack = new Stack<>();
TreeEntity pre = null;
while (root != null || !stack.isEmpty()) {
//先得到所有的左节点
while (root != null) {
stack.push(root);
root = root.getLeft();
}
root = stack.pop();
//当中序遍历前一个节点的值大于后一节点值的时候
if(pre != null && root.getData() <= pre.getData()) {
return false;
}
pre = root;
root = root.getRight();
}
return true;
}
public static List<Integer> list=new ArrayList<>();
//方法二:1、中序遍历该树;2、得到的序列递增
public static List<Integer> inOrder(TreeEntity tree){
if(tree.getLeft()!=null){
inOrder(tree.getLeft());
}
list.add(tree.getData());
if(tree.getRight()!=null){
inOrder(tree.getRight());
}
return list;
}
public static boolean isValidBST2(List<Integer> list){
boolean isBST=true;
if(list==null){
return isBST;
}else {
for(int i=0;i<list.size()-1;i++){
if(list.get(i)>=list.get(i+1)){
isBST=false;
break;
}
}
return isBST;
}
}
}
三、将数组创建为二叉树、用递归法前序中序后序遍历二叉树,
代码如下:
import java.util.ArrayList;
import java.util.List;
public class TreeMethod {
public static List<Integer> preList=new ArrayList<>();
public static List<Integer> inList=new ArrayList<>();
public static List<Integer> laterList=new ArrayList<>();
//先序遍历
public static List<Integer> preOrder(TreeEntity tree){
if(tree!=null){
preList.add(tree.getData());
if(tree.getLeft()!=null){
preOrder(tree.getLeft());
}
if(tree.getRight()!=null){
preOrder(tree.getRight());
}
}
return preList;
}
//中序遍历
public static List<Integer> inOrder(TreeEntity tree){
if(tree!=null){
if(tree.getLeft()!=null){
inOrder(tree.getLeft());
}
inList.add(tree.getData());
if(tree.getRight()!=null){
inOrder(tree.getRight());
}
}
return inList;
}
//后序遍历
public static List<Integer> laterOrder(TreeEntity tree){
if(tree!=null){
if(tree.getLeft()!=null){
laterOrder(tree.getLeft());
}
if(tree.getRight()!=null){
laterOrder(tree.getRight());
}
laterList.add(tree.getData());
}
return laterList;
}
//将数组创建成二叉树
public List<TreeEntity> createBinTree(int[] arr){
//存节点
List<TreeEntity> nodeList=new ArrayList<>();
//将数组的值依次转化为TreeEntity节点
for(int nodeIndex=0;nodeIndex<arr.length;nodeIndex++){
nodeList.add(new TreeEntity(arr[nodeIndex]));
}
//根节点
TreeEntity tree=nodeList.get(0);
for(int i=0;i<arr.length/2;i++){
nodeList.get(i).setLeft(nodeList.get(i*2+1));
if(i*2+2<nodeList.size()){
nodeList.get(i).setRight(nodeList.get(i*2+2));
}
}
return nodeList;
}
// public static void main(String args[]){
// TreeMethod treeMethod=new TreeMethod();
// int[] arr={20,15,30,11,19,25,60,1,14};
// List<TreeEntity> binTree = treeMethod.createBinTree(arr);
// List<Integer> list = preOrder(binTree.get(0));
// for(int i=0;i<list.size();i++){
// System.out.print(list.get(i)+" ");
// }
// }
}
//先序:20 15 11 1 14 19 30 25 60
//中序:1 11 14 15 19 20 25 30 60
//后续:1 14 11 19 15 25 60 30 20
四、通过二叉树的前序、中序序列得到后序序列。
前序序列、中序序列生成二叉树的代码如下:
import java.util.ArrayList;
import java.util.List;
import static package2.TreeMethod.inOrder;
import static package2.TreeMethod.laterOrder;
public class TreeBuild {
public static ArrayList list = new ArrayList();
public static TreeEntity reConstructBinaryTree(int[] pre, int[] in) {
TreeEntity root = buildBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
return root;
}
private static TreeEntity buildBinaryTree(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn) {
if (startPre > endPre || startIn > endIn) {
return null;
}
TreeEntity root = new TreeEntity(pre[startPre]);
for (int i = startIn; i <= endIn; i++)
if (in[i] == pre[startPre]) {
root.setLeft(buildBinaryTree(pre, startPre + 1, startPre + i - startIn, in, startIn, i - 1));
root.setRight(buildBinaryTree(pre, i - startIn + startPre + 1, endPre, in, i + 1, endIn));
break;
}
return root;
}
// public static void main(String[] args) {
// int[] pre = {20,15,11,1,14,19,30,25,60};
// int[] in = {1,11,14,15,19,20,25,30,60};
// TreeEntity treeEntity = reConstructBinaryTree(pre, in);
// List<Integer> list1 = laterOrder(treeEntity);
// for(int i=0;i<list1.size();i++){
// System.out.print(list1.get(i)+" ");
// }
// }
}