二叉树的建立,前中后序遍历的递归版本和非递归版本,层序遍历
2017-10-08 15:07
525 查看
首先声明,此文章的代码都是整理的大神的代码。感觉非常好,写出来当作笔记。
先来看看二叉树的基本操作:
接下来给出非递归的版本,课教科书上的实现不太一样,我觉得教科书上面的实现经典但是理解起来不太容易,这种方式模拟系统栈更加方便的前中后序的变换。
前序遍历:
中序遍历:
后序遍历:
先来看看二叉树的基本操作:
#include <iostream> #include <queue> using namespace std; template <typename Key, typename Value> class BST{ private: struct Node{ Key key; Value value; Node *left; Node *right; Node(Key key, Value value){ this->key = key; this->value = value; this->left = this->right = NULL; } }; Node *root; int count; public: BST(){ root = NULL; count = 0; } ~BST(){ destroy( root ); } int size(){ return count; } bool isEmpty(){ return count == 0; } void insert(Key key, Value value){ root = insert(root, key, value); } bool contain(Key key){ return contain(root, key); } Value* search(Key key){ return search( root , key ); } // 前序遍历 void preOrder(){ preOrder(root); } // 中序遍历 void inOrder(){ inOrder(root); } // 后序遍历 void postOrder(){ postOrder(root); } // 层序遍历 void levelOrder(){ queue<Node*> q; q.push(root); while( !q.empty() ){ Node *node = q.front(); q.pop(); cout<<node->key<<endl; if( node->left ) q.push( node->left ); if( node->right ) q.push( node->right ); } } private: // 向以node为根的二叉搜索树中,插入节点(key, value) // 返回插入新节点后的二叉搜索树的根 Node* insert(Node *node, Key key, Value value){ if( node == NULL ){ count ++; return new Node(key, value); } if( key == node->key ) node->value = value; else if( key < node->key ) node->left = insert( node->left , key, value); else // key > node->key node->right = insert( node->right, key, value); return node; } // 查看以node为根的二叉搜索树中是否包含键值为key的节点 bool contain(Node* node, Key key){ if( node == NULL ) return false; if( key == node->key ) return true; else if( key < node->key ) return contain( node->left , key ); else // key > node->key return contain( node->right , key ); } // 在以node为根的二叉搜索树中查找key所对应的value Value* search(Node* node, Key key){ if( node == NULL ) return NULL; if( key == node->key ) return &(node->value); else if( key < node->key ) return search( node->left , key ); else // key > node->key return search( node->right, key ); } // 对以node为根的二叉搜索树进行前序遍历 void preOrder(Node* node){ if( node != NULL ){ cout<<node->key<<endl; preOrder(node->left); preOrder(node->right); } } // 对以node为根的二叉搜索树进行中序遍历 void inOrder(Node* node){ if( node != NULL ){ inOrder(node->left); cout<<node->key<<endl; inOrder(node->right); } } // 对以node为根的二叉搜索树进行后序遍历 void postOrder(Node* node){ if( node != NULL ){ postOrder(node->left); postOrder(node->right); cout<<node->key<<endl; } } void destroy(Node* node){ if( node != NULL ){ destroy( node->left ); destroy( node->right ); delete node; count --; } } }; int main() { srand(time(NULL)); BST<int,int> bst = BST<int,int>(); int n = 10; for( int i = 0 ; i < n ; i ++ ){ int key = rand()%n; // 为了后续测试方便,这里value值取和key值一样 int value = key; cout<<key<<" "; bst.insert(key,value); } cout<<endl; // test size cout<<"size: "<<bst.size()<<endl<<endl; // test preOrder cout<<"preOrder: "<<endl; bst.preOrder(); cout<<endl<<endl; // test inOrder cout<<"inOrder: "<<endl; bst.inOrder(); cout<<endl<<endl; // test postOrder cout<<"postOrder: "<<endl; bst.postOrder(); cout<<endl<<endl; // test levelOrder cout<<"levelOrder: "<<endl; bst.levelOrder(); cout<<endl<<endl; return 0; }
接下来给出非递归的版本,课教科书上的实现不太一样,我觉得教科书上面的实现经典但是理解起来不太容易,这种方式模拟系统栈更加方便的前中后序的变换。
前序遍历:
#include <iostream> #include <vector> #include <stack> #include <cassert> using namespace std; /// 144. Binary Tree Preorder Traversal /// Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; struct Command{ string s; // go, print TreeNode* node; Command(string s, TreeNode* node): s(s), node(node){} }; class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if( root == NULL ) return res; stack<Command> stack; stack.push( Command("go", root) ); while( !stack.empty() ){ Command command = stack.top(); stack.pop(); if( command.s == "print" ) res.push_back( command.node->val ); else{ assert( command.s == "go" ); if( command.node->right) stack.push( Command("go",command.node->right)); if( command.node->left) stack.push( Command("go",command.node->left)); stack.push( Command("print", command.node)); } } return res; } }; int main() { return 0; }
中序遍历:
struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; struct Command{ string s; // go, print TreeNode* node; Command(string s, TreeNode* node): s(s), node(node){} }; class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; if( root == NULL ) return res; stack<Command> stack; stack.push( Command("go", root) ); while( !stack.empty() ){ Command command = stack.top(); stack.pop(); if( command.s == "print" ) res.push_back( command.node->val ); else{ assert( command.s == "go" ); if( command.node->right) stack.push( Command("go",command.node->right)); stack.push( Command("print", command.node)); if( command.node->left) stack.push( Command("go",command.node->left)); } } return res; } };
后序遍历:
struct Command{ string s; // go, print TreeNode* node; Command(string s, TreeNode* node): s(s), node(node){} }; class Solution { public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; if( root == NULL ) return res; stack<C 4000 ommand> stack; stack.push( Command("go", root) ); while( !stack.empty() ){ Command command = stack.top(); stack.pop(); if( command.s == "print" ) res.push_back( command.node->val ); else{ assert( command.s == "go" ); stack.push( Command("print", command.node)); if( command.node->right) stack.push( Command("go",command.node->right)); if( command.node->left) stack.push( Command("go",command.node->left)); } } return res; } };
相关文章推荐
- 二叉树前中后序遍历的递归版本和非递归版本、队列实现的层次遍历
- C++二叉树之构造拷贝赋值,递归和非递归的前序遍历,中序遍历和后序遍历,以及层序遍历
- 二叉树的建立&&前中后遍历(递归实现)&&层次遍历
- 数据结构——二叉树的建立和遍历(递归建树&层序遍历建树)
- 二叉树非递归遍历和层序遍历(各种版本)
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】
- 【数据结构】二叉树前中后序遍历、层序遍历(递归实现)
- C语言二叉树建立,遍历(递归与非递归),交换子树
- <二叉树 前中后 层序 非递归遍历 c语言实现>
- 括号法递归建立二叉树并层序遍历输出
- java实现二叉树的前中后遍历(递归和非递归)
- 二叉树的前、中、后序遍历(递归和非递归)、层序遍历、深度、叶子节点个数
- 二叉树的层序遍历(递归和非递归)
- <二叉树 前中后 层序 非递归遍历 c语言实现>
- 学习日志---非递归二叉树游标遍历(前中后层序)
- 二叉树的建立,以及递归前中后序遍历二叉树
- 用java实现二叉树相关操作(前序建树,前中后递归非递归遍历,层序遍历)
- 二叉树的层序遍历(递归与非递归)
- 二叉树的递归遍历以及非递归遍历