Code implementation of data structure based on Java language

Resource download address : https://download.csdn.net/download/sheziqiong/88294611
Resource download address : https://download.csdn.net/download/sheziqiong/88294611

Introduction

This project is a code implementation of data structures based on Java language. It contains all classic data structure algorithms and is well-annotated. It is very suitable for understanding and learning data structures. It also includes a contact storage tool (phonebook), which is displayed by swing and applies related concepts of data structure algorithms. If it is helpful to you, please follow itO

structure

  • array – Arrays and generalized tables

  • graph–graph

  • list – linear list

  • phonebook – comprehensive application (using data structure to implement contact storage software)

  • search–find

  • sort–sort

  • stackqueue – stacks and queues

  • string – string

  • tree – tree

  • leetcode_agorithm leetcode algorithm example problem solution

data agency

linear table

Linear table is the most basic, simplest, and most commonly used data structure. The relationship between data elements in a linear table is a one-to-one relationship, that is, except for the first and last data elements, other data elements are connected end to end (note that this sentence only applies to most linear tables , not all. For example, the circular linked list is also a linear list at the logical level (linked storage at the storage level), but the tail pointer of the last data element points to the sentinel node).

Insert image description here

  • SeqList is a singly linked list implemented using arrays. The time complexity is as follows:
    • index:O(1)
    • search:O(n)
    • insert:O(n)
    • Remove:O(n)

Insert image description here

  • SinglyLinkedList is a singly linked list with the head node. It is a linear collection composed of nodes (Node). Each node can use pointers to point to other nodes. The time complexity is as follows:

    • index:O(n)
    • search:O(n)
    • insert:O(1)
    • Remove:O(1)

    Insert image description here

  • SortedSinglyLinkedList is a sortable singly linked list, which requires that the added elements must implement the Comparable interface.

  • PolySLinkedList is a data structure that can be sorted and added to other linked lists. It implements the Addible interface.

Stacks and Queues

The stack is a first-in-last-out collection of elements, where push means pushing onto the stack and pop means popping out of the stack.
A queue is a first-in-first-out collection of elements, where enqueue means entering the queue and dequeue means leaving the queue.

Insert image description here

  • SeqQueue uses an array to implement a queue structure. The time complexity is as follows:
    • enqueue: O(1)
    • dequeue:O(1)
  • LinkedQueue is a queue structure implemented using linked lists.

Insert image description here

  • PriorityQueue priority queue, elements with high priority are dequeued first
  • SeqStack is a stack structure implemented using arrays
    • push: O(1)
    • pop: O(1)
  • LinkedStack is a stack structure implemented using linked lists.

string

A string is a finite sequence composed of zero or more characters, also called a string.

  • MyString simulates string structure
  • MyStringBuffer simulates a mutable string structure

Arrays and generalized tables

An array is a data structure composed of a set of variables of the same type but with different subscripts. The values ​​of elements in a generalized table are non-atomic and can be decomposed. The elements in the table can also be a linear table, and all data elements still belong to the same data type.

  • Generalized list of GenList double-chain structure
  • DownTriangleMatrix Linearly compressed stored lower triangular matrix
  • CrossLinkedSparseMatrix coefficient matrix stored in cross linked list
  • LinkedSparseMatrix Coefficient matrix stored in a singly linked list of triple rows
  • Matrix is ​​a matrix structure stored using arrays
  • SeqSparseMatrix is ​​a matrix structure stored in a sequence table of sparse matrix triples.

Tree

A dendrogram is a data structure, which is a set of hierarchical relationships composed of n (n>=1) limited nodes. It's called a "tree" because it looks like an upside-down tree, which means it has the roots facing up and the leaves facing down.

  • Tree structure, each node contains child nodes and sibling nodes

  • BinaryTree binary tree, each node contains a left child node and a right child node

    Insert image description here

  • CompleteBinaryTree is a complete binary tree. Except for the last level, the number of nodes on each level reaches the maximum; only a few nodes on the right side are missing on the last level.

Full binary tree: A binary tree in which all nodes on each level have two child nodes except the last level which does not have any child nodes.

Insert image description here

  • ThreadBinaryTree in-order clue binary tree is traversed in in-order. If the left child of each node is empty, it points to its predecessor node. If the right child is empty, it points to its successor node.

picture

A graph is a data structure with a many-to-many relationship between data elements.

  • Undirected Graph: An undirected graph has a symmetric adjacency matrix, so if there is an edge from node u to node v, vice versa, an edge from v to u also exists.
  • Directed Graph: The adjacency matrix of a directed graph is asymmetric, that is, if there is an edge from u to v, it does not mean that there must be an edge from v to u.

Insert image description here

  • AdjMatrixGraph is a weighted graph data structure represented by adjacency matrix.
  • AdjListGraph is a weighted graph data structure represented by an adjacency list.

Insert image description here

sort

insertion sort

  • Direct insertion sort (stable)

Insert image description here

  • Hill sort (unstable)

Insert image description here

swap sort

  • Bubble sort (stable)

Insert image description here

  • The time complexity of quick sort (unstable) is as follows:
    • Optimal time: O(nlog(n))

    • Worst time: O(n^2)

    • Average time: O(nlog(n))

      Insert image description here

selection sort

  • Direct selection sort (unstable)

    Insert image description here

  • Heap sort (unstable) is a sorting algorithm designed using the data structure of a stacked tree (heap). It is a type of selection sort. You can use the characteristics of arrays to quickly locate the element at a specified index. The heap is divided into a large root heap and a small root heap, which is a complete binary tree. The requirement of a large root heap is that the value of each node is not greater than the value of its parent node, that is, A[PARENT[i]] >= A[i]. In non-descending sorting of an array, a large root heap needs to be used, because according to the requirements of a large root heap, the maximum value must be at the top of the heap. The time complexity is as follows:

    • Optimal time: O(nlog(n))

    • Worst time: O(nlog(n))

    • Average time: O(nlog(n))

      Insert image description here

  • Merge sort (stable) Merge sort uses a divide-and-conquer idea. It continuously divides an array into two parts, sorts the left sub-array and the right sub-array respectively, and then merges the two arrays into a new one. Ordered array. The time complexity is as follows:

    • Optimal time: O(nlog(n))

    • Worst time: O(nlog(n))

    • Average time: O(nlog(n))

      Insert image description here

look up

  • BinarySortTree is a special binary tree. For each node, the values ​​of all nodes under its left child must be smaller than the values ​​of all nodes under its right child. The construction process is as follows:

    Insert image description here

  • HashSet adopts the hash table data structure of the chain address method, and internally uses the SinglyLinkedList array to store elements.

    Insert image description here

Regarding hashing: In general linear tables and trees, the relative positions of records in the structure are random. Therefore, when searching for records in the structure, a series of comparisons with keywords are required. The ideal situation is to be able to directly find the required record, so a certain correspondence f must be established between the storage location of the record and its key, so that each key corresponds to a unique storage location in the structure.

Resolve conflicts:

  • Link method (zipper method). Store records with the same hash address in a linear linked list. For example, in the division with remainder method, let the keyword be (18,14,01,68,27,55,79) and the divisor be 13. The hash address is (5,1,1,3,1,3,1),
  • Open Address Method In the open address method, when a new value is inserted, it will be judged whether the hash bucket corresponding to the value exists. If it exists, the next possible location will be selected in sequence according to a certain algorithm until an address that has not yet been occupied is found. . The so-called open address method also means that the position of an element is not always determined by its hash value.

Resource download address : https://download.csdn.net/download/sheziqiong/88294611
Resource download address : https://download.csdn.net/download/sheziqiong/88294611

Guess you like

Origin blog.csdn.net/newlw/article/details/132654184