数据结构 Data Structure

时间:2021-09-15
本文章向大家介绍数据结构 Data Structure,主要包括数据结构 Data Structure使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Overview

In this course, we mainly learned some basic data structures, like:

  • Linear Data Structure: Linked List, Stack, Queue
  • Non-Linear Data Structure: Binary Tree and Graph

一、Linear Data Structure

1. Linked List

The linked list uses a set of arbitrary storage units to store the data elements.

Each node of the linked list stores its own data and a pointer to the next node.

In C/C++ Language:

typedef struct Node{
    int val;
    struct Node *next;
}Node, *LinkedList;

There is also double linked list, which has two pointers:

2. Stack

In stack, data can only be inserted at the end of it.

It follows the Last In First Out principle:

Linked List implementation of Stack:

typedef struct Node{
    int val;
    struct Node *next;
}Node, *LinkedStack

3. Queue

In queue, data can only be inserted at the tail and taken out at the head.

It follows First In First Out principle:

Linked List implementation of Queue:

typedef struct Node{
    int val;
    struct Node *next;
}Node, *LinkedQueue

二、Non-Linear Data Structure

1. Binary Tree

Tree is a finite set of n nodes. Any non-empty tree has a root node.

The root of the node's sub-tree is the child of the node, and the node is the parent of the child.

In a binary tree, each node has at most two sub-trees.

Three are two special types of binary tree:

  • Full Binary Tree

In full binary tree, each non-leaf node has two children.

  • Complete Binary tree

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

An important characteristic of a complete binary tree is that the depth of a complete binary tree with n nodes is \(\log_2n + 1\)

C++ implementation of binary tree:

typedef struct Node
{
   	int data;
    struct Node *lchild,*rchild;
}Node,*BinaryTree;

2. Graph

In the graph, the relationship between nodes is arbitrary, and every two nodes may be related.

There are mainly two types of graphs: directed graph and undirected graph.

In this course we learned about the adjacency list implementation of graph.

In the adjacency list, each vertex has a linked list, and the linked list stores the edges attached to the vertex.

Code:

typedef char VertexType;
typedef int ArcType;
typedef struct ArcNode{
    int adjvex;//顶点下标(位置)
    ArcType weight;//权值
    struct ArcNode *nextarc;//指向下一个边结点
}ArcNode;//边结点
typedef struct VNode{
    VertexType data;//顶点内容
    ArcNode *firstArc;//指向第一个边结点
}VNode,AdjList[MVNUM];
typedef struct{
    AdjList vertices;//顶点数组
    int vexNum,arcNum;//顶点数和边数
}ALGraph;

Traversal of Graph:

三、Curriculum Design

I made a Huffman encoder/decoder.

Decoder reads the text file, then counts the frequency of each character and encodes the text according to the Huffman tree.
The decoder reads the encoded file and translates the encoded file into the original text file according to the Huffman code.

原文地址:https://www.cnblogs.com/danielwong2021/p/15271283.html