Layered traversal of Binary Trees

Source: Internet
Author: User

Problem:

How can we achieve layered traversal of Binary Trees?

Resolution:

We can use the queue to solve this problem.

<1> press the root node into the queue.

<2> check whether the queue is empty.

<3> if this parameter is not set, the frontend element of the queue is obtained and printed.

<4> remove this element from the queue

<5> If the element has a left child, the left child enters the queue.

<6> If the element has a right child, the Right child enters the queue.

The main functions are as follows:

template<typename Type> void BSTrees<Type>::traverseLevel(Node* root){if( root == NULL){cout<<"This is a empty tree"<<endl;return;}queue<Node*> que;que.push(root);while( !que.empty() ){Node* ptr = que.front();que.pop();cout<<ptr->e<<endl;if(ptr->leftChild != NULL)que.push(ptr->leftChild);if(ptr->rightChild != NULL)que.push(ptr->rightChild);}}


All programs are as follows:

//BSTrees.h#ifndef DDXX_BSTREES_H#define DDXX_BSTREES_H#include <iostream>#include <queue>using namespace std;template<typename Type>class BSTrees{public:BSTrees();~BSTrees();public:struct Node{Typee;Node*leftChild;Node*rightChild;Node(){}Node(Type _e){e= _e;leftChild= NULL;rightChild= NULL;}};public:boolinsert(Type e);boolerase1(Type e);boolerase2(Type e);voidclear();boolisEmpty();intgetLength();Node*find(Type e);Node*getRoot();Node*getParent(Node* p);voidtraverse(Node* ptr);voidtraverseLevel(Node* ptr);private:voidclears(Node* &p);private:Node*m_root;intm_Length;};template<typename Type> BSTrees<Type>::BSTrees(){m_root = NULL;m_Length = 0;}template<typename Type> bool BSTrees<Type>::insert(Type e){Node* ptr = new Node(e);if ( ptr == NULL ){cout<<"Allocate memory for the element failed"<<endl;return false;}if( m_root == NULL){m_root = ptr;m_Length++;return true;}Node* p= m_root;Node* pParent= m_root;while( p != NULL){if( e == p->e){cout<<"the element is already exist"<<endl;delete ptr;ptr = NULL;return false;}pParent = p;if( e > p->e){p = p->rightChild;}else{p = p->leftChild;}}if( e < pParent->e ){pParent->leftChild = ptr;}if( e > pParent->e){pParent->rightChild = ptr;}m_Length++;return true;}template<typename Type> bool BSTrees<Type>::erase1(Type e){Node *p = find(e);if ( p == NULL ){cout<<"There's no this element to delete"<<endl;return false;}if ( p->leftChild == NULL){Node* pParent = getParent(p);if( pParent->leftChild == p )pParent->leftChild = p->rightChild;elsepParent->rightChild = p->rightChild;delete p;p = NULL;m_Length--;return true;}if ( p->rightChild == NULL){Node* pParent = getParent(p);if( pParent->leftChild == p)pParent->leftChild = p->leftChild;elsepParent->rightChild = p->leftChild;delete p;p = NULL;m_Length--;return true;}if ( p->leftChild != NULL && p->rightChild != NULL){Node* pParent = getParent(p);Node* pPre = p->leftChild;Node* ptmp = pPre;while( pPre->rightChild != NULL ){ptmp = pPre;pPre = pPre->rightChild;}if( ptmp != pPre){ptmp->rightChild = pPre->leftChild;pPre->leftChild = p->leftChild;pPre->rightChild = p->rightChild;}elsepPre->rightChild = p->rightChild;if( pParent == NULL )m_root = pPre;else if( pParent->leftChild == p)  pParent->leftChild = pPre;elsepParent->rightChild = pPre;delete p;p = NULL;m_Length--;return true;}}template<typename Type> boolBSTrees<Type>::erase2(Type e){Node *p = find(e);if ( p == NULL ){cout<<"There's no this element to delete"<<endl;return false;}if ( p->leftChild == NULL){Node* pParent = getParent(p);if( pParent->leftChild == p )pParent->leftChild = p->rightChild;elsepParent->rightChild = p->rightChild;delete p;p = NULL;m_Length--;return true;}if ( p->rightChild == NULL){Node* pParent = getParent(p);if( pParent->leftChild == p)pParent->leftChild = p->leftChild;elsepParent->rightChild = p->leftChild;delete p;p = NULL;m_Length--;return true;}if( p->leftChild != NULL && p->rightChild != NULL){Node* pParent = getParent(p);Node* ptr = p->leftChild;while( ptr->rightChild != NULL )ptr = ptr->rightChild;ptr->rightChild = p->rightChild;if( pParent == NULL )m_root = p->leftChild;elseif(pParent->leftChild == p)pParent->leftChild = p->leftChild;elsepParent->rightChild = p->leftChild;delete p;p = NULL;m_Length--;return true;}}template<typename Type> voidBSTrees<Type>::clear(){if( m_root == NULL )return;clears(m_root);m_root = NULL;}template<typename Type> void BSTrees<Type>::clears(Node* &p){if(p->leftChild != NULL){clears(p->leftChild);p->leftChild = NULL;}if(p->rightChild != NULL){clears(p->rightChild);p->rightChild = NULL;}delete p;p = NULL;m_Length--;}template<typename Type> typename BSTrees<Type>::Node* BSTrees<Type>::getRoot(){return m_root;}template<typename Type> intBSTrees<Type>::getLength(){return m_Length;}template<typename Type> typename BSTrees<Type>::Node* BSTrees<Type>::getParent(Node* p){if( p == m_root)return NULL;Node* ptr = m_root;Node* ptf = ptr;while( ptr != NULL ){if ( ptr->e == p->e )return ptf;if ( ptr->e > p->e ){ptf = ptr;ptr = ptr->leftChild;}else{ptf = ptr;ptr = ptr->rightChild;}}}template<typename Type> typename BSTrees<Type>::Node* BSTrees<Type>::find(Type e){Node* ptr = m_root;while(ptr != NULL){if ( ptr->e == e )return ptr;if ( ptr->e > e )ptr = ptr->leftChild;elseptr = ptr->rightChild;}//if ( ptr == NULL )return NULL;}template<typename Type> void BSTrees<Type>::traverse(Node* ptr){if( ptr == NULL )return;if( ptr->leftChild != NULL )traverse(ptr->leftChild);cout<<"Element value:"<<ptr->e<<endl;if( ptr->rightChild != NULL )traverse(ptr->rightChild);}template<typename Type> void BSTrees<Type>::traverseLevel(Node* root){if( root == NULL){cout<<"This is a empty tree"<<endl;return;}queue<Node*> que;que.push(root);while( !que.empty() ){Node* ptr = que.front();que.pop();cout<<ptr->e<<endl;if(ptr->leftChild != NULL)que.push(ptr->leftChild);if(ptr->rightChild != NULL)que.push(ptr->rightChild);}}template<typename Type> BSTrees<Type>::~BSTrees(){clear();}#endif

#include <iostream>#include "BST.h"using namespace std;void main(){BSTrees<int>Bst;      int Arr[9] = {6,2,8,4,10,0,12,16,14};      for (int i=0;i<9;i++)              Bst.insert(Arr[i]);      Bst.traverse(Bst.getRoot());      cout<<"Tree'slength is:"<<Bst.getLength()<<endl; Bst.traverseLevel(Bst.getRoot());}





Layered traversal of Binary Trees

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.