Representation of generalized tables JavaScript

Source: Internet
Author: User

Generalized table is the generalization of linear table, and some people call it as a list. So what's the difference between it and a linear table? Each member in a linear table can only be a single element, whereas a member in a generalized table may be a single element or a generalized table, which is called the Atomic and Sub-table of a generalized table, respectively. Here are some examples of generalized tables.

A= ();

B= (e);

C= (A, (b,c,d));

D= ((), (e), (A, (b,c,d)));

E= (a,e);


Because data elements in a generalized table can have different structures (atoms or lists), it is difficult to represent them in sequential storage structures, typically with chained storage structures. Because the elements in the list can be atoms or lists, you need two kinds of structure nodes, one is a table node and one is an atomic node.

A table node consists of three fields, a flag field, a pointer field to a table header, and a pointer field pointing to the end of a table. The atomic node requires only two domains, a flag domain, and a range. Such as:



The storage structure of the five lists mentioned above is as follows:


We use JavaScript to implement generalized tables and their basic operations.

First you need to define a storage structure for a generalized table:

var Atom=0;var list=1;//The storage structure of the generalized table function ListNode () {//Identity bit this.tag=undefined;//The domain value of the atomic node this.atom=null;// The domain of the list node This.ptr={hp:null,tp:null};}

Then is the process of creating a generalized table:

Create a generalized table Listnode.prototype.createlist=function (string) {String=string.trim ();//Create a single-atom generalized table var q;if (/^[\w-]+$/.test ( String) {//contains single character this;tag=atom;this.atom=string;} Else{this.tag=list;var p =this;//Remove the outermost brackets (and) var sub=string.substr (1,string.length-2);d O{var h,i=0,k=0,n=sub.length, Ch;do{ch=sub[i++];if (ch== ' (') {++k;} else if (ch== ') ') {--k;}} while (i<n&& (ch!= ', ' | | | k!=0));//i is the first comma-delimited index if (i<n) {h=sub.substr (0,i-1);//Each traversal takes out the first node, whether it is an atom or a list sub=sub.substr (i,n-i);} else{//the last set of h=sub;sub= ';} if (h=== ' () ') {//The empty table has no header node p.ptr.hp=null;} else{//Create Header Node This.createList.call ((P.ptr.hp=new ListNode ()), h);} q=p;//Create the footer node if (sub) {p=new ListNode ();p. tag=list;q.ptr.tp=p;}} while (sub); q.ptr.tp=null;}};
Then the depth of the generalized table is defined as the weight of the parentheses in the generalized table, which is a measure of the generalized table. For example, the depth of a multivariate polynomial generalized table is the number of arguments in a polynomial. Set ls= (A1,a2,a3,..., an), the depth of LS can be decomposed into the problem of N, each sub-problem is to seek the depth of AI. If the AI is an atom, it defines a depth of 0, and if the AI is a generalized table, the depth of LS is +1 of the maximum AI depth. The empty table is also a generalized table, so the depth is 1. The implementation code is as follows:

The depth of the generalized table is listnode.prototype.depth=function () {return getdepth (this);} The function getdepth (list) {//depth is the number of parentheses, or it can be interpreted as the number of open parenthesis occurrences if (!list) {return 1;} else if (list.tag===atom) {return 0;} else {var m=getdepth (list.ptr.hp) +1;var n=getdepth (LIST.PTR.TP); return m>n?m:n;}}

Finally we create a test case:

var node=new listnode (); Node.createlist (' ((), (a), (b, (c,d,e))) '); alert (node.depth ());//5

Node node details such as:

The complete code is as follows:

Since the application of generalized tables lies in the derivation and calculation of formulas in the field of mathematics, there is no longer a detailed explanation.



Representation of generalized tables JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.