0% found this document useful (0 votes)
17 views

Tree Recovery 2

This document defines a class called NODE that represents nodes in a binary tree. It also defines a class called BUILDTREE that contains methods for building a binary tree from a preorder and inorder traversal, and for performing a postorder traversal to output the tree. The main function gets input for multiple test cases, builds the tree using the BUILDTREE class, and displays the postorder traversal output.

Uploaded by

Khan Jibran Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Tree Recovery 2

This document defines a class called NODE that represents nodes in a binary tree. It also defines a class called BUILDTREE that contains methods for building a binary tree from a preorder and inorder traversal, and for performing a postorder traversal to output the tree. The main function gets input for multiple test cases, builds the tree using the BUILDTREE class, and displays the postorder traversal output.

Uploaded by

Khan Jibran Khan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

// try.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <conio.

h> #include <cstring> #include <ctype.h> #include <stdio.h> #define MAX 26 using namespace std; class NODE { private: char data; NODE *leftchild; NODE *rightchild; public: void setvalue(char temp) { data=temp; } char getvalue() { return data; } void setleftchild(NODE *temp) { leftchild=temp; } NODE* getleftchild() { return leftchild; } void setrightchild(NODE *temp) { rightchild=temp; } NODE* getrightchild() { return rightchild; } }; class BUILDTREE { private: int preindex;

int inindex; NODE *tree_node; public: BUILDTREE() { preindex=inindex=0; tree_node=NULL; } NODE* buildtree(char in[], char pre[], int start, int end) { if(start>end) { return NULL; } tree_node=new NODE(); tree_node->setvalue(pre[preindex++]); tree_node->setleftchild(NULL); tree_node->setrightchild(NULL); if(start==end) { return tree_node; } inindex=search(in,start,end,tree_node->getvalue()); tree_node->setleftchild(buildtree(in,pre,start,inindex-1)); tree_node->setrightchild(buildtree(in,pre,inindex+1,end)); return tree_node; } int search(char in[], int start, int end, char value) { for(int i=start;i<end;i++) { if(in[i]==value) { return i; } } return 0; } void postorder(NODE *temp) { if(temp==NULL) { return ; } postorder(temp->getleftchild()); postorder(temp->getrightchild()); cout<<temp->getvalue(); } };

int _tmain(int argc, _TCHAR* argv[]) { char in[MAX]; char preorder[MAX]; int times,i=1,len; NODE *root; BUILDTREE B; cin>>times; cin.ignore(); while(i<=times) { system("cls"); fgets(preorder,sizeof(preorder),stdin); fgets(in,sizeof(in),stdin); preorder[strlen(preorder)-1]='\0'; in[strlen(in)-1]='\0'; len=strlen(preorder); system("cls"); for(int j=0;j<len;j++) { cout<<preorder[j]; } cout<<" "; for(int j=0;j<len;j++) { cout<<in[j]; } getche(); system("cls"); root=B.buildtree(in,preorder,0,len-1); B.postorder(root); i++; } cout<<"\n\n"; return 0; }

You might also like