首页 > 代码库 > 二叉树的基本操作(递归版)

二叉树的基本操作(递归版)

递归:

    二叉树的创建,遍历,求高度,求结点数,求叶子数。 递归写法很简单,不多说了。

 

  1 #include<iostream>
  2 using namespace std;
  3 
  4 struct Node {
  5     char data;
  6     Node * lchild;
  7     Node * rchild;
  8 };
  9 
 10 class Tree {
 11 public:
 12     Tree();
 13     ~Tree();
 14     Node * getRoot();
 15     void preTree(Node *);
 16     void inTree(Node *);
 17     void postTree(Node *);
 18     int getHigh(Node *);
 19     int getLeaf(Node *);
 20     int getNode_number(Node *);
 21 private:
 22     Node * root;
 23     Node * Create();
 24     void Delete(Node *);
 25 };
 26 
 27 int main() {
 28 
 29     Tree T;
 30     cout << "先序遍历:";
 31     T.preTree(T.getRoot());
 32     cout << endl << "中序遍历:";
 33     T.inTree(T.getRoot());
 34     cout << endl << "后序遍历:";
 35     T.postTree(T.getRoot());
 36     cout << endl << "高度:";
 37     cout << T.getHigh(T.getRoot());
 38     cout << endl << "结点数:";
 39     cout << T.getNode_number(T.getRoot());
 40     cout << endl << "叶子数:";
 41     cout << T.getLeaf(T.getRoot());
 42 
 43     system("pause");
 44 }
 45 
 46 int Tree::getLeaf(Node * root) {
 47     int Left = 0, Right = 0;
 48     if (root->lchild == NULL && root->rchild == NULL) {        // 左右孩子都为空,表示是叶子 ,返回叶子个数 1
 49         return 1;
 50     }
 51     else if (root->lchild == NULL) {    // 左孩子为空,就递归该结点的右子树
 52         Right = getLeaf(root->rchild);
 53         return Right;
 54     }
 55     else if(root->rchild == NULL) {        // 右孩子为空,就递归该结点的左子树
 56         Left = getLeaf(root->lchild);
 57         return Left;
 58     }
 59     else {                                // 左右孩子都不空,就先递归该结点的左子树,然后递归该结点的右子树
 60         Left =  getLeaf(root->lchild);
 61         Right = getLeaf(root->rchild);
 62         return Left + Right;
 63     }
 64 }
 65 
 66 int Tree::getHigh(Node * root) {
 67     int Left = 0, Right = 0;
 68     if (root == NULL) {
 69         return 0;
 70     }
 71     else {
 72         Left = getHigh(root->lchild);
 73         Right = getHigh(root->rchild);
 74         return Left > Right ? (Left + 1) : (Right + 1);
 75     }
 76 }
 77 
 78 int Tree::getNode_number(Node * root) {
 79     int Left = 0, Right = 0;
 80     if (root == NULL) {
 81         return 0;
 82     }
 83     else {
 84         Left = getNode_number(root->lchild);
 85         Right = getNode_number(root->rchild);
 86         return Left + Right + 1;
 87     }
 88 }
 89 
 90 Node * Tree::Create() {
 91     Node * node;
 92     char ch;
 93     cin >> ch;
 94     if (ch == #) {
 95         node = NULL;
 96     }
 97     else {
 98         node = new Node();
 99         node->data =http://www.mamicode.com/ ch;
100         node->lchild = Create();
101         node->rchild = Create();
102     }
103     return node;
104 }
105 
106 void Tree::Delete(Node * root) {
107     if (root != NULL) {
108         Delete(root->lchild);
109         Delete(root->rchild);
110         delete root;
111     }
112 }
113 
114 Tree::Tree() {
115     root = Create();
116 }
117 
118 Tree::~Tree() {
119     Delete(root);
120 }
121 
122 Node * Tree::getRoot() {
123     return root;
124 }
125 
126 void Tree::preTree(Node * root) {
127     if (root == NULL) {
128         return;
129     }
130     else {
131         cout << root->data << " ";
132         preTree(root->lchild);
133         preTree(root->rchild);
134     }
135 }
136 
137 void Tree::inTree(Node * root) {
138     if (root == NULL) {
139         return;
140     }
141     else {
142         inTree(root->lchild);
143         cout << root->data << " ";
144         inTree(root->rchild);
145     }
146 }
147 
148 void Tree::postTree(Node * root) {
149     if (root == NULL) {
150         return;
151     }
152     else {
153         postTree(root->lchild);
154         postTree(root->rchild);
155         cout << root->data << " ";
156     }
157 }

 

二叉树的基本操作(递归版)