首页 > 其他 > 详细

二叉树链式存储结构

时间:2014-06-09 16:30:49      阅读:418      评论:0      收藏:0      [点我收藏+]
  • 二叉链表的C语言描述

  • 基本运算的算法——建立二叉链表、先序遍历二叉树、中序遍历二叉树、后序遍历二叉树、后序遍历求二叉树深度

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
#include<iostream>
#include<cstdio>
using namespace std;
class Tree
{
private:
    struct Node
    {
        char data;
        Node * lchild;
        Node * rchild;
        Node()
        {
            lchild = NULL;
            rchild = NULL;
        }
        Node(char a)
        {
            data = a;
            lchild = NULL;
            rchild = NULL;
        }
    };
    void creatTree(Node* &head)
    {
 
        char a;
        cin>>a;
        if(a == ‘#‘)
        {
            head->lchild = head->rchild = NULL;
            head = NULL;
        }
        else
        {
            head->data = a;
            head->lchild = new Node();
            head->rchild = new Node();
            creatTree(head->lchild);
            creatTree(head->rchild);
        }
    }
    void NLR(Node *head)
    {
        if(head)
        {
            cout<<head->data;
            NLR(head->lchild);
            NLR(head->rchild);
        }
        else cout<<"#";
    }
    void LNR(Node *head)
    {
        if(head)
        {
            LNR(head->lchild);
            cout<<head->data;
            LNR(head->rchild);
        }
        else cout<<"#";
    }
    void LRN(Node * head)
    {
        if(head)
        {
            LRN(head->lchild);
            LRN(head->rchild);
            cout<<head->data;
        }
        else cout<<"#";
    }
public:
    Node * head;
    Node * cur;
    Tree() {}
    Tree(char a)
    {
        Node * tem = new Node(a);
        head = tem;
        cur = tem;
 
    }
    void creatTree()
    {
        head = new Node();
        creatTree(head);
    }
    void NLR()
    {
        if(head)
            NLR(head);
        else cout<<"#";
    }
    void LNR()
    {
        if(head)
            LNR(head);
        else cout<<"#";
    }
    void LRN()
    {
        if(head)
        {
            LRN(head);
        }
        else cout<<"#";
    }
    int BiTreeDeep(Node * head)
    {
        num++;
        int dept = 0;
        if(head)
        {
            int lchilddept = BiTreeDeep(head->lchild);
            int rchilddept = BiTreeDeep(head->rchild);
            dept = lchilddept >= rchilddept ? (lchilddept + 1) : (rchilddept + 1);
        }
        return dept;
    }
};
int main()
{
    Tree dusk;
    dusk.creatTree();
    int cnt = 0;
    dusk.NLR();
    cout<<endl;
    dusk.LNR();
    cout<<endl;
    dusk.LRN();
    cout<<endl;
    cout<<dusk.BiTreeDeep(dusk.head);
}

  

二叉树链式存储结构,布布扣,bubuko.com

二叉树链式存储结构

原文:http://www.cnblogs.com/Duskcl/p/3777400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!