1. 程式人生 > >二叉樹遍歷C++程式碼

二叉樹遍歷C++程式碼

/***********************二叉樹遍歷*********************/
#include <iostream>

using namespace std;

template<class Type>
class BSTree;

template<class Type>
class BinaryNode{
    friend class BSTree<Type>;
public:
    BinaryNode(): left(NULL),right(NULL){}
    BinaryNode(const Type&
value): data(value),left(NULL),right(NULL){} private: Type data; BinaryNode *left; BinaryNode *right; }; template<class Type> class BSTree{ BinaryNode<Type> *root; public: BSTree(): root(NULL){} BinaryNode<Type>* GetRoot() const{return root;} //用來加入一個新的值,並返回加入的值
Type AddValue(const Type& value); //以startNode為根節點列印其子節點資料 void printData_NLR(const BinaryNode<Type> *startNode); //前序遍歷 void printData_LNR(const BinaryNode<Type> *startNode); //中序遍歷 void printData_LRN(const BinaryNode<Type> *startNode); //後續遍歷 void Cout(){ } }; template<
class Type> Type BSTree<Type>::AddValue(const Type& value){ if(root == NULL){ root = new BinaryNode<Type>(value); }else{ BinaryNode<Type> *node = root; while(1){ if(value > node->data){ if(node->right == NULL){ //此時node已經定位在空的節點處了 node->right = new BinaryNode<Type>(value); break; }else{ node = node->right; } }else{ if(node->left == NULL){ node->left = new BinaryNode<Type>(value); break; }else{ node = node->left; } } } } return value; } //前序遍歷 template<class Type> void BSTree<Type>::printData_NLR(const BinaryNode<Type> *startNode){ if(startNode == NULL){ return; }else{ cout << startNode->data << " "; printData_NLR(startNode->left); printData_NLR(startNode->right); } } //中序遍歷 template<class Type> void BSTree<Type>::printData_LNR(const BinaryNode<Type> *startNode){ if(startNode == NULL){ return; }else{ printData_LNR(startNode->left); cout << startNode->data << " "; printData_LNR(startNode->right); } } //後序遍歷 template<class Type> void BSTree<Type>::printData_LRN(const BinaryNode<Type> *startNode){ if(startNode == NULL){ return; }else{ printData_LRN(startNode->left); printData_LRN(startNode->right); cout << startNode->data << " "; } } int main(){ BSTree<int> tree; /********************填充資料*****************************/ tree.AddValue(7); tree.AddValue(4); tree.AddValue(10); tree.AddValue(1); tree.AddValue(5); tree.AddValue(-1); tree.AddValue(2); tree.AddValue(9); tree.AddValue(13); tree.AddValue(12); tree.AddValue(11); tree.AddValue(14); /*********************************************************/ cout << "前序遍歷:根節點 -> 左子樹 -> 右子樹" << endl; tree.printData_NLR(tree.GetRoot()); cout << "\n\n中序遍歷: 左子樹-> 根節點 -> 右子樹" << endl; tree.printData_LNR(tree.GetRoot()); cout << "\n\n後續遍歷: 左子樹 -> 右子樹 -> 根節點" << endl; tree.printData_LRN(tree.GetRoot()); return 0; }