您的位置:首页 > 其它

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: