test
2015-09-23 23:29
225 查看
/* 深度优先搜索用栈(stack)来实现,整个过程可以想象成一个倒立的树形: 1、把根节点压入栈中。 2、每次从栈中弹出一个元素,搜索所有在它下一级的元素,把这些元素压入栈中。并把这个元素记为它下一级元素的前驱。 3、找到所要找的元素时结束程序。 4、如果遍历整个树还没有找到,结束程序。 广度优先搜索使用队列(queue)来实现,整个过程也可以看做一个倒立的树形: 1、把根节点放到队列的末尾。 2、每次从队列的头部取出一个元素,查看这个元素所有的下一级元素,把它们放到队列的末尾。并把这个元素记为它下一级元素的前驱。 3、找到所要找的元素时结束程序。 4、如果遍历整个树还没有找到,结束程序 */ #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <stack> #include <queue> #include <iostream> using namespace std; #define T char; int index = 0; struct Node{ char data; int pre; struct Node *lchild; struct Node *rchild; }; int TreeBrand(Node* root) { queue<Node*>Nodequeue; root->pre = 0; Nodequeue.push(root); Node * TreeNode; int brand[100] = { 0 }; while (Nodequeue.size() != 0) { TreeNode = Nodequeue.front(); Nodequeue.pop(); brand[TreeNode->pre]++; if (TreeNode->lchild) { TreeNode->lchild->pre = TreeNode->pre + 1; Nodequeue.push(TreeNode->lchild); } if (TreeNode->rchild) { TreeNode->rchild->pre = TreeNode->pre + 1; Nodequeue.push(TreeNode->rchild); } } int max = 0; for (int i = 0; i < 100; i++) { if (brand[i]> max) max = brand[i]; } return max; } void TreeNodeConstruct(Node* &root, char Data[])//(Node* root, char Data[])不能通过,root指针未初始化 { char value = Data[index++]; if (value == '#') root = NULL; else { root = (Node*)malloc(sizeof(Node));//申请sizeof(Node)大小的Node类型的内存空间,并将这连续的存储单元的首地址赋给root root->data = value; TreeNodeConstruct(root->lchild, Data);//递归构建左子树 TreeNodeConstruct(root->rchild, Data);//递归构建右子树 } } void DFS(Node* root) { stack<Node*> NodeStack;//stl模板,存的是Node类型的指针 NodeStack.push(root); Node * TreeNode; while (!NodeStack.empty()) { TreeNode = NodeStack.top();//取栈顶元素 cout << TreeNode->data << endl;//遍历根节点 NodeStack.pop();//栈顶元素出栈 if (TreeNode->rchild)//将右孩子压栈,先进后出,右节点后遍历 NodeStack.push(TreeNode->rchild); if (TreeNode->lchild) NodeStack.push(TreeNode->lchild); } } void BFS(Node* root) { queue<Node*> NodeQueue;//stl模板 NodeQueue.push(root); Node* TreeNode; while (!NodeQueue.empty()) { TreeNode = NodeQueue.front(); cout << TreeNode->data << endl; NodeQueue.pop(); if (TreeNode->lchild) NodeQueue.push(TreeNode->lchild);//队列,将左子树入队列,左子树先遍历 if (TreeNode->rchild) NodeQueue.push(TreeNode->rchild); } } void InOrder_Tree_Search(Node* root) { if (root != NULL) { InOrder_Tree_Search(root->lchild); cout << root->data << endl; InOrder_Tree_Search(root->rchild); } } int TreeDepth(Node * root) { if (root == NULL) return 0; int nLeft = TreeDepth(root->lchild); int nRight = TreeDepth(root->rchild); return (nLeft > nRight) ? (nLeft + 1) : (nRight + 1); } void main() { char Data[15] = { 'A', 'B', 'D', '#', '#', 'E', '#', '#', 'C', 'F', '#', '#', 'G', '#', '#' }; Node * root; TreeNodeConstruct(root, Data);//此时对root指针没有初始化,但是在调用函数中加入&引用变量,就可以通过??? cout << "Depth First Search" << endl; DFS(root); cout << "Brand First Search" << endl; BFS(root); cout << "InOder Tree_Search" << endl; InOrder_Tree_Search(root); cout << TreeDepth(root) << endl; cout << TreeBrand(root) << endl; }
相关文章推荐
- c# windows form 生命周期 (Life cycle) 事件 顺序
- android WebView的缓存模式、JS交互
- Caffe安装 Ubuntu14.04
- JAVA学习第二天
- Codeforces Round #321 (Div. 2) E - Kefa and Watch(hash + 线段树)
- 转 JVM内存分析及导致内存溢出的不健壮代码及解决办法
- 1044. 火星数字(20)
- MySQL的常见SQL语句范例
- Swift代码实现加载WEBVIEW
- C++中getline被跳过
- Android基础学习之navigate(导航)
- 乱记-2
- .9.png 转载地址http://www.cnblogs.com/lwbqqyumidi/p/3373070.html
- TCP/UDP编程
- POJ 2778 DNA Sequence (AC自动机+矩阵快速幂)
- C语言求解素数(质数)的优化:打印100-200之间的素数
- 【小游戏】C++手工制作贪吃蛇
- 【小游戏】C++手工制作贪吃蛇
- iOS-Core-Animation-Advanced-Techniques(四-5)
- Android--PullToRefreshListView的使用