二叉树

#include<iostream>
//
// Created by jt on 2020/8/22.
//
#include <vector>
#include <queue>

using namespace std;
class BiNode {
public:
    int data;
    BiNode* lchild, * rchild;
};
class BiTree {
public:
    BiTree();
    void createBiTree(BiNode*& temp, int data);
    void preOrder(BiNode* temp);
    void LevelTree(BiNode* temp);
    int Depth(BiNode* temp);
    int NodeCount(BiNode* temp);
    int LeafCount(BiNode* temp);
    BiNode* SerchTree(BiNode* temp, int key);
    BiNode* Tre;


};
BiTree::BiTree() {
    Tre = NULL;
}
BiNode* BiTree::SerchTree(BiNode* temp, int key) {
    if (temp == NULL || temp->data == key) return temp;
    else {
        if (temp->data > key) return SerchTree(temp->lchild, key);
        else return SerchTree(temp->rchild, key);
    }
}
void BiTree::createBiTree(BiNode*& temp, int data) {
    if (temp == NULL) {
        temp = new BiNode;
        temp->data = data;
        temp->lchild = NULL;
        temp->rchild = NULL;
    }
    else if (temp->data == data) {
        return;
    }
    else if (temp->data < data) {
        createBiTree(temp->rchild, data);
    }
    else if (temp->data > data) {
        createBiTree(temp->lchild, data);
    }
}
void BiTree::preOrder(BiNode* temp)
{
    if (temp != NULL) {
        cout << temp->data << " ";
        preOrder(temp->lchild);
        preOrder(temp->rchild);

    }
}

int BiTree::Depth(BiNode* temp) {
    if (temp == 0) return 0;
    else {
        int n = 0, m = 0;
        n = Depth(temp->lchild);
        m = Depth(temp->rchild);
        if (m > n) return m + 1;
        else return n + 1;
    }
}
int BiTree::NodeCount(BiNode* temp) {
    if (temp == NULL) return 0;
    else return NodeCount(temp->lchild) + NodeCount(temp->rchild) + 1;
}
int BiTree::LeafCount(BiNode* temp) {
    if (temp == NULL) return 0;
    if (temp->lchild == NULL && temp->rchild == NULL) return 1;
    else return LeafCount(temp->lchild) + LeafCount(temp->rchild);

}


void BiTree::LevelTree(BiNode* temp) {
    queue<BiNode*>q;
    vector<vector<int> > ans;
    q.push(temp);
    //while (q.empty() != true)
    //while (!q.empty() )
    while (q.size()) {
        vector<int> temp1;
        for (int i = q.size(); i > 0; i--) {
            BiNode* tep = q.front(); q.pop();
            temp1.push_back(tep->data);
            //    cout << q.front()->data << "*";
            if (tep->lchild != NULL) q.push(tep->lchild);
            if (tep->rchild != NULL) q.push(tep->rchild);
        }
        ans.push_back(temp1);
    }
    /*   vector<vector<int>>::iterator i;
       vector<int>::iterator it;
       for ( i = ans.begin(); i < ans.end(); ++i) {
           for (it = (*i).begin(); it < (*i).end(); ++it) {
               cout << *it <<" ";
           }
           cout << endl;
       }*/
    for (auto i : ans) {
        for (auto it : i) {
            cout << it << " ";
        }
        cout << endl;
    }

}


/*
void BiTree::LevelTree(BiNode* temp) {
    queue<BiNode*>q;

    q.push(temp);
    //while (q.empty() != true)
    //while (!q.empty() )
    while (q.size())
    {
        vector<int> temp1;
        int size = q.size();
        cout << q.front()->data << "*";
        //    if (q.front()->lchild != NULL) LevelTree(q.front()->lchild);
        //    if (q.front()->rchild != NULL) LevelTree(q.front()->rchild);
        if (q.front()->lchild != NULL) q.push(q.front()->lchild);
        if (q.front()->rchild != NULL) q.push(q.front()->rchild);
        q.pop();
    }
}*/




int main() {
    BiTree* tree = new BiTree;
    int a[9] = { 5,3,2,4,8,9,7,1,10 };
    for (auto i = 0; i < 9; i++) {
        tree->createBiTree(tree->Tre, a[i]);
    }
    tree->LevelTree(tree->Tre);
    cout << endl;
    tree->preOrder(tree->Tre);
    cout << endl;


    cout << tree->Depth(tree->Tre) << "**" << endl;
    cout << tree->NodeCount(tree->Tre) << "* " << endl;
    //cout << tree->SerchTree(tree->Tre, 9)->lchild->data << "** " << endl;
    cout << tree->LeafCount(tree->Tre) << "* " << endl;

    return 0;
}

 

posted @ 2022-03-16 00:20  Dsad123FFFG6645  阅读(15)  评论(0)    收藏  举报