C++로 트리를 만들고 싶습니다. 오류 또는 경고없이 코드를 컴파일 할 수는 있지만 출력이 표시되지 않습니다.postfix 표현식에서 표현식 트리를 만드는 방법은 무엇입니까?
오류가 inorder fn에 있다고 생각하지만 이것을 제거하는 방법을 모르겠습니다.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree * left;
struct tree * right;
};
typedef struct tree * TREE;
TREE maketree(int x)
{
TREE tree = (TREE)malloc(sizeof(tree));
if(tree == NULL)
{
return NULL;
}
tree->left = tree->right = NULL;
tree->data = x;
return tree;
}
void setleft(TREE tree,int x)
{
if(tree == NULL || tree->left != NULL)
{
cout<<"\t Error !! Inserting New Node At Left Side !\n";
}
else
{
tree->left = maketree(x);
}
}
void setright(TREE tree,int x)
{
if(tree == NULL || tree->right != NULL)
{
cout<<"\t Error !! Inserting New Node At Right Side !\n";
}
else
{
tree->right = maketree(x);
}
}
void inorder(TREE root)
{
if(root != NULL)
{
TREE left=root->left;
TREE right=root->right;
inorder(left);
cout<<root->data;
inorder(right);
}
}
void main()
{
clrscr();
TREE root = NULL,child,parent;
int i,j = 1;
cout<<"Root Of Binary Search Tree :- ";
cin>>i;
root = maketree(i);
cout<<"\n\n";
while(i)
{
cout<<j<<" Node Value:- ";
cin>>i;
if(i < 0)
{
break;
}
parent = child = root;
while((i != parent->data) && (child != NULL))
{
parent = child;
if(i < parent->data)
{
child = parent->left;
}
else
{
child = parent->right;
}
}
if(i == parent->data)
{
cout<<"\t Value "<<i<<" Already Present In BST !!\n";
}
else if(i < parent->data)
{
setleft(parent,i);
}
else
{
setright(parent,i);
}
j++;
}
inorder(root);
getch();
}
일부 예제 입력의 경우 예상 출력은 무엇이고 실제 출력은 무엇입니까? 질문을 수정하여 포함 시키십시오. –
을 보자 마자 대신에을 포함 시켰습니다. 귀하의 메인이 int가 아닌 void를 반환 할 것이라는 것을 알았습니다. 그리고 C++의 유일한 최악의 기능으로 C로 작성하지 마십시오. –
CashCow