Java数据结构函数-包括常见数据结构的实现函数,如堆、链表、二叉树等。
发布时间:2023-06-12 05:37:39
一、堆
1.1.建堆:将无序的数组转化为一个堆的过程。
public void buildHeap(int[] nums) {
int len = nums.length;
for (int i = (len - 2) / 2; i >= 0; i--) {
heapify(nums, len, i);
}
}
1.2.堆化:保持堆的特性。
public void heapify(int[] nums, int len, int index) {
int left = 2 * index + 1;
int right = 2 * index + 2;
int largest = index;
if (left < len && nums[left] > nums[largest]) {
largest = left;
}
if (right < len && nums[right] > nums[largest]) {
largest = right;
}
if (largest != index) {
swap(nums, largest, index);
heapify(nums, len, largest);
}
}
1.3.堆的插入:将新元素插入堆中。
public void insert(int val) {
nums[size++] = val;
int index = size - 1;
while (index > 0 && nums[(index - 1) / 2] < nums[index]) {
swap(nums, (index - 1) / 2, index);
index = (index - 1) / 2;
}
}
1.4.堆的删除:删除堆中的最大元素。
public int deleteMax() {
int maxVal = nums[0];
nums[0] = nums[--size];
heapify(nums, size, 0);
return maxVal;
}
二、链表
2.1.链表的创建:通过在链表末尾逐个添加节点来创建链表。
public ListNode createLinkedList(int[] nums) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
for (int num : nums) {
cur.next = new ListNode(num);
cur = cur.next;
}
return dummy.next;
}
2.2.链表的遍历:通过循环遍历链表的每个节点。
public void traverse(ListNode head) {
ListNode cur = head;
while (cur != null) {
System.out.print(cur.val + "->");
cur = cur.next;
}
System.out.println("null");
}
2.3.链表的反转:将链表的指针逆序。
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
2.4.链表的合并:将两个有序链表合并为一个有序链表。
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
三、二叉树
3.1.二叉树的创建:通过递归地创建左右子树来创建一颗二叉树。
public TreeNode createTree(int[] nums, int i) {
if (i >= nums.length || nums[i] == null) {
return null;
}
TreeNode root = new TreeNode(nums[i]);
root.left = createTree(nums, 2 * i + 1);
root.right = createTree(nums, 2 * i + 2);
return root;
}
3.2.二叉树的遍历:通过递归依次访问二叉树节点,分为先序遍历、中序遍历和后序遍历。
public void preorderTraversal(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.val + " ");
preorderTraversal(root.left);
preorderTraversal(root.right);
}
public void inorderTraversal(TreeNode root) {
if (root == null) {
return;
}
inorderTraversal(root.left);
System.out.print(root.val + " ");
inorderTraversal(root.right);
}
public void postorderTraversal(TreeNode root) {
if (root == null) {
return;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
System.out.print(root.val + " ");
}
3.3.二叉树的层次遍历:使用队列实现二叉树的层次遍历。
public void levelTraversal(TreeNode root) {
if (root == null) {
return;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int levelSize = queue.size();
for (int i = 0; i < levelSize; i++) {
TreeNode node = queue.poll();
System.out.print(node.val + " ");
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
}
3.4.求二叉树的深度:递归求解二叉树的深度。
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return Math.max(leftDepth, rightDepth) + 1;
}
以上就是一些常见数据结构的实现函数。无论是在面试还是实际开发中,对这些数据结构的实现函数掌握得越熟练,就越能写出高效,健壮性好的代码。
