The core idea, not debugged.
Tree class methods have to be impelmented.
// treeInsert.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>#include <string.h>#include <ctype.h>const char openBrace = '(';const char closeBrace = ')';const int treeDimension = 2; //binaryclass Node{};class Tree{public : // returns pointer to the new added node. Node* addNode(Node *nodeOwner, int value) { // custon case: nodeOwner == NULL when the node is the very first one. return NULL; }; Node* getParentNode(Node* node) { return NULL; } void deleteNode(Node *node) { };};// on input accepts zero-terminated stringchar * getNextCharacter(char* buff){ int index = 0; while ( 0 != buff[index]) { if ( (openBrace == buff[index]) || // ok for bracket (closeBrace == buff[index]) || // ok for bracket (isdigit(buff[index])) // ok for digits ) { return buff + index; } index++; } return NULL;}int main(int argc, char* argv[]){ char test_string[50]="(5(3(2( () () ) ( () )(9( () () )( () )) ) )"; Tree myTree; //add a root initially: Node * current = myTree.addNode(NULL, 0); int nodesCount = 0; int digit; char *buffPtr = test_string; while (1) { char* ch = getNextCharacter(buffPtr); buffPtr = ch; if (NULL == buffPtr) { break; // string exhausted. } char nextChar = *ch; printf("%c\t", ch[0]); if (nextChar != closeBrace) { if (nextChar == openBrace) { current = myTree.addNode(current, digit); // add a subnode digit = 0; nodesCount++; // nodes count for current tree level if (nodesCount > (treeDimension-1) ) { // go to the upper level: nodesCount = 1; current = myTree.getParentNode(current); } } else if ( isdigit(nextChar) ) { digit = nextChar; // remember value for further use. } } buffPtr++; } printf("Hello World!\n"); return 0;}