void mid_travel(boost::shared_ptr<tree> root) { if (root == nullptr) { cout << "root is nullptr" << endl; } else { mid_travel(root->left); cout << root->data << endl; mid_travel(root->right); } }
int main(){ int n; int data; Node_id = 0; while(scanf("%d",&n)!=EOF){ //for(int i=0;i<n;i++){ // scanf("%d",&data[i]); //} Node *root=NULL; for(int j=0;j<n;j++){ scanf("%d",&data); root = compare(root,data); } pro_travel(root); printf("\n"); mid_travel(root); printf("\n"); end_travel(root); printf("\n"); //delete_tree(root); }//end while return 0; }
void test_tree() { //中序遍历为:123654 boost::shared_ptr<tree> node1 = boost::shared_ptr<tree>(new tree(1)); boost::shared_ptr<tree> node3 = boost::shared_ptr<tree>(new tree(3)); boost::shared_ptr<tree> node4 = boost::shared_ptr<tree>(new tree(4)); boost::shared_ptr<tree> node2 = boost::shared_ptr<tree>(new tree(2)); node2->left = node1; node2->right = node3; boost::shared_ptr<tree> node6 = boost::shared_ptr<tree>(new tree(6)); node6->left = node2; boost::shared_ptr<tree> node5 = boost::shared_ptr<tree>(new tree(5)); node6->right = node5; node5->right = node4; mid_travel(node6); boost::shared_ptr<tree> test1=common_ancestor(node6, node1, node3); cout << test1->data << endl; boost::shared_ptr<tree> test2=common_ancestor(node6, node3, node4); cout << test2->data << endl; boost::shared_ptr<tree> test3 = common_ancestor(node6, node4, node5); cout << test3->data << endl; boost::shared_ptr<tree> test4 = common_ancestor(node6, node2, node3); cout << test4->data << endl; }
void mid_travel(Node* root){ if(root->Lchild!=NULL)mid_travel(root->Lchild); printf("%d ",root->data); if(root->Rchild!=NULL)mid_travel(root->Rchild); }