#include #include using namespace std; template class BinaryTreeNode { public: T data; BinaryTreeNode* left; BinaryTreeNode* right; BinaryTreeNode(T data){ this->data=data; left=NULL; right=NULL; } ~BinaryTreeNode(){ delete left; delete right; } }; //Traversal techniques //Level order //pre order //post order //in order //inorder traversal void inorder(BinaryTreeNode* root){ if(root==NULL) return; inorder(root->left); cout<data<<" "; inorder(root->right); } //remaining traversals do it by yourself //generating tree with preorder and inorder values given BinaryTreeNode* buildTreeHelper(int* in, int* pre, int inS, int inE, int preS, int preE){ if(inS>inE){ return NULL; } int rootData = pre[preS]; int rootIndex=-1; for(int i=inS;i<=inE;i++) { if(in[i]==rootData){ rootIndex=i; break; } } int lInS=inS; int lInE=rootIndex-1; int lPreS=preS+1; int lPreE=lInE-lInS+lPreS; int rPres=lPreE+1; int rPreE=preE; int rInS=rootIndex+1; int rInE=inE; BinaryTreeNode* root = new BinaryTreeNode(rootData); root->left=buildTreeHelper(in,pre,lInS,lInE,lPreS,lPreE); root->right=buildTreeHelper(in,pre,rInS,rInE,lPreS,lPreE); return root; } BinaryTreeNode* buildTree(int* in, int *pre, int size){ return buildTreeHelper(in,pre,0,size-1,0,size-1); } //printing binary tree void printTree(BinaryTreeNode* root) { if(root==NULL) return; cout<data<<":"; if(root->left){ cout<<"L"<left->data<<" "; } if(root->right){ cout<<"R"<right->data<<" "; } cout<left); printTree(root->right); } //taking input level wise BinaryTreeNode* takeInputLevelwise(){ int rootData; cout<<"Enter root data"<>rootData; if(rootData==-1) return NULL; BinaryTreeNode* root = new BinaryTreeNode(rootData); queue*> pendingNodes; pendingNodes.push(root); while(pendingNodes.size() != 0){ BinaryTreeNode* front = pendingNodes.front(); pendingNodes.pop(); cout<<"Enter left child of "<data<>leftChildData; if(leftChildData!=-1){ BinaryTreeNode* child = new BinaryTreeNode(leftChildData); front->left=child; pendingNodes.push(child); } cout<<"Enter right child of "<data<>rightChildData; if(rightChildData!=-1){ BinaryTreeNode* child = new BinaryTreeNode(rightChildData); front->right=child; pendingNodes.push(child); } } return root; } //counting nodes int numNodes(BinaryTreeNode* root){ if(root==NULL) return 0; return 1+numNodes(root->left)+numNodes(root->right); }; // 1 2 3 4 5 6 7 -1 -1 -1 -1 8 9 -1 -1 -1 -1 -1 -1 int main() { //binary tree creation using inorder and preorder int in[]={4,2,5,1,8,6,9,3,7}; int pre[]={1,2,4,5,3,6,8,9,7}; BinaryTreeNode* root=buildTree(in,pre,9); //levelwise input //BinaryTreeNode* root=takeInputLevelwise(); cout<<"Total number of nodes: "<