A generalized table is a nonlinear structure whose definition is recursive.
Here are a few simple generalized table models:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/22/wKioL1cU06LDp3cZAAAGadRgppI798.png "style=" float: none; "title=" QQ picture 20160418202025.png "alt=" Wkiol1cu06ldp3czaaagadrgppi798.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/25/wKiom1cU0uWjiRe8AAAM3dKYxa8731.png "style=" float: none; "title=" QQ picture 20160418202854.png "alt=" Wkiom1cu0uwjire8aaam3dkyxa8731.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/22/wKioL1cU06PCYs3lAAAm-7A2avQ633.png "style=" float: none; "title=" QQ picture 20160418202716.png "alt=" Wkiol1cu06pcys3laaam-7a2avq633.png "/>
As we can see, the node type of the generalized table is nothing but head, value, sub Three, set the enumeration type here, and use the enumeration variable to record the type of each node:
Enum Type{head,//Head node value,//Value node sub,//Child table node};
Each node has its own type and next pointer, and in addition, if the node is a value type, it also allocates space to store valid values for that node, but if the node is a sub type, you need to define a pointer to the head of the child table.
Here we can use the Union to solve this problem.
(A union (or community) is a method of sharing storage space between members of different data types, and a Union object can store only one member value at a time)
To construct a node :
struct Generalizednode{type _type; 1. Type generalizednode* _next; 2. Point to the next node in the same layer Union{char _value; 3. Valid value generalizednode* _sublink; 3. Pointer to child table}; Generalizednode (Type type = HEAD, char value = ' 0 '): _value (value), _type (type), _next (NULL) {if (_type = = SUB) {_sublink = NU LL;}};
definition and basic operation of generalized tables :
Class generalized{public://A parameterless constructor, establishes an empty generalized table generalized ();//Constructs a generalized table, a constructor with parameters generalized (const char* str);//Print Generalized table void Print ();//Gets the number of value nodes size_t Amount ();//Gets the depth of the generalized table size_t Depth ();//Copy Construction Generalized (const generalized& g);//// The overloaded generalized& operator= (const generalized& g) of the assignment operator,////destructor ~generalized ();p rotected:void _print ( generalizednode* head); generalizednode* _creatlist (const char*& str); size_t _amount (generalizednode* head); generalizednode* _copy (generalizednode* head), void _destory (generalizednode* head);p rotected:generalizednode* _head ; Record generalized table-head pointers};
Initializes a generalized table for cyclic recursion. When a string is traversed, a value node is created, and the ' (' recursively creates a child table; ') ' Ends the creation of the current child table and returns the head pointer of the current child table.
generalizednode* _creatlist (const char*& str) {assert (*str = = '); generalizednode* head = new Generalizednode (head, ' 0 '); generalizednode* cur = head;str++;while (str! = ') ' {if (*str >= ' 0 ' &&*str <= ' 9 ') | | (*str >= ' a ' &&*str <= ' z ') | | (*str >= ' A ' &&*str <= ' Z ')) {cur->_next = new Generalizednode (VALUE, *str); cur = cur->_next;} else if (*str = = ' (') {cur->_next = new Generalizednode (SUB); cur = Cur->_next;cur->_sublink = _creatlist (str);} else if (*str = = ') ') {return head;} str++;} return head;}
Print a generalized table: When a node is of type sub, do not forget to print a post bracket for each layer you print.
void _print (generalizednode* head) {if (head = = NULL) {cout << "generalized table is NULL" << Endl;return;} generalizednode* cur = head;while (cur) {if (Cur->_type = head) {cout << ' (';} else if (Cur->_type = = VALUE) {cout << cur->_value;if (cur->_next) {cout << ', ';}} else if (Cur->_type = = SUB) {_print (Cur->_sublink); if (cur->_next) {cout << ', ';}} cur = cur->_next;} cout << ') ';}
gets the number of value nodes : Sets the Count variable, encounters a value node adds 1, encounters a sub node recursively merges the return value to count
size_t _amount (generalizednode* head) {generalizednode* begin = head;size_t count = 0;while (BEGIN) {if (Begin->_type = = VALUE) {count++;} if (Begin->_type = = SUB) {count + = _amount (Begin->_sublink);} begin = Begin->_next;} return count;}
Generalized Table Depth: Sets the variable DP and Max to record the current child table, which is the sub-table depth pointed to by the current sub node, and the maximum number of sub-tables in all sub nodes in this layer.
size_t _depth (generalizednode* head) {if (_head = = NULL) {return 0;} size_t Dp=0; generalizednode* cur = head;size_t max = 0;while (cur) {if (Cur->_type = = SUB) {dp=_depth (Cur->_sublink); if (Max < DP) {max = DP;}} cur = cur->_next;} return max+1;}
destroys generalized tables : Iterates through the nodes, encounters the child table recursion, completes the node delete of the child table, and then goes back to the current layer to continue the traversal.
void _destory (generalizednode* head) {if (head = = NULL) {return;} while (head) {generalizednode* begin = head->_next;if (Head->_type = = SUB) {_destory (head->_sublink);} Delete head;head = begin;}}
The copy construction and assignment operator overloading of generalized tables are similar to the recursive methods of constructors, where redundant information is no longer elaborated.
This article from "Yan Anyang" blog, declined reproduced!
Implementation of generalized tables