您的位置:首页 > 其它

二叉树的建立,前中后序遍历的递归版本和非递归版本,层序遍历

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二叉树 遍历