二叉树的基础
今天学习了二叉树,有点膨胀,赶紧写个博客记下来,不然又忘记了。
直接在代码里面解释吧。。。
package BinaryTreeText;
import java.util.Stack;
/**
* 二叉树
* */
public class BinaryTree {
// 根节点
private TreeNode root;
// 二叉树的节点域
public class TreeNode{
private int index;
private String data; // 节点的数据域
private TreeNode leftChild; //相当于节点的指针域
private TreeNode rifhtChild;
public TreeNode(int index, String data){
this.index = index;
this.data = data;
this.leftChild = null;
this.rifhtChild = null;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
/**
* // 初始化有一个根节点
* */
public BinaryTree(){
root = new TreeNode(1,"A");
root.leftChild = null;
root.rifhtChild = null;
}
/** // 二叉树的构建
* A
* b C
* d E F
* */
public void createBinaryTree(){
TreeNode nodeB = new TreeNode(2,"B");
TreeNode nodeC = new TreeNode(3,"C");
TreeNode nodeD = new TreeNode(4,"D");
TreeNode nodeE = new TreeNode(5,"E");
TreeNode nodeF = new TreeNode(6,"F");
root.leftChild = nodeB;
root.rifhtChild = nodeC;
nodeB.leftChild = nodeD;
nodeB.rifhtChild = nodeE;
nodeC.rifhtChild = nodeF;
}
/**
* // 求二叉树的高度
* */
public int getHigh(){
return getHigh(root);
}
public int getHigh(TreeNode root){
if (null == root){
return 0;
}else{
// 求子节点的高度--> 左子树和右子树 -->选出高度最高的为整个二叉树的高度
int i = getHigh(root.leftChild);
int j = getHigh(root.rifhtChild);
return ((i>j)?(i+1):(j+1));
}
}
/**
*
* 求二叉树的节点个数
*/
public int getSize(){
return getSize(root);
}
public int getSize(TreeNode root){
if (null == root){
return 0;
}else{
return 1+ getSize(root.leftChild) + getSize(root.rifhtChild);
}
}
/**
*
* 二叉树的遍历
*
* */
/**
*
* 前序遍历 -->根,左,右(迭代)
* */
public void preOrder(){
preOrder(root);
}
public void preOrder(TreeNode root){
if(null == root){
return ;
}else{
System.out.println(root.data);
preOrder(root.leftChild);
preOrder(root.rifhtChild);
}
}
/**
* 前序遍历非迭代
*
* */
public void nonRecOrder(){
nonRecOrder(root);
}
public void nonRecOrder(TreeNode root){
// 采用栈的方法
// 出去一个根节点,进去两个子节点
if (null==root){
return;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode t = stack.pop();
System.out.println(t.data);
// 此处的入栈是有顺序的
/*
* 因为是前序遍历二叉树,(根,左,右)
* 而栈 FILO 的结构,所以得先让右儿子压栈
* 这样左儿子才能先出栈
* */
if (t.rifhtChild!=null){
stack.push(t.rifhtChild);
}
if(t.leftChild!=null){
stack.push(t.leftChild);
}
}
}
/**
* 中序遍历-->左,根,右
*
* */
public void midOrder(){
midOrder(root);
}
public void midOrder(TreeNode root){
if (null == root){
return;
}else{
midOrder(root.leftChild);
System.out.println(root.data);
midOrder(root.rifhtChild);
}
}
/**
* 后序遍历 左右根
* */
public void postOrder(){
postOrder(root);
}
public void postOrder(TreeNode root){
if (null == root){
return;
}else{
postOrder(root.leftChild);
postOrder(root.rifhtChild);
System.out.println(root.data);
}
}
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
binaryTree.createBinaryTree();
int high = binaryTree.getHigh();
System.out.println(high);
int size = binaryTree.getSize();
System.out.println(size);
System.out.println("前序遍历");
binaryTree.preOrder();
System.out.println("前序遍历(非迭代)");
binaryTree.nonRecOrder();
System.out.println("中序遍历");
binaryTree.midOrder();
System.out.println("后序遍历");
binaryTree.postOrder();
}
}
就这些了。。。