Algorithm and data structure basics 4: C ++ binary tree implementation and traversal methods, binary tree traversal

Source: Internet
Author: User

Algorithm and data structure basics 4: C ++ binary tree implementation and traversal methods, binary tree traversal

Binary search tree, which is translated into binary search tree, binary search tree, or binary search tree. BST for short.

This article collects the five Traversal Algorithms of Binary Trees: first-order traversal, middle-order traversal, later-order traversal, depth-first traversal, and breadth-first traversal (same-layer traversal, that is, depth-first traversal ).


// BSTree. h

# Include <cstdio> # include <iostream> # include <stack> # include <queue> using namespace std; // binary search tree, it is translated into a binary search tree, a binary search tree, or a binary sorting tree. BSTclass BSTree {struct Node {Node (int x = 0): data (x), lchild (NULL), rchild (NULL) {} struct Node * lchild; struct Node * rchild; int data;}; public: //************************************** * *********************************** // class four functions: constructor, copy constructor, overload assignment operator, destructor //************************ **************************************** * ********* BSTree (); ~ BSTree (); //************************************** * ********************************** // add, delete, modify, and delete query //************************************* ************************************ void Insert (int x ); // return the number of Binary Trees unsigned short Size (); unsigned short Deep (); unsigned short Leaf (); bool IsEmpty (); // traverse void PreorderTraversal (); // first traverse void InorderTraversal (); // In the middle order traverse void PostorderTraversal (); // In the latter order traverse void DepthFirstSear Ch (); // depth first traversal void BreadthFirstSearch (); // breadth first traversal private: // recursive calculation of the number of Binary Trees unsigned short CountSize (Node * n ); unsigned short CountDeep (Node * n); unsigned short CountLeaf (Node * n); // recursively traverse void PreorderTraversal (Node * n); void InorderTraversal (Node * n ); void PostorderTraversal (Node * n); void DepthFirstSearch (Node * n); void BreadthFirstSearch (Node * n); void Free (Node * node); private: Node * m_root ;}; //************** **************************************** * ***************** // Private method //************** **************************************** * ******************* unsigned short BSTree:: CountSize (Node * n) {if (! N) {return 0;} return CountSize (n-> lchild) + CountSize (n-> rchild) + 1;} unsigned short BSTree: CountDeep (Node * n) {if (! N) {return 0;} int ldeep = CountDeep (n-> lchild); int rdeep = CountDeep (n-> rchild); return (ldeep> rdeep )? (Ldeep + 1): (rdeep + 1);} unsigned short BSTree: CountLeaf (Node * n) {if (! N) {return 0;} if (! N-> lchild &&! N-> rchild) {return 1;} return CountLeaf (n-> lchild) + CountLeaf (n-> rchild);} void BSTree: PreorderTraversal (Node * n) {if (n) {cout <n-> data <","; PreorderTraversal (n-> lchild); PreorderTraversal (n-> rchild) ;}} void BSTree:: InorderTraversal (Node * n) {if (n) {InorderTraversal (n-> lchild); cout <n-> data <","; inorderTraversal (n-> rchild) ;}} void BSTree: PostorderTraversal (Node * n) {if (n) {PostorderTraversal (n -> Lchild); PostorderTraversal (n-> rchild); cout <n-> data <"," ;}} void BSTree: DepthFirstSearch (Node * root) {stack <Node *> nodeStack; nodeStack. push (root); Node * node = NULL; while (! NodeStack. empty () {node = nodeStack. top (); cout <node-> data <","; nodeStack. pop (); if (node-> rchild) {nodeStack. push (node-> rchild);} if (node-> lchild) {nodeStack. push (node-> lchild) ;}}void BSTree: BreadthFirstSearch (Node * root) {queue <Node *> nodeQueue; nodeQueue. push (root); Node * node = NULL; while (! NodeQueue. empty () {node = nodeQueue. front (); nodeQueue. pop (); cout <node-> data <","; if (node-> lchild) {nodeQueue. push (node-> lchild);} if (node-> rchild) {nodeQueue. push (node-> rchild) ;}}void BSTree: Free (Node * n) {if (n) {Free (n-> lchild ); free (n-> rchild); delete n; n = NULL ;}} //************************************** * *********************************** // class four functions: constructor, copy constructor, overload assignment operator, destructor //************** **************************************** * ******************* BSTree:: BSTree () {m_root = NULL;} BSTree ::~ BSTree () {Free (m_root );} //************************************** * ********************************** // add, delete, modify, and delete query //************************************* *********************************** void BSTree:: Insert (int x) {Node * tmp = new Node (x); if (! M_root) {m_root = tmp;} else {Node * pre = m_root; Node * cur = m_root; while (cur) {pre = cur; cur = (x <cur-> data )? (Cur-> lchild): (cur-> rchild) ;}( x <pre-> data )? (Pre-> lchild = tmp): (pre-> rchild = tmp) ;}} unsigned short BSTree: Size () {return CountSize (m_root);} unsigned short BSTree:: Deep () {return CountDeep (m_root);} unsigned short BSTree: Leaf () {return CountLeaf (m_root);} bool BSTree: IsEmpty () {return m_root = NULL;} void BSTree: PreorderTraversal () {PreorderTraversal (m_root); cout <endl;} void BSTree: InorderTraversal () {InorderTraversal (m_root ); cout <endl;} void BSTree: PostorderTraversal () {PostorderTraversal (m_root); cout <endl;} void BSTree: DepthFirstSearch () {DepthFirstSearch (m_root ); cout <endl;} void BSTree: BreadthFirstSearch () {BreadthFirstSearch (m_root); cout <endl ;}

// Mian. cpp

// test for BSTree#include "BSTree.h"#include <cstdlib>#include <iostream>using namespace std;int main(){BSTree tree;int arr[6] = {5, 4, 8, 1, 7, 10};for (int i = 0; i < 6; ++i){tree.Insert(arr[i]);}tree.PreorderTraversal();tree.InorderTraversal();tree.PostorderTraversal();tree.DepthFirstSearch();tree.BreadthFirstSearch();cout << "size:" << tree.Size() << endl;cout << "deep:" << tree.Deep() << endl;cout << "leaf:" << tree.Leaf() << endl;system("pause");return 0;}


// Output




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.