当前位置:   article > 正文

【数据结构】二叉树DFS(深度优先搜索)_c++ 深度优先搜索(dfs)二叉树

c++ 深度优先搜索(dfs)二叉树

静态实现(数组)

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct Node{
    char value;
    int lson, rson;
}tree[N];
int index=1;
int newNode(char val)
{
    tree[index].value=val;
    tree[index].lson=tree[index].rson=0;
    return index++;
}
void insert(int &father,int child,int l_r){
    if(l_r==0)
        tree[father].lson=child;
    else
        tree[father].rson=child;

}

int dfn[N]={0};
int dfn_timer=0;

void dfn_order(int father)
{
    if(father!=0){
        dfn[father]=++dfn_timer;
        printf("dfn[%c]=%d;",tree[father].value,dfn[father]);
        dfn_order(tree[father].lson);
        dfn_order(tree[father].rson);
    }
}

int visit_timer=0;
void visit_order(int father)
{
    if(father!=0){
        printf("visit[%c]=%d;",tree[father].value,++visit_timer);
        visit_order(tree[father].lson);
        visit_order(tree[father].rson);
        printf("visit[%c]=%d;",tree[father].value,++visit_timer);
    }
}

int deep[N]={0};
int deep_timer=0;
void deep_node(int father)
{
    if(father!=0)
    {
        deep[father]=++deep_timer;
        printf("deep[%c]=%d;",tree[father].value,deep[father]);
        deep_node(tree[father].lson);
        deep_node(tree[father].rson);
        deep_timer--;
    }
}

int num[N]={0};
int num_node(int father){
    if(father==0)
        return 0;
    else
    {
        num[father]=num_node(tree[father].lson)+num_node(tree[father].rson)+1;
        printf("num[%c]=%d;",tree[father].value,num[father]);
        return num[father];
    }
}

void preorder(int father)
{
    if(father!=0) {
        cout << tree[father].value << " ";
        preorder(tree[father].lson);
        preorder(tree[father].rson);
    }
}

void inorder(int father)
{
    if(father!=0) {
        inorder(tree[father].lson);
        cout << tree[father].value << " ";
        inorder(tree[father].rson);
    }
}

void postorder(int father)
{
    if(father!=0) {
        postorder(tree[father].lson);
        postorder(tree[father].rson);
        cout << tree[father].value << " ";
    }
}

int buildtree()
{
    int A=newNode('A');
    int B=newNode('B');
    int C=newNode('C');
    int D=newNode('D');
    int E=newNode('E');
    int F=newNode('F');
    int G=newNode('G');
    int H=newNode('H');
    int I=newNode('I');
    insert(E,B,0);
    insert(E,G,1);
    insert(B,A,0);
    insert(B,D,1);
    insert(G,F,0);
    insert(G,I,1);
    insert(D,C,0);
    insert(I,H,0);
    int root=E;
    return root;
}
int main()
{
    int root=buildtree();
    cout<<"dfn_order:";
    dfn_order(root);
    cout<<endl;
    cout<<"visit_order:";
    visit_order(root);
    cout<<endl;
    cout<<"deep_node:";
    deep_node(root);
    cout<<endl;
    cout<<"num_node:";
    num_node(root);
    cout<<endl;
    cout<<"preorder:";
    preorder(root);
    cout<<endl;
    cout<<"inorder:";
    inorder(root);
    cout<<endl;
    cout<<"postorder:";
    postorder(root);
    cout<<endl;
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147

动态实现(链表)

#include <bits/stdc++.h>
using namespace std;
struct node{
    char value;
    node* left;
    node* right;
    node(char value='#',node *left=NULL,node* right=NULL):value(value),left(left),right(right){}
};
void preorder(node *root){
    if(root!=NULL)
    {
        cout<<root->value<<" ";
        preorder(root->left);
        preorder(root->right);
    }
}
void inorder(node *root){
    if(root!=NULL)
    {
        preorder(root->left);
        cout<<root->value<<" ";
        preorder(root->right);
    }
}
void postorder(node *root){
    if(root!=NULL)
    {
        preorder(root->left);
        preorder(root->right);
        cout<<root->value<<" ";
    }
}
void remove_tree(node* root){
    if(root==NULL)
        return;
    remove_tree(root->left);
    remove_tree(root->right);
    delete root;
}
int main()
{
    node *A=new node('A');
    node *B=new node('B');
    node *C=new node('C');
    node *D=new node('D');
    node *E=new node('E');
    node *F=new node('F');
    node *G=new node('G');
    node *H=new node('H');
    node *I=new node('I');
    E->left=B;E->right=G;
    B->left=A;B->right=D;
    G->left=F;G->right=I;
    D->left=C;I->left=H;
    cout<<"Preorder: ";preorder(E);cout<<endl;
    cout<<"Inorder: ";inorder(E);cout<<endl;
    cout<<"Postorder: ";postorder(E);cout<<endl;
    remove_tree(E);
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/888011
推荐阅读
相关标签
  

闽ICP备14008679号