2025-01-20 完全二叉树排序
This commit is contained in:
parent
cc4a8c06dc
commit
d9355923ec
53
src/dataANDcalc/java/tree/CompleteBinaryTree.java
Normal file
53
src/dataANDcalc/java/tree/CompleteBinaryTree.java
Normal file
@ -0,0 +1,53 @@
|
||||
package tree;
|
||||
|
||||
public class CompleteBinaryTree {
|
||||
|
||||
private int[] arr;
|
||||
|
||||
public CompleteBinaryTree(int[] arr) {
|
||||
this.arr = arr;
|
||||
}
|
||||
|
||||
private void fowardSortArr(int index){
|
||||
if(arr.length == 0){
|
||||
System.out.println("数组为空");
|
||||
}
|
||||
|
||||
System.out.println("当前节点为:"+index+", 数据:"+arr[index]);
|
||||
if (index * 2 + 1 < arr.length){
|
||||
fowardSortArr(index * 2 + 1);
|
||||
}
|
||||
if (index * 2 + 2 < arr.length){
|
||||
fowardSortArr(index * 2 + 2);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CompleteBinaryTree completeBinaryTree = new CompleteBinaryTree(new int[]{1,2,3,4,5,6,7});
|
||||
completeBinaryTree.fowardSortArr(0);
|
||||
}
|
||||
|
||||
public static class TreeNode{
|
||||
private int node;
|
||||
|
||||
private int data;
|
||||
|
||||
private TreeNode left;
|
||||
|
||||
private TreeNode right;
|
||||
|
||||
public TreeNode(int node) {
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TreeNode{" +
|
||||
"node=" + node +
|
||||
"data=" + data +
|
||||
", left=" + (left == null ? "null" : left.node) +
|
||||
", right=" + (right == null ? "null" : right.node) +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user