void Bvh::constructTree(Node* n) 
{
	if (n->endPrim < n->startPrim) {
		std::cout << "ERROR: endPrim is smaller than startPrim in constructTree" << std::endl;
		return;
	}

	// Compute the node's bounding box and assign the start and endPrim to the node
	std::pair<Vec3f, Vec3f> bb = Bvh::computeBB(n->startPrim, n->endPrim);
	n->bbMin = bb.first;
	n->bbMax = bb.second;

	if (n->endPrim - n->startPrim + 1 > MAX_TRIANGLES_PER_LEAF)
	{
		// Decide how to split the primitives and
		// perform the actual split: shuffle the indices in the global list
		// so that they're split into two intervals; return the index that
		// separates the two intervals
		int splitPrim = Bvh::partitionPrimitives(n->startPrim, n->endPrim);		

		// Create the left child and recursively call this
		// function with the left triangle interval
		n->leftChild = new Node(n->startPrim, splitPrim-1);
		constructTree(n->leftChild);

		// Create the right child and recursively call this
		// function with the right triangle interval
		n->rightChild = new Node(splitPrim, n->endPrim);
		constructTree(n->rightChild);
	}
}
Example #2
0
// A function to input new data
void inputNew()
{
	int res=0,i=0;
	while(i<3)
	{
		if(ti==0)
		{
			printf("Enter temperature\n");
			scanf("%d",&ta);
			res=constructTree();
		}
		else if(ti==1)
		{
			printf("Enter humidity\n");
			scanf("%d",&ha);
			res=constructTree();
		}		
		else if(ti==2)
		{
			printf("Enter windspeed\n");
			scanf("%d",&wa);
			res=constructTree();
		}
		if(res==1)
			break;
		mGini=max();
		i++;
	}
}
TreeNode* constructTree(const vector<int> &A, int low, int high)
 {
     if(low>high)
        return NULL;
    int mid = (low+high)/2;
    TreeNode* t = new TreeNode(A[mid]);
    t->left = constructTree(A,low,mid-1);
    t->right = constructTree(A,mid+1,high);
    return t;
 }
Example #4
0
 TreeNode* constructTree(vector<int>& nums, int left, int right)
 {
     if(left >= right)
         return NULL;
     int max_idx = left;
     for(int i = left; i < right; i++)
         if(nums[i] > nums[max_idx])
             max_idx = i;
     TreeNode* node = new TreeNode(nums[max_idx]);
     node->left = constructTree(nums, left, max_idx);
     node->right = constructTree(nums, max_idx+1, right);
     return node;
 }
 TreeNode *constructTree(int left, int right) {
     if (left > right)
         return nullptr;
     
     int mid = left + (right - left) / 2;
     TreeNode *leftNode = constructTree(left, mid-1);
     TreeNode *node = new TreeNode(ptrNode->val);
     node->left = leftNode;
     ptrNode = ptrNode->next;
     TreeNode *rightNode = constructTree(mid+1, right);
     node->right = rightNode;
     
     return node;
 }
Example #6
0
int main() {
    vector<string> data = {"1", "2", "3"};
    auto root = constructTree(data);
    cout << Solution().sumNumbers(root);

    return 1;
}
Example #7
0
void octreeSolid::init(SurfaceObj* obj, int res)
{
	sufObj = obj;

	constructTree(sufObj, res);
	computeBoxAndVolume(m_root);
}
Example #8
0
void octreeSolid::init(SurfaceObj* obj, float voxelSize)
{
	sufObj = obj;

	constructTree(sufObj, voxelSize);
	computeBoxAndVolume(m_root);
}
Example #9
0
 TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
     if(inorder.size() == 0) return NULL;
     else{
         constructTree(inorder, postorder, 0, inorder.size()-1, postorder.size()-1);
     }
     
 }
Example #10
0
File: 6.cpp Project: dasima/Books
void constructTreeTest(int *pre_order, 
        int *in_order, 
        int length)
{
    std::cout << "---The pre order sequence is:---\n";
    for(int index = 0; index < length; ++index)
        std::cout << pre_order[index];
    std::cout << std::endl;

    std::cout << "---The in_order sequence is:---\n";
    for(int index = 0; index < length; ++index)
        std::cout << in_order[index];
    std::cout << std::endl;

    try
    {
        BinaryTreeNode *root = constructTree(pre_order, 
                in_order, 
                length);
        printTree(root);

        destroyTree(root);
    }
    catch(std::exception &exception)
    {
        std::cout << "Invalid inout.\n";
    }
}
 TreeNode *constructTree(InputIterator pStart, InputIterator pEnd, InputIterator iStart, InputIterator iEnd) {
     if (pStart == pEnd) {
         return nullptr;
     }
     if (iStart == iEnd) {
         return nullptr;
     }
     
     auto root = new TreeNode(*pStart);
     auto rootIndex = find(iStart, iEnd, *pStart);
     auto leftLength = distance(iStart, rootIndex);
     
     root->left = constructTree(next(pStart), next(pStart, leftLength+1), iStart, next(iStart, leftLength));
     root->right = constructTree(next(pStart, leftLength+1), pEnd, next(rootIndex), iEnd);
     return root;
 }
Example #12
0
/*
    MAINLINE
*/
int main(int argc, const char *argv[]){
  printf("MakeTreeSpawn - Gareth Bradshaw Feb 2003\n");

  /*
     parse command line
  */
  decodeIntParam(argc, argv, intParams);
  decodeBoolParam(argc, argv, boolParams);
  printf("Options : \n");
  writeParam(stdout, intParams);
  writeParam(stdout, boolParams);

  /*
     look for filenames and construct trees
  */
  int numFiles = 0;
  for (int i = 1; i < argc; i++){
    if (argv[i] != NULL){
      constructTree(argv[i], yaml);
      numFiles++;
      }
    }

  /*
     check we had a file name
  */
  if (numFiles == 0)
    error("no files given :(");

  waitForKey();
}
Bvh::Bvh(std::vector<RTTriangle>& triangles) : m_triangles(&triangles), m_root(new Node(0, (int)(triangles.size()-1)))
{
	constructTree(m_root);
	std::cout << "DEBUG: BVH construction complete." << std::endl;
	std::cout << "Total nodes: " << getNumOfNodes() << std::endl;
	std::cout << "Leaf nodes: " << getNumOfLeafNodes() << std::endl;
	std::cout << "Maximum depth: " << getDepth() << std::endl << std::endl;
}
Example #14
0
int main() {
    vector<string> data{"1","2","3","#","#","4","5"};
    auto root = constructTree(data);
    Codec cd;
    auto s = cd.serialize(root);
    auto proot = cd.deserialize(s);
    return 1;
}
node *constructTree(int *pre, int *post, int start, int end, int *preStart){
	if(end-start+1 <1 )
		return NULL;
	node *n = newNode(pre[*preStart]);
	n->left = NULL;
	n->right = NULL;
	++*preStart;
	if(end-start+1 >1){
		int i = find(post+start, post+end+1, pre[*preStart+1]) - post;
		if(i <= end){
			n->left = constructTree(pre, post, start, i, preStart);
			n->right = constructTree(pre, post, i+1, end, preStart);
		}
	}
	return n;
	
}
 TreeNode* constructTree(vector<int> &preorder, vector<int> &inorder, int start, int end, int start2, map<int,int> &inorderMap) {
     if (start > end)
         return NULL;
     
     int i;
     TreeNode* newNode;
     newNode = new TreeNode(preorder[start]);
     int rootVal = preorder[start];
     //To avoid the loop function, using map instead
     //for (i = start; i <= end && inorder[i] != rootVal; i++);
     i = inorderMap[rootVal] - start2 + start;
     
     newNode->left = constructTree(preorder, inorder, start+1, i, start2, inorderMap);
     newNode->right = constructTree(preorder, inorder, i+1, end, inorderMap[rootVal] + 1,inorderMap);
     
     return newNode;
 }
    TreeNode* constructTree(vector<int> &nums, size_t begin, size_t end)
    {
        if (begin > end) return nullptr;
        size_t mid = (begin+end+1)/2;       //begin + (end - begin + 1) / 2;

        TreeNode *root = new TreeNode(nums[mid]);
        if (mid - begin > 0)
            root->left = constructTree(nums, begin, mid - 1);
        else
            root->left = nullptr;
        
        if (end - begin > 0)
            root->right = constructTree(nums, mid + 1, end);
        else
           root->right = nullptr;
           
        return root;
    }
Example #18
0
 TreeNode *constructTree(vector<int> &inorder, vector<int> &postorder, int istart, int iend, int pend) {
     if(istart>iend) return NULL;
     else{
         int root_val = postorder[pend];
         TreeNode *root = new TreeNode(root_val);
         int mid;
         for(auto it = inorder.begin() + istart; it <= inorder.begin() + iend; it++) {
             if(*it == root_val) {
                 mid = it - inorder.begin();
                 break;
             }
         }
         
         root->left = constructTree(inorder, postorder, istart, mid-1, pend-1-iend+mid);
         root->right = constructTree(inorder, postorder, mid+1, iend, pend-1);
         return root;
     }
 }
Example #19
0
void octreeSolid::init(char* filePath, int res)
{
	surfaceLoaded = true;
	sufObj = new SurfaceObj;
	sufObj->readObjDataSTL(filePath);
	sufObj->constructAABBTree();

	constructTree(sufObj,  res);
	computeBoxAndVolume(m_root);
}
Example #20
0
    TreeNode *constructTree(vector<int>& preorder, int i, int j, vector<int>& inorder, int x, int y){
      if(i > j){
        return NULL;
      }

      TreeNode *root = new TreeNode(preorder[i]);

      int mid;
      for(mid = x; mid <= y; mid++){
        if(inorder[mid] == preorder[i]){
          break;
        }
      }

      int left_tree_size = mid - x;
      root->left = constructTree(preorder, i + 1, i + left_tree_size, inorder, x, mid - 1);
      root->right = constructTree(preorder, i + left_tree_size + 1, j, inorder, mid + 1, y);
      return root;
    }
 TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
     if (preorder.size() == 0)
         return NULL;
     
     map<int,int> inorderMap;
     for (int i = 0; i < inorder.size(); i++)
         inorderMap[inorder[i]] = i;
     
     return constructTree(preorder, inorder, 0, preorder.size()-1, 0, inorderMap);
 }
Example #22
0
void octreeSolid::initTest()
{
	surfaceLoaded = true;
	sufObj = new SurfaceObj;
	sufObj->readObjDataSTL("../Data_File/euroFighter.stl");
	sufObj->constructAABBTree();

	constructTree(sufObj,  4);
	computeBoxAndVolume(m_root);
}
 TreeNode* sortedListToBST(ListNode* head) {
     ListNode *curNode = head;
     int num = 0;
     while (curNode != nullptr) {
         curNode = curNode->next;
         num++;
     }
     ptrNode = head;
     return constructTree(0, num-1);
 }
int main(){
	int pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7};
    int post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
    int size = sizeof( pre ) / sizeof(int);
	int preStart = 0;
    struct node *root = constructTree(pre, post, 0, size-1, &preStart);

    printf("Inorder traversal of the constructed tree: \n");
    printInorder(root);
	return 0;
}
Example #25
0
struct TreeNode* constructTree(int* nums, int start, int end) {
    int mid;
    struct TreeNode *root;

    if (NULL == nums) return NULL;
    mid = (start + end) / 2;
    root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = nums[mid];

    if (mid == end)
        root->right = NULL;
    else
        root->right = constructTree(nums, mid + 1, end);

    if (mid == start)
        root->left = NULL;
    else
        root->left = constructTree(nums, start, mid - 1);
   
    return root;
}
int main ()
{
    int pre[] = {10, 5, 1, 7, 40, 50};
    int size = sizeof( pre ) / sizeof( pre[0] );
 
    struct node *root = constructTree(pre, size);
 
    printf("   Inorder traversal of the constructed tree: \n");
    printInorder(root);
 
    return 0;
}
Bvh::Bvh(std::vector<RTTriangle>& triangles) : m_triangles(&triangles), m_root(new Node(0, (int)(triangles.size()-1)))
{
	Timer stopwatch;
	stopwatch.start();
	constructTree(m_root);
	stopwatch.end();

	std::cout << "BVH construction complete:" << std::endl;
	std::cout << "--------------------------" << std::endl;
	std::cout << "BVH mode.....: " << (mode ? "SAH" : "Spatial") << std::endl;
	std::cout << "Build time...: " << stopwatch.getTotal() << " sec" << std::endl;
	std::cout << "Total nodes..: " << getNumOfNodes() << std::endl;
	std::cout << "Leaf nodes...: " << getNumOfLeafNodes() << std::endl;
	std::cout << "Maximum depth: " << getDepth() << std::endl << std::endl;
}
/**
 * Given an array of points the function creates a kd-tree containing the points.
 *
 * @param arr - the array of SPPoints
 * @param size - the size of arr
 * @param dim - the dimension of the points in arr
 * @param splitMethod - the method to construct
 * the tree: 0 - RANDOM, 1 - MAX_SPREAD, 2 - INCREMENTAL
 *
 * @assert dim = point->dim for each point in arr;
 *
 * @return
 * NULL if arr == NULL or size < 1 or dim <1 or splitMethod
 * isn't between 0 and 2.
 * the new kd-tree otherwise
 */
KDTreeNode kdTreeInit(SPPoint* arr, int size, int dim, int splitMethod) {
	KDTreeNode kdt;

	if (arr == NULL || size < 1 || dim < 1 || splitMethod < 0
			|| splitMethod > 2)
		return NULL ;

	KDArray mat;
	mat = kdArrayInit(arr, size, dim);

	kdt = constructTree(mat, size, dim, splitMethod, dim);
	kdArrayDestroy(mat);
	return kdt;

}
Example #29
0
    vector<string> findWords(vector<vector<char> > &board, vector<string> &words) {
        // write your code here
        vector<string> res; 
        if(board.size() == 0 || board[0].size() == 0 || words.size() == 0) {
            return res;
        }
        
        TrieNode* root = constructTree(words);
        
        int row = board.size();
        int col = board[0].size(); 
        
        for(int i=0; i<row; i++) {
            for(int j=0; j<col; j++) {
                wordSearch(board, row, col, i, j, root, res);
            }
        }

        return res;
        
    }
int main()
{
        int num,i;
        char c;
        char sentence[MAXSIZE];
        int top=0,arr[MAXSIZE];
        
        NODE *temp1,*temp2;
        
        printf("Enter the sentence to be encoded : ");
        scanf("%[^\n]",sentence);
        
        num = calculateFre(fre,sentence);       //calculate the frequency of occurrence
                
        displayFre(fre);                        //displayFre() function present in the header file.
       
        for(i=0;i<num;i++)
        {
                insert(fre[i].ch,fre[i].prob);  //insert every character into linked list
        }
        
        //display();
        
        while(n->link != NULL)
        {
                temp1=n;
                temp2 = n->link;
                n=n->link->link;
                constructTree(temp1,temp2);
                
        }
      
        encode(n->root,arr,top);
       
        diplayEncodedString(sentence);
        //inorder(n->root);


}