Data structure study notes - generalized table

1. Definition of generalized table

The generalized table is a further extension of the linear table and is a finite sequence composed of n (n≥0) data elements. The data elements in a linear table can only be a single element (atom), which is indivisible, while the data elements in a generalized table can be either an atom or a generalized table (including empty tables and non-empty tables). The table is enclosed by parentheses "()", and the individual data elements in the table are separated by commas ",".
Insert image description here
An n-dimensional array can be regarded as a generalized table whose elements are n-1-dimensional arrays. The elements of the generalized table are all n-1-dimensional arrays. A generalized table satisfies the characteristics of a linear table, except that the elements in it are atoms or generalized tables (sublists). There is only one head element and one tail element respectively. The head element has a successor but no predecessor, and the tail element has a predecessor but no successor. , each remaining element has a unique predecessor and successor.

2. Header and footer of generalized table

Generalized tables can be recursive. A generalized table can also be its own subtable. The first element in the generalized table is called the generalized table表头, and the remaining data The table composed of elements is called a generalized table. The head and tail of the generalized table can be regarded as operations on the generalized table through the functions head() and tail(). For example, given the generalized table S=(((a)),(b),c,(a),(((d,e)))), the operation of removing element e through head() and tail() is as follows :表尾

head(tail(head(head(head(tail(tail(tail(tail(A)))))))))

For any non-empty generalized list, the head of the table may be a single element (atom) or a generalized list, but the tail of the table can only be a generalized list. The reason is that the tail() of the generalized table is a non-empty generalized list. After removing the head element, the remaining elements in the table are composed of atoms, so they cannot be atoms.
Insert image description here
For example, C=(a,b,c,d,e,f,g), the head of the generalized table is (a) and the tail is (b,c,d, e,f,g);
For example, D=((a,b),((c,d,e),(f,g,h))), the generalized table The header is (a,b) and the tail is ((c,d,e),(f,g,h)).

In addition, if a generalized table is empty, it is an empty table. For example, E=( ), F=(( )), generalized table E is an empty table, and only non-empty generalized tables can take the header. The header and tail of generalized table F are both ().

3. Depth and length of generalized tables

  • The depth of the generalized table is found through括号的层数, while the length is contained in the generalized table元素的个数. [Number of depth layers, number of lengths]

For example, an empty generalized table G=(), the number of bracket levels is 1, so the depth of the generalized table is 1, and since it is an empty table, the length of the generalized table is 0;
For example, a generalized table H=((a,b),(c,(d,e))), the number of bracket levels is 3, so the depth of the generalized table is 3, and the highest level is (c,(d) ,e)), a comma separates the atom (c) and the generalized list (d,e). The number of elements is 2, so the length of the generalized list is 2.
For example, a generalized table I=((),(a),(b,c,(d),((d,f)))), since the maximum number of brackets is 4, so the depth of the generalized table is 4. It can be seen that the generalized table has three elements, namely (), (a), (b,c,(d), ((d,f))), and the number of elements is 3. So the length of the generalized table is 3.
For example, assume that the generalized table J=(( ),( )), for the generalized table J, head(J)=( ), tail(J)=(( )), the maximum of the brackets The number of levels is 2, so the depth of the generalized table is 2. The generalized table has two elements, namely (), (), and the number of elements is 2, so the length of the generalized table is 2.

Note: Tail(J)=(( )) here, not ( ).

4. Generalized tables and binary trees

(1) Generalized table represents binary tree

According to the point in the generalized table that "data elements can be either atoms or a generalized table (including empty tables and non-empty tables)", a binary tree can be represented, that is, through(根结点,根结点的广义表), which can be nested.
For example, the following is a full binary tree:
Insert image description here
The binary tree is represented by a generalized table:
(A , ( B , ( D , E ) ) , ( C , ( F , G ) ) ) )
The explanation of this binary tree is as follows:
The root node is A, its left child is B, B's left child is D, and B's right child is E.
The right child of root node A is C, the left child of C is F, and the right child of C is G.

(2) Code implementation of generalized table representation of binary tree

Displays the established binary tree through a generalized table, a non-empty binary tree T. When it is a left child node or a right child node, a left bracket "( is output ", recursively process the left subtree, output a "," to separate the nodes, then recursively process the right subtree, output a right bracket ")", thereby completing the following root node The two left/right nodes are processed, the code is as follows:

/*广义表输出二叉树*/
void ShowTree(BTree T) {
    
    
	if(T!=NULL) {
    
    
		//当二叉树不为空时
		printf("%c",T->data);	//输入出该结点的数据域
		if(T->lchild!=NULL) {
    
    		//若该结点的左子树不为空
			printf("(");	//输出一个左括号
			ShowTree(T->lchild);	//通过递归继续输出结点的左子树结点下的各结点
			if(T->rchild!=NULL) {
    
    	//若该结点右子树不为空
				printf(",");	//输出一个逗号
				ShowTree(T->rchild);	//通过递归继续输出结点的右子树结点下的各结点
			}
			printf(")");	//输出一个右括号
		} else {
    
    	//若左子树为空,右子树不为空
			if(T->rchild!=NULL) {
    
    
				printf("(");	//输出一个左括号
				ShowTree(T->lchild);	//通过递归继续输出结点的左子树结点下的各结点
				if(T->rchild!=NULL) {
    
    		//若该结点的右子树不为空	
					printf(",");	//输出一个逗号
					ShowTree(T->rchild);	//通过递归继续输出结点的右子树结点下的各结点
				}
				printf(")");	//输出一个右括号
			}
		}
	}
}

For example, a binary tree is as shown below. The binary tree is established and output through the chain storage structure.
Insert image description here
The code is as follows:

#include <stdio.h>
#include <malloc.h>
/*1、二叉树的定义*/
typedef struct BNode {
    
    
	int data;		//数据域
	struct BNode *lchild,*rchild;		//左孩子、右孩子指针
} *BTree;

/*2、二叉树的建立*/
BTree CreateTree() {
    
    
	BTree T;
	char ch;
	scanf("%c",&ch);
	getchar();	//getchar()用于接收每次输入字符结点后的回车符,从而以便输入下一个字符结点
	if(ch=='0')	//当为0时,将结点置空
		T=NULL;
	else {
    
    
		T=(BTree)malloc(sizeof(BTree));	//分配一个新的结点
		T->data=ch;
		printf("请输入%c结点的左孩子结点:",T->data);
		T->lchild=CreateTree();		//通过递归建立左孩子结点
		printf("请输入%c结点的右孩子结点:",T->data);
		T->rchild=CreateTree();		//通过递归建立右孩子结点
	}
	return T;
}

/*3、广义表输出二叉树*/
void ShowTree(BTree T) {
    
    
	if(T!=NULL) {
    
    
		//当二叉树不为空时
		printf("%c",T->data);	//输入出该结点的数据域
		if(T->lchild!=NULL) {
    
    		//若该结点的左子树不为空
			printf("(");	//输出一个左括号
			ShowTree(T->lchild);	//通过递归继续输出结点的左子树结点下的各结点
			if(T->rchild!=NULL) {
    
    	//若该结点右子树不为空
				printf(",");	//输出一个逗号
				ShowTree(T->rchild);	//通过递归继续输出结点的右子树结点下的各结点
			}
			printf(")");	//输出一个右括号
		} else {
    
    	//若左子树为空,右子树不为空
			if(T->rchild!=NULL) {
    
    
				printf("(");	//输出一个左括号
				ShowTree(T->lchild);	//通过递归继续输出结点的左子树结点下的各结点
				if(T->rchild!=NULL) {
    
    		//若该结点的右子树不为空	
					printf(",");	//输出一个逗号
					ShowTree(T->rchild);	//通过递归继续输出结点的右子树结点下的各结点
				}
				printf(")");	//输出一个右括号
			}
		}
	}
}

/*主函数*/
int main() {
    
    
	BTree T;
	T=NULL;
	printf("请输入二叉树的根结点:");
	T=CreateTree();		//建立二叉树
	printf("建立的二叉树如下:\n");
	ShowTree(T);		//通过广义表显示二叉树
}

Enter the left and right child nodes of each node in sequence. If the node does not exist, enter 0. For example, the left child node of node d in the tree does not exist, and nodes f, g, h, i, j None of the left and right children exist, and 0 is entered when inputting.
The running results are as follows, and the results are displayed through the definition of generalized tables:
Insert image description here

Guess you like

Origin blog.csdn.net/qq_43085848/article/details/134790544
Recommended