test

/*
深度优先搜索用栈(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;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值