我有一个向量< int> 具有二叉树的中序遍历。现在我使用 qsort 对其进行排序。之后,当我尝试打印向量的内容时,运行时出现以下错误。
[main] C:\General_algorithms\binaryTreeToBST.exe 1000 (0) handle_exceptions
: Exception: STATUS_ACCESS_VIOLATION
[main] binaryTreeToBST 1000 (0) handle_exceptions: Dumping stack trace to binary
TreeToBST.exe.core
代码如下:
struct BinTreeNode
{
int id;
BinTreeNode *left;
BinTreeNode *right;
BinTreeNode(int a)
{
id=a;
left=right=NULL;
}
};
int compare (const void *a, const void *b)
{
return ( *(int*)a - *(int*)b );
}
int main()
{
BinTreeNode n10(10);
BinTreeNode n2(2);
BinTreeNode n7(7);
BinTreeNode n8(8);
BinTreeNode n4(4);
n10.left=&n2;
n10.right=&n7;
n2.left= &n8;
n2.right= &n4;
cout<<"OK TILL HERE1"<<endl;
vector<int> InTr= inorder(n10); //returns a vector containing the inorder traversal of the tree.
cout<<"OK TILL HERE2"<<endl;
if(InTr.size()!=0)
qsort(&InTr, InTr.size(), InTr[0], compare);
cout<<"OK TILL HERE3"<<endl; //THIS GETS PRINTED
cout<<InTr[0]; //THIS DOESN'T GET PRINTED(Error occurs)
for(int i=0;i<InTr.size();i++)
cout<<InTr[i]<<endl;
cout<<"OK TILL HERE4"<<endl; //THIS DOESN'T GET PRINTED
return 0;
}