这个帖子讲非递归后序遍历
http://www.cnblogs.com/MichaelYin/archive/2010/12/23/1915316.html
http://blog.csdn.net/ThinkArt/archive/2009/04/18/4089453.aspx
/*
* =====================================================================================
*
* Filename: BinaryTree.h
*
* Description:
*
* Version: 1.0
* Created: 10/06/2010 05:15:42 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Company:
*
* =====================================================================================
*/
#ifndef __BINARYTREE_H__
#define __BINARYTREE_H__
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
#include "Stack.h"
#include "Queue.h"
template <class C> class TNode
{
public:
C data;
TNode<C> *parent;
TNode<C> *lchild;
TNode<C> *rchild;
TNode():parent(NULL),lchild(NULL), rchild(NULL) {}
TNode(C elem, TNode<C> *p):parent(p),lchild(NULL), rchild(NULL)
{
data = elem;
}
};
template <class C> class BinaryTree
{
TNode<C> *root;
void __inorder(TNode<C> *t, int level, string pre)
{
if (!t)
return;
if (t->lchild)
__inorder(t->lchild, level+1, "(l)");
cout << string(level*3, ' ') << pre << t->data << " " << endl;
if (t->rchild)
__inorder(t->rchild, level+1, "(r)");
}
void __preorder(TNode<C> *t, int level, string pre)
{
if (!t)
return;
cout << string(level*3, ' ') << pre << t->data << " " << endl;
if (t->lchild)
__preorder(t->lchild, level+1, "(l)");
if (t->rchild)
__preorder(t->rchild, level+1, "(r)");
}
void __postorder(TNode<C> *t)
{
if (!t)
return;
if (t->lchild)
__postorder(t->lchild);
if (t->rchild)
__postorder(t->rchild);
cout << t->data << " " << endl;
}
TNode<C>* min(TNode<C> *node)
{
while(node->lchild)
{
node = node->lchild;
}
return node;
}
TNode<C>* max(TNode<C> *node)
{
while(node->rchild)
{
node = node->rchild;
}
return node;
}
public:
BinaryTree(): root(NULL){}
BinaryTree(int level)
{
if (level <= 0)
return ;
srand(time(0));
root = CreateSubTree(level);
}
TNode<C> *CreateSubTree(int level)
{
TNode<C> *node;
if (level <= 0)
return NULL;
node = new TNode<C>(rand()%100+1, NULL);
node->rchild = (rand()%2?CreateSubTree(level-1):NULL);;
node->lchild = CreateSubTree(level-1);
return node;
}
void insert(C leaf)
{
TNode<C> *parent = root;
if (!root)
{
root = new TNode<C>(leaf, NULL);
return;
}
while (parent)
{
if (leaf < parent->data)
{
if (parent->lchild)
parent = parent->lchild;
else
{
parent->lchild = new TNode<C>(leaf, parent);
break;
}
}
else
{
if (parent->rchild)
parent = parent->rchild;
else
{
parent->rchild = new TNode<C>(leaf, parent);
break;
}
}
}
}
TNode<C> *dele(TNode<C> *node)
{
TNode<C> *move_out, *move_out_child, *move_out_parent;
if (node == NULL)
return NULL;
/* move_out is the real one will be moved */
if ( (node->lchild == NULL) || (node->rchild == NULL))
move_out = node;
else
move_out = successor(node);
if (move_out->lchild != NULL)
move_out_child = move_out->lchild;
else
move_out_child = move_out->rchild;
/* child's parent points to child's grand-parent */
if (move_out_child)
move_out_child->parent = move_out->parent;
/* now, make "move_out_child" a child of its grand-parent*/
if (!move_out->parent)
root = move_out_child;
else
{
move_out_parent = move_out->parent;
if (move_out == move_out_parent->lchild)
move_out_parent->lchild = move_out_child;
else
move_out_parent->rchild = move_out_child;
}
/* exchange the data if necessary */
if (move_out != node)
{
C tmp = node->data;
node->data = move_out->data;
move_out->data = tmp;
}
return move_out;
}
void inorder()
{
TNode<C> *node = root;
int level = 0;
__inorder(root, level, "");
}
void preorder()
{
TNode<C> *node = root;
int level = 0;
__preorder(root, level, "root ");
}
void postorder()
{
TNode<C> *node = root;
__postorder(root);
}
void iter_inorder()
{
Stack<TNode<C>*> stack;
TNode<C> *node = root;
// while(1)
// {
// for(;node; node = node->lchild)
// stack.push(node);
//
// if (stack.isEmpty())
// break;
// node = stack.pop();
// cout << node->data << endl;
// node = node->rchild;
// }
while(node || !stack.isEmpty())
{
if(node)
{
stack.push(node);
node = node->lchild;
}
else
{
node = stack.pop();
cout << node->data << endl;
node = node->rchild;
}
}
return;
}
void iter_preorder()
{
Stack<TNode<C>*> stack;
TNode<C> *node = root;
// while(1)
// {
// for(;node; node = node->lchild)
// {
// cout << node->data << endl;
// stack.push(node);
// }
// if (stack.isEmpty())
// break;
// node = stack.pop();
// node = node->rchild;
// }
while(node || !stack.isEmpty())
{
if (node)
{
stack.push(node);
cout << node->data << endl;
node = node->lchild;
}
else
{
node = stack.pop();
node = node->rchild;
}
}
return;
}
void iter_postorder()
{
Stack<TNode<C>*> stack;
Stack<TNode<C>*> stack_r;
TNode<C> *node = root;
while(node || !stack.isEmpty())
{
if (node)
{
stack.push(node);
node = node->lchild;
}
else
{
node = stack.pop();
if (!stack_r.isEmpty())
{
node_r = stack_r.top();
if (node_r == )
}
/* there are three cases for this node */
/* leaf */
if(node->rchild == NULL && node->lchild == NULL)
{
cout << node->data << endl;
}
/* no right child */
else if(node->rchild == NULL)
{
cout << node->data << endl;
}
/* has right child */
else
{
stack_r.push(node);
}
node = node->rchild;
}
}
return;
return;
}
void level_order()
{
Queue<TNode<C>*> queue;
TNode<C> *node;
if (!root)
return;
queue.enQueue(root);
while(!queue.isEmpty())
{
node = queue.deQueue();
cout << node->data << endl;
if (node->lchild)
queue.enQueue(node->lchild);
if (node->rchild)
queue.enQueue(node->rchild);
}
return;
}
TNode<C>* iter_search(C key)
{
TNode<C> *node = root;
while (node)
{
if (key == node->data)
return node;
if (key < node->data)
node = node->lchild;
else
node = node->rchild;
}
return node;
}
TNode<C>* min()
{
TNode<C> *node = root;
if (node == NULL)
return NULL;
else
return min(node);
}
TNode<C>* max()
{
TNode<C> *node = root;
if (node == NULL)
return NULL;
else
return max(node);
}
TNode<C>* successor(TNode<C> *node)
{
TNode<C> *y;
if (node == NULL)
return NULL;
/* if node has rchild */
if (node->rchild)
return min(node->rchild);
/* if node hasn't rchild */
y = node->parent;
/* go up the tree until node is the left child of y */
while ((y != NULL) && (node == y->rchild))
{
node = y;
y = node->parent;
}
return y;
}
TNode<C>* predecessor(TNode<C> *node)
{
TNode<C> *y;
if (node == NULL)
return NULL;
/* if node has lchild */
if (node->lchild)
return max(node->lchild);
/* if node hasn't lchild */
y = node->parent;
/* go up the tree untill node is the rchild of y */
while ((y != NULL) && (node == y->lchild))
{
node = y;
y = node->parent;
}
return y;
}
};
#endif //__BINARYTREE_H__
2011.06.08做了个update,现在用preorder遍历树,可以打印出相对来说比较清楚的树型结构。
=======================
#include "BinaryTree.h"
int main()
{
cout << "this is a test" << endl;
BinaryTree<int> bt;
bt.insert(1);
bt.insert(2);
bt.insert(3);
bt.insert(4);
bt.insert(-4);
bt.insert(-1);
//bt.preorder();
BinaryTree<int> bct(2);
bct.preorder();
}