#include
#include
using namespace std;
struct infor
;class test
;test::test()
static int creattrees(struct infor *p, int k)
q->word = word;
q->lchild = null;
q->rchild = null;
q->next = null;
if (1 == k)
if (2 == k)
creattrees(q, 1);
creattrees(q, 2);
}return 0;
}void test::creattree()
if (null == (p = new struct infor))
p->word = word;
p->lchild = null;
p->rchild = null;
m_root = p;
creattrees(p, 1);
creattrees(p, 2);
return;
}static int preorders(struct infor *p)
return 0;
}void test::preorder()
//前序遍歷的非遞迴演算法
static int prepasstoreturns(struct infor *p)
if (-1 != ntop)
}return 0;
}void test::prepasstoreturn()
static int midorders(struct infor *p)
return 0;
}void test::midorder()
//中序遍歷的非遞迴演算法
static int midpasstoreturns(struct infor *p)
if (-1 != ntop)
}return 0;
}void test::midpasstoreturn()
return 0;
}void test::lastorder()
//後續遍歷的非遞迴演算法
static int lastpasstoreturns(struct infor *p)
// // while (-1 != ntop && 2 == q[ntop]->nflags)
//
// // if (-1 != ntop)
//
// // if (-1 != ntop && null != p)
//
// }
return 0;
}void test::lastpasstoreturn()
//層序遍歷演算法
static int levelorders(struct infor *p)
else
if (null != q->rchild)}}
return 0;
}void test::levelorder()
int main()
{freopen("1.txt", "r", stdin);
test t;
t.creattree();
cout << "preorder" << endl;
t.preorder();
cout << "midorder" << endl;
t.midorder();
cout << "levelorder" << endl;
t.levelorder();
cout << "prepasstoreturn" << endl;
t.prepasstoreturn();
cout << "midpasstoreturn" << endl;
t.midpasstoreturn();
cout << "lastorder" << endl;
t.lastorder();
// cout << "lastpasstoreturns" << endl;
// t.lastpasstoreturn();
return 0;
二叉樹 遞迴 非遞迴
include include include include using namespace std typedef struct node bintree typedef struct node1 btnode void creatbintree char s,bintree root 建立二叉...
非遞迴二叉樹
由於棧和遞迴原理相同,且遞迴建立二叉樹的效率較低,所以我們可以借助棧來實現二叉樹的非遞迴建立以及遍歷。include include using namespace std template struct binarytreenode template class binarytree binary...
二叉樹遞迴和非遞迴訪問的實現
二叉樹是一種非常重要的資料結構,很多其它資料結構都是基於二叉樹的基礎演變而來的。對於二叉樹,有前序 中序以及後序三種遍歷方法。因為樹的定義本身就是遞迴定義,因此採用遞迴的方法去實現樹的三種遍歷不僅容易理解而且 很簡潔。而對於樹的遍歷若採用非遞迴的方法,就要採用棧去模擬實現。在三種遍歷中,前序和中序遍...