MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / treebuilding

Function treebuilding

CPP/binarytreeuse.cpp:151–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

149}
150
151binarytree<int> * treebuilding(int *in, int *pre, int preS,int preE,int inS,int inE)
152{
153 if(inS>inE)
154 {
155 return NULL;
156 }
157
158 int rootdata=pre[preS];
159 binarytree<int>*root=new binarytree<int>(rootdata);
160
161 int rootindex=-1;
162 for(int i=inS;i<=inE;i++)
163 {
164 if(rootdata==in[i])
165 {
166 rootindex=i;
167 break;
168 }
169 }
170
171
172 int lpreS=preS+1;
173 int linS = inS;
174 int linE = rootindex-1;
175 int lpreE = linE-linS+lpreS;
176 int rpreS = lpreE +1;
177 int rpreE = preE;
178 int rinS = rootindex +1;
179 int rinE = inE;
180
181
182 root->left=treebuilding(in,pre,lpreS,lpreE,linS,linE);
183 root->right=treebuilding(in,pre,rpreS,rpreE,rinS,rinE);
184
185 return root;
186
187
188}
189
190binarytree<int> * buildtree(int *in,int *pre,int size)
191{

Callers 1

buildtreeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected