广度优先遍历(BFS)例题

谁践踏了优雅 2022-05-05 09:37 311阅读 0赞

二叉树的层次遍历 UVa122

题目:

70

转载:树的层次遍历,紫书P150UVa122

一、输入数据的处理:

70 1

此处两次用到的c语言字符串的灵活性——可以把任何指向字符的指针看成一个字符串,从该位置开始直到‘\0’结束的字符串。例如,若读到的字符串节点是“(11,LL)”,则‘&s[1]’代表的含义是字符串“11,LL)”。函数strchr(s,’,’)返回字符串中从左往右第一个‘,’字符的指针,因此strchr(s,’,’)所对应的字符串为“LL)”。这样实际调用的是addnode(11,”LL”)。

二、二叉树基本结构的定义:

struct Node{
bool have_value;
int v;
Node* left, *right;
Node():have_value(false),left(NULL),right(NULL){}
};

三、添加节点操作

70 2

四、层序遍历(BFS)、

70 3

五、递归释放二叉树(防止内存泄漏)

70 4

最后贴上完整程序源代码:

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <vector>
  5. #include <queue>
  6. using namespace std;
  7. //树结点
  8. struct Node{
  9. int v ;
  10. Node* left,*right ;
  11. int have_value ;
  12. Node():have_value(false),left(NULL),right(NULL){} ;
  13. } ;
  14. Node* root ;//根节点
  15. Node* newnode(){
  16. return new Node() ; //返回一个新结点
  17. }
  18. bool failed ;
  19. void addnode(int v,char* s){//添加新结点
  20. int n = strlen(s);
  21. Node* u = root ;
  22. for(int i = 0;i < n;i++)//找到要加入的位置
  23. {
  24. if(s[i] == 'L'){
  25. if(u->left == NULL) u->left = newnode();
  26. u = u->left;
  27. }
  28. else if(s[i] == 'R'){
  29. if(u->right == NULL) u->right= newnode();
  30. u = u->right ;
  31. }
  32. }
  33. if(u->have_value) failed = true ;//是否已经被访问过;
  34. u->v = v;
  35. u->have_value = true;
  36. }
  37. void freetree(Node* u){ //释放内存
  38. if(u == NULL) return ;
  39. freetree(u->left);
  40. freetree(u->right);
  41. delete u;
  42. }
  43. char s[1005];
  44. bool read_input(){
  45. failed = false ;
  46. freetree(root) ;
  47. root = newnode();
  48. while(true){
  49. if(scanf("%s", s) != 1) return false;
  50. if(!strcmp(s,"()")) break;
  51. int v ;
  52. sscanf(&s[1],"%d",&v);
  53. addnode(v,strchr(s,',')+1);
  54. }
  55. return true ;
  56. }
  57. bool bfs(vector<int>& ans){//搜索
  58. queue<Node*> q;
  59. ans.clear();
  60. q.push(root);
  61. while(!q.empty()){
  62. Node *u = q.front();q.pop();
  63. if(!u->have_value) return false;
  64. ans.push_back(u->v);
  65. if(u->left != NULL) q.push(u->left);
  66. if(u->right != NULL) q.push(u->right);
  67. }
  68. return true ;
  69. }
  70. int main(int argc, char *argv[])
  71. {
  72. vector<int> ans;
  73. while(read_input()){
  74. if(!bfs(ans)) failed = 1;
  75. if(failed) printf("not complete\n");
  76. else{
  77. for(int i = 0;i < ans.size();i++)
  78. {
  79. if(i != 0) cout << " " ;
  80. cout << ans[i];
  81. }
  82. cout << endl ;
  83. }
  84. }
  85. return 0;
  86. }

上面的代码写的比我的好些,但还是习惯性贴上自己的代码:

  1. #include<iostream>
  2. #include<string>
  3. #include<queue>
  4. #include<vector>
  5. using namespace std;
  6. struct node//构造树的节点,下面的构造函数是非常好的
  7. {
  8. bool value;
  9. int v;
  10. node *left;
  11. node *right;
  12. node():value(false),v(0),left(NULL),right(NULL){}
  13. };
  14. node *root=new node;//创建一个根节点,便于创建和遍历
  15. //一定要开辟空间
  16. int v;
  17. string com_s;
  18. /*
  19. void print(node *u)
  20. {
  21. cout<<endl<<u->value;
  22. cout<<" "<<u->v;
  23. if(u->left)
  24. cout<<" "<<"left exit";
  25. if(u->right)
  26. cout<<" "<<"right exit";
  27. }
  28. */
  29. void extract(string &in_s)//提取命令
  30. {
  31. int i;
  32. v=0;
  33. com_s.clear();//清空是clear,不是empty()。大爷的,查这么长时间查不出
  34. for(i=1;isdigit(in_s[i]);i++)//s[0]为(
  35. v=v*10+(in_s[i]-'0');
  36. //跳出循环时,读取的是,的位置
  37. i++;
  38. for(i;in_s[i]!=')';i++)
  39. com_s.push_back(in_s[i]);
  40. }
  41. void creat_node()
  42. {
  43. node *u=root;
  44. for(int i=0;com_s[i];i++)
  45. {
  46. if(com_s[i]=='L')
  47. {
  48. if(u->left==NULL)
  49. u->left=new node;
  50. u=u->left;
  51. }
  52. else if(com_s[i]=='R')//如果右子树的空间已经存在
  53. {
  54. if(u->right==NULL)
  55. u->right=new node;
  56. u=u->right;
  57. }
  58. }
  59. u->v=v;
  60. if(!u->value)
  61. u->value=true;
  62. }
  63. bool bfs(vector<int> &ans)
  64. {
  65. ans.clear();
  66. queue<node*> q;
  67. q.push(root);
  68. while(!q.empty())
  69. {
  70. node* u=q.front();
  71. if(!u->value) return false;
  72. ans.push_back(u->v); //print(u);
  73. q.pop();
  74. if(u->left) q.push(u->left);
  75. if(u->right) q.push(u->right);
  76. }
  77. return true;
  78. }
  79. void remove_tree(node *u)
  80. {
  81. if(u==NULL) return;
  82. remove_tree(u->left);
  83. remove_tree(u->right);
  84. delete u;
  85. }
  86. int main()
  87. {
  88. string in_s;
  89. vector<int> ans;
  90. /*输入信息,建立树*/
  91. while(cin>>in_s)
  92. {
  93. if(in_s=="()") break;
  94. extract(in_s);//从中提取命令放入v和com_s中
  95. // cout<<v;
  96. //cout<<com_s;
  97. creat_node();//创建节点,生成树
  98. //system("pause");
  99. }
  100. /*广度遍历树*/
  101. if(bfs(ans))
  102. {
  103. int n=ans.size();
  104. for(int i=0;i<n;i++)
  105. cout<<ans[i]<<" ";
  106. }
  107. else
  108. cout<<-1;
  109. remove_tree(root);
  110. return 0;
  111. }

发表评论

表情:
评论列表 (有 0 条评论,311人围观)

还没有评论,来说两句吧...

相关阅读