堆排序和构建完全二叉树
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
class TreeNode {
int data;
TreeNode left;
TreeNode right;
public TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class CompleteBinaryTreeBuilder {
private TreeNode root;
private int[] values;
public CompleteBinaryTreeBuilder(int[] values) {
this.values = values;
this.root = buildTree(0);
}
private TreeNode buildTree(int index) {
if (index < values.length) {
TreeNode newNode = new TreeNode(values[index]);
newNode.left = buildTree(2 * index + 1);
newNode.right = buildTree(2 * index + 2);
return newNode;
}
return null;
}
public TreeNode getRoot() {
return root;
}
// 层次遍历输出二叉树
public static void query(TreeNode root) {
Deque<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
System.out.print(current.data + " ");
if (current.left != null)
queue.add(current.left);
if (current.right != null)
queue.add(current.right);
}
}
// 堆排序
public static void heapSort(int[] array) {
int n = array.length;
// 构建最大堆
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(array, n, i);
}
// 逐步取出堆顶元素(最大值),并调整堆
for (int i = n - 1; i > 0; i--) {
// 交换堆顶元素和当前末尾元素
int temp = array[0];
array[0] = array[i];
array[i] = temp;
// 调整堆,确保剩余部分仍然是最大堆
heapify(array, i, 0);
}
}
// 将数组表示的树调整为最大堆
private static void heapify(int[] array, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && array[left] > array[largest]) {
largest = left;
}
if (right < n && array[right] > array[largest]) {
largest = right;
}
if (largest != i) {
// 交换父节点和最大的子节点
int temp = array[i];
array[i] = array[largest];
array[largest] = temp;
// 递归调整交换后的子树
heapify(array, n, largest);
}
}
public static void main(String[] args) {
int[] array = {1, 8, -1, 3, 22, 0, -6};
CompleteBinaryTreeBuilder builder = new CompleteBinaryTreeBuilder(array);
TreeNode root = builder.getRoot();
System.out.println("Original Tree:");
query(root);
// 进行堆排序
heapSort(array);
System.out.println("\nSorted Array:");
for (int value : array) {
System.out.print(value + " ");
}
}
}
import java.util.ArrayList;
import java.util.Deque;
class TreeNode {
int data;
TreeNode left;
TreeNode right;
public TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class CompleteBinaryTreeBuilder {
private TreeNode root;
private int[] values;
public CompleteBinaryTreeBuilder(int[] values) {
this.values = values;
this.root = buildTree(0);
}
private TreeNode buildTree(int index) {
if (index < values.length) {
TreeNode newNode = new TreeNode(values[index]);
newNode.left = buildTree(2 * index + 1);
newNode.right = buildTree(2 * index + 2);
return newNode;
}
return null;
}
public TreeNode getRoot() {
return root;
}
// 层次遍历输出二叉树
public static void query(TreeNode root) {
Deque<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode current = queue.poll();
System.out.print(current.data + " ");
if (current.left != null)
queue.add(current.left);
if (current.right != null)
queue.add(current.right);
}
}
// 堆排序
public static void heapSort(int[] array) {
int n = array.length;
// 构建最大堆
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(array, n, i);
}
// 逐步取出堆顶元素(最大值),并调整堆
for (int i = n - 1; i > 0; i--) {
// 交换堆顶元素和当前末尾元素
int temp = array[0];
array[0] = array[i];
array[i] = temp;
// 调整堆,确保剩余部分仍然是最大堆
heapify(array, i, 0);
}
}
// 将数组表示的树调整为最大堆
private static void heapify(int[] array, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && array[left] > array[largest]) {
largest = left;
}
if (right < n && array[right] > array[largest]) {
largest = right;
}
if (largest != i) {
// 交换父节点和最大的子节点
int temp = array[i];
array[i] = array[largest];
array[largest] = temp;
// 递归调整交换后的子树
heapify(array, n, largest);
}
}
public static void main(String[] args) {
int[] array = {1, 8, -1, 3, 22, 0, -6};
CompleteBinaryTreeBuilder builder = new CompleteBinaryTreeBuilder(array);
TreeNode root = builder.getRoot();
System.out.println("Original Tree:");
query(root);
// 进行堆排序
heapSort(array);
System.out.println("\nSorted Array:");
for (int value : array) {
System.out.print(value + " ");
}
}
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17914796.html
千万千万赵千万