mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08:00
Optimize arrToTree function
in java, cpp, py, go, js, ts.
This commit is contained in:
115
codes/java/chapter_heap/my_heap.java
Normal file
115
codes/java/chapter_heap/my_heap.java
Normal file
@ -0,0 +1,115 @@
|
||||
/**
|
||||
* File: my_heap.java
|
||||
* Created Time: 2023-01-07
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_heap;
|
||||
|
||||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
class MaxHeap {
|
||||
private List<Integer> heap;
|
||||
|
||||
public MaxHeap() {
|
||||
heap = new ArrayList<>();
|
||||
}
|
||||
|
||||
public MaxHeap(List<Integer> nums) {
|
||||
// 将元素拷贝至堆中
|
||||
heap = new ArrayList<>(nums);
|
||||
// 堆化除叶结点外的其他所有结点
|
||||
for (int i = parent(size() - 1); i >= 0; i--) {
|
||||
heapify(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取左子结点 */
|
||||
private int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* 获取右子结点 */
|
||||
private int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* 获取父结点 */
|
||||
private int parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
|
||||
/* 交换元素 */
|
||||
private void swap(int i, int j) {
|
||||
int tmp = heap.get(i);
|
||||
heap.set(i, j);
|
||||
heap.set(j, tmp);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return heap.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
public int peek() {
|
||||
return heap.get(0);
|
||||
}
|
||||
|
||||
/* 元素入堆 */
|
||||
public void push(int val) {
|
||||
heap.add(val);
|
||||
|
||||
// 从底至顶堆化
|
||||
int i = size();
|
||||
while (true) {
|
||||
int p = parent(i);
|
||||
if (p < 0 || heap.get(i) > heap.get(p))
|
||||
break;
|
||||
swap(i, p);
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* 元素出堆 */
|
||||
public int poll() {
|
||||
// 判空处理
|
||||
if (isEmpty())
|
||||
throw new EmptyStackException();
|
||||
// 交换根结点与右下角(即最后一个)结点
|
||||
swap(0, size() - 1);
|
||||
// 删除结点
|
||||
int val = heap.remove(size() - 1);
|
||||
// 从顶至底堆化
|
||||
heapify(0);
|
||||
// 返回堆顶元素
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 从结点 i 开始,从顶至底堆化 */
|
||||
private void heapify(int i) {
|
||||
while (true) {
|
||||
// 判断结点 i, l, r 中的最大结点,记为 ma ;
|
||||
int l = left(i), r = right(i), ma = i;
|
||||
if (heap.get(l) > heap.get(ma)) ma = l;
|
||||
if (heap.get(r) > heap.get(ma)) ma = r;
|
||||
// 若结点 i 最大,则无需继续堆化,跳出
|
||||
if (ma == i) break;
|
||||
// 交换结点 i 与结点 max
|
||||
swap(i, ma);
|
||||
// 循环向下堆化
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class my_heap {
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -30,8 +30,7 @@ public class binary_tree_bfs {
|
||||
public static void main(String[] args) {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] {
|
||||
1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null });
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 1, 2, 3, 4, 5, 6, 7 });
|
||||
System.out.println("\n初始化二叉树\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
|
||||
@ -43,8 +43,7 @@ public class binary_tree_dfs {
|
||||
public static void main(String[] args) {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] {
|
||||
1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null});
|
||||
TreeNode root = TreeNode.arrToTree(new Integer[] { 1, null, 3, 4, 5 });
|
||||
System.out.println("\n初始化二叉树\n");
|
||||
PrintUtil.printTree(root);
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ package include;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
|
||||
class Trunk {
|
||||
Trunk prev;
|
||||
String str;
|
||||
@ -103,4 +104,11 @@ public class PrintUtil {
|
||||
System.out.println(kv.getKey() + " -> " + kv.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static void printHeap(PriorityQueue<Integer> queue) {
|
||||
Integer[] nums = (Integer[])queue.toArray();
|
||||
TreeNode root = TreeNode.arrToTree(nums);
|
||||
|
||||
printTree(root);
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +22,7 @@ public class TreeNode {
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree with an array
|
||||
* Generate a binary tree given an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
@ -32,19 +32,19 @@ public class TreeNode {
|
||||
|
||||
TreeNode root = new TreeNode(arr[0]);
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
int i = 1;
|
||||
int i = 0;
|
||||
while(!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
if (++i >= arr.length) break;
|
||||
if(arr[i] != null) {
|
||||
node.left = new TreeNode(arr[i]);
|
||||
queue.add(node.left);
|
||||
}
|
||||
i++;
|
||||
if (++i >= arr.length) break;
|
||||
if(arr[i] != null) {
|
||||
node.right = new TreeNode(arr[i]);
|
||||
queue.add(node.right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
@ -71,20 +71,4 @@ public class TreeNode {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tree node with specific value in a binary tree
|
||||
* @param root
|
||||
* @param val
|
||||
* @return
|
||||
*/
|
||||
public static TreeNode getTreeNode(TreeNode root, int val) {
|
||||
if (root == null)
|
||||
return null;
|
||||
if (root.val == val)
|
||||
return root;
|
||||
TreeNode left = getTreeNode(root.left, val);
|
||||
TreeNode right = getTreeNode(root.right, val);
|
||||
return left != null ? left : right;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user