zoukankan      html  css  js  c++  java
  • 二叉树

     前序输入:A、B、C、#、#、#、D、#、#

    二叉树的实现以及四种遍历:

      1 #include <iostream>
      2 #include <string>
      3 #include <queue>
      4 using namespace std;
      5 
      6 template<class T>
      7 struct BiNode {
      8     T data;
      9     BiNode<T> *lchild, *rchild;//左子树、右子树
     10 };
     11 
     12 template<class T>
     13 class BiTree
     14 {
     15 public:
     16     BiTree();//构造函数,初始化二叉树,前序序列由键盘输入
     17     ~BiTree();//析构函数,释放二叉链表中的各结点的存储空间
     18     BiNode<T>* Getroot();//获得指向根节点的指针
     19     void PreOrder(BiNode<T>* root);//前序遍历二叉树
     20     void InOrder(BiNode<T>* root);//中序遍历二叉树
     21     void PostOrder(BiNode<T>* root);//后序遍历二叉树
     22     void LeverOrder(BiNode<T>* root);//层序遍历二叉树
     23 private:
     24     BiNode<T> *root;//指向根节点的头指针
     25     BiNode<T> *Great();//有参构造函数调用
     26     void ReLease(BiNode<T> *root);//析构函数调用
     27 };
     28 template<class T>
     29 BiTree<T>::BiTree() {
     30     this->root = Great();//创建对象时,构造函数被调用后,调用Great函数进行初始化
     31 }
     32 
     33 template<class T>
     34 BiTree<T>::~BiTree() {
     35     ReLease(root);//调用ReLease释放二叉树链表
     36 }
     37 
     38 template<class T>
     39 BiNode<T>* BiTree<T>::Getroot() {
     40     return root;//返回根节点地址
     41 }
     42 
     43 template<class T>
     44 void BiTree<T>::PreOrder(BiNode<T>* root) {
     45     if (root == NULL)
     46         return;
     47     cout << root->data << " ";
     48     PreOrder(root->lchild);//前序遍历二叉树(根,左,右)
     49     PreOrder(root->rchild);
     50 }
     51 
     52 template<class T>
     53 void BiTree<T>::InOrder(BiNode<T> *root) {
     54     if (root == NULL)
     55         return;
     56     InOrder(root->lchild);//中序遍历二叉树(左,根,右)
     57     cout << root->data << " ";
     58     InOrder(root->rchild);
     59 }
     60 
     61 template<class T>
     62 void BiTree<T>::PostOrder(BiNode<T>* root) {
     63     if (root == NULL)
     64         return;
     65     PostOrder(root->lchild);//后序遍历二叉树(左,右,根)
     66     PostOrder(root->rchild);
     67     cout << root->data << " ";
     68 }
     69 
     70 template<class T>
     71 void BiTree<T>::LeverOrder(BiNode<T>* root) {
     72     BiNode<T> *p;
     73     if (root == NULL)
     74         return;
     75     queue<BiNode<T>*> q;//include<queue>库,queue类型为BiNode<T>*,函数push,empty,pop,front
     76     q.push(root);
     77     while (!q.empty()) {
     78         p = q.front();
     79         cout << p->data << " ";
     80         q.pop();
     81         if (p->lchild != NULL) {
     82             q.push(p->lchild);
     83         }
     84         if (p->rchild != NULL) {
     85             q.push(p->rchild);
     86         }
     87     }
     88 }
     89 
     90 template<class T>
     91 BiNode<T>* BiTree<T>::Great() {
     92     BiNode<T>* root;
     93     T ch;
     94     cout << "请输入创建的一颗二叉树的结点数据:" << endl;
     95     cin >> ch;
     96     if (ch == "#")
     97         root = NULL;
     98     else {
     99         root = new BiNode<T>;
    100         root->data = ch;
    101         root->lchild = Great();
    102         root->rchild = Great();
    103     }
    104     return root;
    105 }
    106 
    107 template<class T>
    108 void BiTree<T>::ReLease(BiNode<T>* root) {
    109     if (root != NULL) {
    110         ReLease(root->lchild);
    111         ReLease(root->rchild);
    112         delete root;
    113     }
    114 }
    115 
    116 
    117 int main()
    118 {
    119     BiTree<string> bt;//创建一棵树
    120     BiNode<string>* root = bt.Getroot();
    121     
    122     cout << "------前序遍历------" << endl;
    123     bt.PreOrder(root);
    124     cout << endl;
    125     cout << "------后序遍历------" << endl;
    126     bt.PostOrder(root);
    127     cout << endl;
    128     cout << "------中序遍历------" << endl;
    129     bt.InOrder(root);
    130     cout << endl;
    131     cout << "------层序遍历------" << endl;
    132     bt.LeverOrder(root);
    133     cout << endl;
    134 
    135     system("pause");
    136     return 0;
    137 }
    View Code

    新增功能

    1)复制二叉树

     1 template<class T>
     2 BiNode<T>* BiTree<T>::Copy(BiNode<T>* extN, BiNode<T>* newN)
     3 {
     4     if (extN == NULL)
     5     {
     6         return NULL;
     7     }
     8     else
     9     {
    10         newN = new BiNode<T>;
    11         newN->data = extN->data;
    12         newN->lchild = Copy(extN->lchild, newN->lchild);
    13         newN->rchild = Copy(extN->rchild, newN->rchild);
    14         return newN;
    15     }
    16 }
    17 
    18 //...
    19     BiNode<string>* root2 = NULL;
    20     root2 = bt.Copy(root, root2);
    21     cout << "前序输出" << endl;
    22     bt.PreOrder(root2);
    View Code

    2)统计结点总数

    1 template<class T>
    2 int BiTree<T>::get_count(BiNode<T>* root) {
    3     if (root == NULL)
    4         return 0;
    5     else
    6         return get_count(root->lchild) + get_count(root->rchild) + 1;
    7 }
    View Code

    3)统计叶子结点数目

    1 template<class T>
    2 int BiTree<T>::get_leafNum(BiNode<T>* root) {
    3     if (root == NULL)
    4         return 0;
    5     if (root->lchild == NULL&&root->rchild == NULL)
    6         return 1;
    7     else
    8         return get_leafNum(root->lchild) + get_leafNum(root->rchild);
    9 }
    View Code

     4)求树的深度

     1 template<class T>
     2 int BiTree<T>::get_height(BiNode<T>* root) {
     3     if (root == NULL)
     4         return 0;
     5     else
     6     {
     7         int h_left  = get_height(root->lchild);
     8         int h_right = get_height(root->rchild);
     9         int max = h_left;
    10         if (max < h_right) { max = h_right; }
    11         return max + 1;
    12     }
    13 }
    View Code
  • 相关阅读:
    POJ 1887 Testing the CATCHER
    HDU 3374 String Problem
    HDU 2609 How many
    POJ 1509 Glass Beads
    POJ 1458 Common Subsequence
    POJ 1159 Palindrome
    POJ 1056 IMMEDIATE DECODABILITY
    POJ 3080 Blue Jeans
    POJ 1200 Crazy Search
    软件体系结构的艺术阅读笔记1
  • 原文地址:https://www.cnblogs.com/guoyujiang/p/11880137.html
Copyright © 2011-2022 走看看