已知外部提供创建树所需要的数据。
方法1:按照从上到下从左到右依次把数据放在对应结点来创建树。
即数组中index处的数据在树中仍然是放在index处,而它的左右子结点分别是 2*index+1 , 2*index+2
-- BinaryTree.java
public class BinaryTree {
Node root;
int size; // 结点总个数
Object[] data;
// 内部类
private class Node{
private Node left;
private Node right;
private Object data;
public Node(Object data){
this.left = null;
this.right = null;
this.data = data;
}
}
public BinaryTree(Object[] data){
this.data = data;
size = data.length;
root = createTree(0);
}
// 创建下标为index处的结点及其子树,递归
public Node createTree(int index){
if (index >= size)
return null;
Node node = new Node(data[index]);
node.left = createTree(2*index+1);
node.right = createTree(2*index+2);
return node;
}
// 先序遍历
public void preShow(Node node) {
if(node!=null){
System.out.print(node.data + " ");
preShow(node.left);
preShow(node.right);
}
}
// 先序非递归
public void preShow(TreeNode root){
Stack<TreeNode> s = new Stack<>();
TreeNode p = root;
while(p!=null || !s.isEmpty()){
while(p!=null){
System.out.println(p.val);
s.push(p);
p=p.left;
}
p=s.pop();
p=p.right;
}
}
// 中序遍历
public void middleShow(Node node) {
if (node != null) {
middleShow(node.left);
System.out.print(node.data + " ");
middleShow(node.right);
}
}
// 中序非递归
public void midShow(TreeNode root){
Stack<TreeNode> s = new Stack<>();
TreeNode p = root;
while(p!=null || !s.isEmpty()){
while(P!=null){
s.push(p);
p=p.left;
}
p=s.pop();
System.out.println(p.val);
p=p.right;
}
}
// 后序遍历
public void backShow(Node node) {
if (node != null) {
middleShow(node.left);
middleShow(node.right);
System.out.print(node.data + " ");
}
}
// 层次遍历
public void levelShow(Node node){
Queue<Node> queue = new LinkedList<>();
if(node == null)
return;
queue.add(node);
while(!queue.isEmpty()){
Node n = queue.poll();
System.out.print(n.data+" ");
if(n.left!=null)
queue.offer(n.left);
if(n.right!=null)
queue.offer(n.right);
}
}
// 求树的深度
public int getDepth(Node node){
if(node==null)
return 0;
return getDepth(node.left)>getDepth(node.right)?getDepth(node.left)+1:getDepth(node.right)+1;
}
// 判断是否平衡二叉树
// 网友方法,只要有个结点不平衡,result就会变为false
private boolean result = true;
public boolean isBalanced(TreeNode root) {
maxDepth(root);
return result;
}
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
int l = maxDepth(root.left);
int r = maxDepth(root.right);
if (Math.abs(l - r) > 1)
result = false;
return 1 + Math.max(l, r);
}
// 树中序遍历时是否是升序的?
public boolean isValidBST(TreeNode root) {
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
public boolean isValidBST(TreeNode root, long minVal, long maxVal) {
if (root == null) return true;
if (root.val >= maxVal || root.val <= minVal) return false;
return isValidBST(root.left, minVal, root.val) && isValidBST(root.right, root.val, maxVal);
}
public static void main(String[] args) {
Object[] my = new Object[]{'1', '2', '3', '4', '0', '5', '6', '7', '8', '0', '0', '9', 'A'};
BinaryTree tree = new BinaryTree(my);
tree.preShow(tree.root);
System.out.println();
tree.middleShow(tree.root);
}
}
法2:采用先序遍历创建二叉树
-- BinaryTree2.java
public class BinaryTree2 {
Node root;
int size; // 结点总个数
Object[] data;
int index; //表示取到数组中下标为index的元素了
// 内部类
private class Node{
private Node left;
private Node right;
private Object data;
public Node(Object data){
this.left = null;
this.right = null;
this.data = data;
}
}
public BinaryTree2(Object[] data){
size = data.length;
this.data = data;
index = 0;
root = createTree();
}
// 按照先序遍历创造树
public Node createTree(){
if(index>=size)
return null;
Object o = data[index++];
Node node = new Node(o);
node.left = createTree();
node.right = createTree();
return node;
}
// 先序遍历
public void preShow(Node node) {
if(node!=null){
System.out.print(node.data + " ");
preShow(node.left);
preShow(node.right);
}
}
// 中序遍历
public void middleShow(Node node) {
if (node != null) {
middleShow(node.left);
System.out.print(node.data + " ");
middleShow(node.right);
}
}
// 后序遍历
public void backShow(Node node) {
if (node != null) {
middleShow(node.left);
middleShow(node.right);
System.out.print(node.data + " ");
}
}
public static void main(String[] args) {
Object[] my = new Object[]{'1', '2', '3', '4', '0', '5', '6', '7', '8', '0', '0', '9', 'A'};
BinaryTree2 tree = new BinaryTree2(my);
tree.preShow(tree.root);
System.out.println();
tree.middleShow(tree.root);
}
}