1. 程式人生 > 其它 >資料結構 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.