二叉树的创建(数组转二叉树) java
程序员文章站
2022-05-19 22:09:05
...
将一个数组转换成一个二叉树的java实现,如下:
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* 二叉树的生成
*
* @author DangerShi
*/
public class BTreeBuilder {
class TreeNode {
Object data;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(Object data, TreeNode left, TreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
}
public TreeNode arrayToBTree(Object[] arrs) {
if (arrs == null || arrs.length == 0) {
return new TreeNode();
}
List<TreeNode> nodes = new ArrayList<>(arrs.length);
for (Object obj : arrs) {
TreeNode treeNode = new TreeNode();
treeNode.data = obj;
nodes.add(treeNode);
}
for (int i = 0; i < arrs.length/2 - 1; i++) {
TreeNode node = nodes.get(i);
node.left = nodes.get(i*2 + 1);
node.right = nodes.get(i*2 + 2);
}
// 只有当总节点数是奇数时,最后一个父节点才有右子节点
int lastPNodeIndex = arrs.length/2 - 1;
TreeNode lastPNode = nodes.get(lastPNodeIndex);
lastPNode.left = nodes.get(lastPNodeIndex*2 + 1);
if (arrs.length%2 != 0) {
lastPNode.right = nodes.get(lastPNodeIndex*2 + 2);
}
return nodes.get(0);
}
@Test
public void test() {
/**
* 1
* 2 3
* 4 5 6 7
*/
TreeNode treeNode = arrayToBTree(new Object[]{1,2,3,4,5,6,7});
}
}