根据给定的数组或链表构建完全二叉树(Java)

import java.util.*;

/**
 * 根据二叉树的性质(将一个完全二叉树按照从上到下,从左到右进行编号,其编号为i的节点,
 * 如果满足2*i<=n,则说明编号为i的节点有左孩子,否则没有,如果满足2*i+1<=n,
 * 则说明编号为i的节点有右孩子,否则没有)可知     


2*i<=n


2*i+1<=n


该性质是对树的编号(1~n)成立的,而数组的下标是从0到n-1,将对应下标都减1可知,对于数组0~n-1来说,


2*i-1<=n-1


2*i<=n-1是成立的。
 */
import java.util.*;
public class 根据数组构建完全二叉树 {
private static class BinaryTreeNode {
private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;


public BinaryTreeNode(int e) {
this.data = e;
}


public BinaryTreeNode() {
// TODO Auto-generated constructor stub
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int[] arr = new int[] {1,2,3,4,5,6,7,8,9,10};

BinaryTreeNode root = createTree(arr);

System.out.println("-leverTraverse----preOrder-----inOrder");
levelTraverse(root);
System.out.println();
preOrder(root);
System.out.println();
inOrder(root);
}
// 根据数组创建完全二叉树
private static BinaryTreeNode createTree(int[] arr) {
if(arr.length==1)
return new BinaryTreeNode(arr[0]);
List<BinaryTreeNode> nodeList = new ArrayList<>();
for(int i=0;i<arr.length;i++)
nodeList.add(new BinaryTreeNode(arr[i]));

for(int i=1;i<=arr.length/2;i++) {
if(2*i-1<=arr.length-1)
nodeList.get(i-1).left = nodeList.get(2*i-1);
if(2*i<=arr.length-1)
nodeList.get(i-1).right = nodeList.get(2*i);
}
return nodeList.get(0);
}


static void preOrder(BinaryTreeNode root) {
if (root == null)
return;
else {
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}


static void inOrder(BinaryTreeNode root) {
if (root == null)
return;
else {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}


private static void levelTraverse(BinaryTreeNode root) {
BinaryTreeNode temp;
Queue<BinaryTreeNode> queue = new LinkedList<>();
if (root == null)
return;
queue.offer(root);
while ((temp = queue.poll())!=null) {
System.out.print(temp.data + " ");
if (temp.left != null)
queue.offer(temp.left);
if (temp.left != null)
queue.offer(temp.right);
}
queue.clear();
}
}
public class 根据给定链表构建完全二叉树 {
private static class BinaryTreeNode {
private int data;
private BinaryTreeNode left;
private BinaryTreeNode right;


public BinaryTreeNode(int e) {
this.data = e;
}


public BinaryTreeNode() {
// TODO Auto-generated constructor stub
}
}


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
List<BinaryTreeNode> list = new ArrayList<>();
for (int i = 1; i <= 8; i++) {
list.add(new BinaryTreeNode(i));
}
BinaryTreeNode root = createTree(list);


System.out.println("leverTraverse----preOrder-----inOrder----postOrder");
levelTraverse(root);
System.out.println();
preOrder(root);
System.out.println();
inOrder(root);
System.out.println();
postOrder(root);
}


private static BinaryTreeNode createTree(List<BinaryTreeNode> list) {
if (list.size() == 1)
return list.get(0);
for (int i = 1; i <= list.size() / 2; i++) {
if (2 * i - 1 <= list.size() - 1)
list.get(i - 1).left = list.get(2 * i - 1);
if (2 * i <= list.size() - 1)
list.get(i - 1).right = list.get(2 * i);
}
return list.get(0);
}


static void preOrder(BinaryTreeNode root) {
if (root == null)
return;
else {
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}


static void inOrder(BinaryTreeNode root) {
if (root == null)
return;
else {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}

static void postOrder(BinaryTreeNode root) {
if (root == null)
return;
else {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
}
private static void levelTraverse(BinaryTreeNode root) {
BinaryTreeNode temp;
Queue<BinaryTreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty())
        {
            temp = queue.poll();
            System.out.print(temp.data+" ");
            if(temp.left != null)
            {
                queue.offer(temp.left);
            }
            if(temp.right != null)
            {
                queue.offer(temp.right);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/tan915730/article/details/79367747