[問題] 子濬幫幫我-.- (上次的樹搜尋)
其他人會幫我也行 拜託-.-
這次這是我新拼的 可是我資結嚴重不行
1.想請問這份程式跑出來的結果是不是對的
2.這份程式碼後半部是搜尋函式 可是它假如搜尋不到不會跑出任何東西
我想要它跑出"false"可是都改不出來...
--------------------------程式碼*---------------------------------
#include<iostream>
#include<cstdlib>
#include<iomanip>
#define ArraySize 20
using namespace std;
struct Node
{
int data;
struct Node *left_Node;
struct Node *right_Node;
int lc,rc;
};
typedef struct Node TreeNode;
typedef struct Node *BinaryTree;
BinaryTree rootNode;
void Add_Node_To_Tree (int);
void showtree (BinaryTree,int);
void searchtree (int);
void print_inorder();
void inorder(Node *);
void print_preorder();
void preorder(Node *);
void print_postorder();
void postorder(Node *);
int main ()
{
int i=0;
int content[ArraySize];
int tempdata, rank;
int choice;
rootNode = NULL;
start:
do
{
cout << "=========================" << endl;
cout << "1.Insert number. " << endl;
cout << "2.Search Binary Tree. " << endl;
cout << "3.In-Order Traversal " <<endl;
cout << "4.Pre-Order Traversal " <<endl;
cout << "5.Post-Order Traversal " <<endl;
cout << "6.Exit. " << endl;
cout << "=========================" << endl;
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1;
cout << "請連續輸入20筆資料:" << endl;
for(i=0;i<ArraySize;i++)
{
cout << "請輸入第" << setw(1) << (i+1) << "筆資料:";
cin >> tempdata;
content[i]=tempdata;
}
for(i=0;i<ArraySize;i++)
Add_Node_To_Tree (content[i]);
break;
case 2:
cout << "You want to find the number : ";
cin >> rank;
cout << endl << endl;
searchtree(rank);
break;
case 3 : cout<<endl;
cout<<" In-Order Traversal "<<endl;
cout<<" -------------------"<<endl;
print_inorder();
break;
case 4 : cout<<endl;
cout<<" Pre-Order Traversal "<<endl;
cout<<" -------------------"<<endl;
print_preorder();
break;
case 5 : cout<<endl;
cout<<" Post-Order Traversal "<<endl;
cout<<" --------------------"<<endl;
print_postorder();
break;
case 6:
cout << "Good bye." << endl;
break;
}
} while(choice != 6);
system("PAUSE");
return 0;
}
void Add_Node_To_Tree (int data)
{
BinaryTree currentNode;
BinaryTree newnode;
newnode = (BinaryTree) malloc (sizeof(TreeNode));
newnode -> data = data;
newnode -> left_Node = NULL;
newnode -> right_Node = NULL;
newnode -> lc = 0;
newnode -> rc = 0;
if (rootNode == NULL)
{
rootNode = newnode;
}
else
{
currentNode = rootNode;
while (1)
{
if (newnode -> data == currentNode -> data)
{
cout << "The number has entered." << endl;
break;
}
else if (data < currentNode -> data)
{
if (currentNode -> left_Node == NULL)
{
currentNode -> lc ++;
currentNode -> left_Node = newnode;
break;
}
else
{
currentNode = currentNode -> left_Node;
}
}
else
{
if (currentNode -> right_Node == NULL)
{
currentNode -> rc ++;
currentNode -> right_Node = newnode;
break;
}
else
{
currentNode = currentNode -> right_Node;
}
}
}
}
}
void print_inorder()
{
inorder(rootNode);
}
void inorder(Node * p)
{
if(p != NULL)
{
if(p->left_Node) inorder(p->left_Node);
cout<<" "<<p->data<<" ";
if(p->right_Node) inorder(p->right_Node);
}
else return;
}
void print_preorder()
{
preorder(rootNode);
}
void preorder(Node * p)
{
if(p != NULL)
{
cout<<" "<<p->data<<" ";
if(p->left_Node) preorder(p->left_Node);
if(p->right_Node) preorder(p->right_Node);
}
else return;
}
void print_postorder()
{
postorder(rootNode);
}
void postorder(Node * p)
{
if(p != NULL)
{
if(p->left_Node) postorder(p->left_Node);
if(p->right_Node) postorder(p->right_Node);
cout<<" "<<p->data<<" ";
}
else return;
}
void searchtree (int data)
{
int temp = 0;
BinaryTree currentNode;
currentNode = rootNode;
while (currentNode != NULL)
{
if (currentNode -> data < data)
{
temp += currentNode->lc + 1;
currentNode = currentNode -> right_Node;
}
else if (currentNode -> data > data)
{
currentNode = currentNode -> left_Node;
}
else if (currentNode -> data == data)
{
cout<<"we find it"<<endl;
cout<<"The rank of "<<data<<" is "<<currentNode->lc + 1 + temp<<endl;
return;
}
}
}
--------------------------------------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.33.233.75
→
07/06 02:14, , 1F
07/06 02:14, 1F
→
07/06 20:55, , 2F
07/06 20:55, 2F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):