Construction of c++ Preordered Binary Tree

Time:2019-9-24

In order to consider the subsequent traversal, the binary tree must first solve the problem of construction. Here we put forward four traversal methods of binary tree construction by precedence (precedence, mid-order, post-order, layer by layer).

First, define the BinaryTreeNode class


#include <iostream>

#include <string>

#include <queue>

using namespace std;

 

template<typename T >class BinaryTree;

template <typename T> class BinaryTreeNode {

public:

  friend class BinaryTree<T>;

  BinaryTreeNode() {

    data = NULL;

    lChild = rChild = NULL;

  }

  BinaryTreeNode(T newdata) {

    this->data = newdata;

    lChild = rChild = NULL;

  }

  T getData() {

    return data;

  }

  BinaryTreeNode<T> * getLeftNode() {

    return lChild;

  }

  BinaryTreeNode<T> * getRightNode() {

    return rChild;

  }

  T data;

  BinaryTreeNode<T>* lChild;

  BinaryTreeNode<T>* rChild;

private:

 

};

View Code

Second, define the BinaryTree class

template <typename T> class BinaryTree {

public:

  BinaryTreeNode<T> *root;

  char* p;

  BinaryTree() { root = NULL; }

  BinaryTree(T data) {

    root = new BinaryTreeNode<T>(data);

    root->lChild = NULL;

    root->rChild = NULL;

  }

  ~BinaryTree() {

    delete root;

  }

 

  // Build a binary tree and return

  BinaryTreeNode<T>* CreateTree() {

    BinaryTreeNode<int>* bt = NULL;

    char t;

    cin >> t;

    if (t == '#')

    {

      return NULL;

    }

    else {

      int num = t - '0';

      bt = new BinaryTreeNode<T>(num);

      bt->lChild = CreateTree();

      bt->rChild = CreateTree();

    }

    return bt;

  }

 

  // Construction of Binary Trees in Preorder

  BinaryTreeNode<T>* PreCreateTree() {

    BinaryTreeNode<int>* bt = NULL;

    if (this->root == NULL)

    {

      Court < < "Please enter the root node (# stands for empty tree):";

    }

    else {

      Cout < < "Please enter the node (# stands for empty tree):";

    }

    char t;

    cin >> t;

    if (t == '#')

    {

      return NULL;

    }

    else {

      int num = t - '0';

      bt = new BinaryTreeNode<T>(num);

      if (this->root == NULL)

      {

        this->root = bt;

      }

      Cout < < bt - > Data < < < left child;"

      bt->lChild = PreCreateTree();

 

      Cout < < bt - > Data < < < the right child;"

      bt->rChild = PreCreateTree();

    }

    return bt;

  }  

 

  Void preOderTraversal (BinaryTreeNode < T > * bt); //Preorder traversal

  Void in OrderTraversal (BinaryTreeNode < T > * bt); //Intermediate traversal

  Void postOrderTraversal (BinaryTreeNode<T>*bt); //Postorder traversal

  Void level Traversal (BinaryTreeNode < T > * bt); // Layer-by-layer traversal

 

private:

 

};

 

template <typename T>

void BinaryTree<T>::preOderTraversal(BinaryTreeNode<T> *bt) {

  if (bt)

  {

    cout << bt->data;

    BinaryTree<T>::preOderTraversal(bt->getLeftNode());

    BinaryTree<T>::preOderTraversal(bt->getRightNode());

  }

}

 

template <typename T>

void BinaryTree<T>::inOrderTraversal(BinaryTreeNode<T> *bt) {

  if (bt)

  {

    BinaryTree<T>::inOrderTraversal(bt->getLeftNode());

    cout << bt->data;

    BinaryTree<T>::inOrderTraversal(bt->getRightNode());

  }

}

 

template <typename T>

void BinaryTree<T>::postOrderTraversal(BinaryTreeNode<T> *bt) {

  if (bt)

  {

    BinaryTree<T>::postOrderTraversal(bt->getLeftNode());

    BinaryTree<T>::postOrderTraversal(bt->getRightNode());

    cout << bt->data;

  }

}

 

template <typename T>

void BinaryTree<T>::levelTraversal(BinaryTreeNode<T> *bt) {

 

  queue<BinaryTreeNode<T>*> que;

  que.push(bt);

  while (!que.empty())

  {

    BinaryTreeNode<T>* proot = que.front();

    que.pop();

    cout << proot->data;

 

    if (proot->lChild != NULL)

    {

      Que. push (proot - > lChild); // Left child joins the team

    }

    if (proot->rChild != NULL)

    {

      Que. push (proot - > rChild); // Right child enrolled

    }

  }

}

View Code

Third, Main Program Running

#include "pch.h"

#include <iostream>

#include "BinaryTree.h"

 

int main()

{

  // Scenario Test 2

  BinaryTree<int> btree;

  Btree. PreCreateTree (); // Preorder to build a binary tree

  Court < < "Preorder traversal:";

  Btree. preOderTraversal (btree. root); cout << endl; // precedent traversal  

  Cout < < "intermediate traversal:";

  Btree.inOrderTraversal (btree.root); cout <<endl;//intermediate traversal

  Cout < < "post-order traversal:";

  Btree. postOrderTraversal (btree. root); cout << endl; // postorder traversal

  Cout < < "traversing in sequence:";

  btree.levelTraversal(btree.root);

 

}

View Code

Final test run screenshot

Recommended Today

Deeply analyze the principle and practice of RSA key

1、 Preface After experiencing many dark moments in life, when you read this article, you will regret and even be angry: why didn’t you write this article earlier?! Your darkest moments include: 1. Your project needs to be connected with the bank, and the other party needs you to provide an encryption certificate. You have […]