常见数据结构

常见数据结构

数组

  • 由相同类型的元素组成的有序集合。
  • 元素通过索引访问,索引通常从0开始。
  • 连续的内存空间存储元素。
public class ArrayExample {
    public static void main(String[] args) {
        // 创建一个整型数组
        int[] array = new int[5];
        
        // 初始化数组元素
        for (int i = 0; i < array.length; i++) {
            array[i] = i * 2;
        }
        
        // 访问数组元素并打印
        for (int i = 0; i < array.length; i++) {
            System.out.println("数组元素 " + i + ": " + array[i]);
        }
    }
}

链表

  • 由节点组成,每个节点包含数据和指向下一个节点的指针。
  • 分为单向链表和双向链表。
  • 灵活插入和删除元素,但访问元素需要遍历。
public class ListNode {
    int val;
    ListNode next;
    
    public ListNode(int val) {
        this.val = val;
    }
}

public class LinkedListExample {
    public static void main(String[] args) {
        // 创建链表节点
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        
        // 遍历链表并打印节点值
        ListNode current = head;
        while (current != null) {
            System.out.println("节点值: " + current.val);
            current = current.next;
        }
    }
}

双向链表

// 双向链表节点类
class Node {
    int data;
    Node prev;
    Node next;

    public Node(int data) {
        this.data = data;
        this.prev = null;
        this.next = null;
    }
}

// 双向链表类
class DoublyLinkedList {
    Node head;
    Node tail;

    public DoublyLinkedList() {
        this.head = null;
        this.tail = null;
    }

    // 在链表末尾添加节点
    public void append(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
    }

    // 在链表头部添加节点
    public void prepend(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }
    }
}

  • 后进先出 (LIFO) 的数据结构。
  • 只能在栈顶进行插入和删除操作。
  • 常用于表达式求值、函数调用等。
import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        
        // 将元素推入栈
        stack.push(1);
        stack.push(2);
        stack.push(3);
        
        // 弹出栈顶元素并打印
        while (!stack.isEmpty()) {
            System.out.println("栈顶元素: " + stack.pop());
        }
    }
}

队列

  • 先进先出 (FIFO) 的数据结构。
  • 可以在队列的两端进行插入和删除操作。
  • 常用于广度优先搜索、缓冲等。
import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();
        
        // 将元素添加到队列
        queue.add(1);
        queue.add(2);
        queue.add(3);
        
        // 从队列中移除元素并打印
        while (!queue.isEmpty()) {
            System.out.println("移除队列元素: " + queue.remove());
        }
    }
}

二叉树

二叉树的基本概念

  • 根节点(Root):二叉树的顶端节点,是树的起始节点。
  • 父节点(Parent):一个节点的直接上级节点称为其父节点。
  • 子节点(Children):一个节点的直接下级节点称为其子节点,最多有两个子节点。
  • 叶子节点(Leaf):没有子节点的节点称为叶子节点(也称为终端节点)。
  • 深度(Depth):从根节点到某个节点的唯一路径上的节点总数。
  • 高度(Height):从一个节点到其叶子节点的最长路径上的节点总数。

二叉树的分类

  • 满二叉树:每个非叶子节点都有两个子节点,叶子节点都在同一层上的二叉树。
  • 完全二叉树:除了最底层,其他层的节点都是满的,且最底层的节点都尽可能靠左排列的二叉树。

二叉树的遍历方式

  • 前序遍历:先访问根节点,然后递归地前序遍历左子树和右子树。
  • 中序遍历:先递归地中序遍历左子树,然后访问根节点,最后递归地中序遍历右子树。
  • 后序遍历:先递归地后序遍历左子树和右子树,然后访问根节点。

二叉树的应用

  • 二叉搜索树(Binary Search Tree,BST):一种特殊的二叉树,左子树上所有节点的值均小于根节点的值,右子树上所有节点的值均大于根节点的值。
  • 表达式树:用于表示表达式的二叉树,可以方便地进行表达式的计算和求值。
  • 堆(Heap):一种特殊的二叉树结构,常用于实现优先队列等数据结构。
// 二叉树节点类
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

// 二叉树类
class BinaryTree {
    TreeNode root;

    public BinaryTree() {
        this.root = null;
    }

    // 插入节点
    public void insert(int val) {
        root = insertRec(root, val);
    }

    private TreeNode insertRec(TreeNode root, int val) {
        if (root == null) {
            root = new TreeNode(val);
            return root;
        }

        if (val < root.val) {
            root.left = insertRec(root.left, val);
        } else if (val > root.val) {
            root.right = insertRec(root.right, val);
        }

        return root;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值