//廣度優先遍歷
void breadthFirst(Node* &bt){
queue<Node*> que;
Node* BT = bt;
que.push(BT);
while (!que.empty()){
BT = que.front();
que.pop();
printf("%c", BT->data);
if (BT->lchild)
que.push(BT->lchild);
if (BT->rchild)
que.push(BT->rchild);
}
}複製代碼
//從左向右前中後遞歸遍歷
void leftPreOrder(Node *bt) {
if (bt){
printf("%c", bt->data);
leftPreOrder(bt->lchild);
leftPreOrder(bt->rchild);
}
}
void leftInOrder(Node *bt) {
if (bt){
leftInOrder(bt->lchild);
printf("%c", bt->data);
leftInOrder(bt->rchild);
}
}
void leftPostOrder(Node *bt) {
if (bt){
leftPostOrder(bt->lchild);
leftPostOrder(bt->rchild);
printf("%c", bt->data);
}
}複製代碼
節點定義:
typedef struct BiNode{
int data;
struct BiNode * lchild;
struct BiNode * rchild;
}BiNode,*BiTree;
前序遍歷:
void preOrderBiTree(BiNode * root){
if(root == NULL)
return;
BiNode * node = root;
stack<BiNode*> nodes;
while(node || !nodes.empty()){
while(node != NULL){
nodes.push(node);
printf("%d",node->data);
node = node -> lchild;
}
node = nodes.top();//回溯到父節點
nodes.pop();
node = node -> rchild;
}
}複製代碼
void inOrderBinaryTree(BiNode * root){
if(root == NULL)
return;
BiNode * node = root;
stack<BiNode*> nodes;
while(node || !nodes.empty()){
while(node != NULL){
nodes.push(node);
node = node ->lchild;
}
node = nodes.top();
printf("%d ",node ->data);
nodes.pop();
node = node ->rchild;
}
}複製代碼
void PostOrderS(Node *root) {
Node *p = root, *r = NULL;
stack<Node*> s;
while (p!=NULL || !s.empty()) {
if (p!=NULL) {//走到最左邊
s.push(p);
p = p->left;
}
else {
p = s.top();
if (p->right!=NULL && p->right != r)//右子樹存在,未被訪問
p = p->right;
else {
s.pop();
visit(p->val);
r = p;//記錄最近訪問過的節點
p = NULL;//節點訪問完後,重置p指針
}
}//else
}//while
}複製代碼
/*二叉樹節點*/
typedef struct node {
char data;
struct node *lchild, *rchild;
}Node;
/*二叉樹棧*/
typedef struct {
int top;//指向二叉樹節點指針的指針
struct node* data[MAX];
bool isNode[MAX];
}SqStack;
void push(SqStack *&s, Node *bt, bool isNode = true) {
if (s == NULL)
return;
if (s->top == MAX - 1)
{
printf("棧滿,不能再入棧");
}
else
{
s->top++;
s->data[s->top] = bt;
s->isNode[s->top] = isNode;
}
}
Node* pop(SqStack *&s) {
if (s->top == -1)
{
printf("棧空,不能出棧");
}
else
{
Node* temp;
temp = s->data[s->top];
s->top--;
return temp;
}
}
bool topIsNode(SqStack *&s){
if (s->top != -1)
return s->isNode[s->top];
return false;
}
//從左向右前中後非遞歸遍歷
void leftPreOrder(Node *bt) {
Node *BT = bt;
bool isNode = false;
push(s, BT, isNode);
while (!EmptyStack(s)){
isNode = topIsNode(s);
BT = pop(s);
if (isNode){
printf("%c", BT->data);
}
else{
if (BT->rchild != NULL)
push(s, BT->rchild, false);
if (BT->lchild != NULL)
push(s, BT->lchild, false);
push(s, BT, true);
}
}
}
void leftSimPreOrder(Node *bt) {
Node *BT = bt;
push(s, BT);
while (!EmptyStack(s)){
BT = pop(s);
printf("%c", BT->data);
if (BT->rchild != NULL)
push(s, BT->rchild);
if (BT->lchild != NULL)
push(s, BT->lchild);
}
}
void leftInOrder(Node *bt) {
Node *BT = bt;
bool isNode = false;
push(s, BT, isNode);
while (!EmptyStack(s)){
isNode = topIsNode(s);
BT = pop(s);
if (isNode){
printf("%c", BT->data);
}
else{
if (BT->rchild != NULL)
push(s, BT->rchild, false);
push(s, BT, true);
if (BT->lchild != NULL)
push(s, BT->lchild, false);
}
}
}
void leftPostOrder(Node *bt) {
Node *BT = bt;
bool isNode = false;
push(s, BT, isNode);
while (!EmptyStack(s)){
isNode = topIsNode(s);
BT = pop(s);
if (isNode){
printf("%c", BT->data);
}
else{
push(s, BT, true);
if (BT->rchild != NULL)
push(s, BT->rchild, false);
if (BT->lchild != NULL)
push(s, BT->lchild, false);
}
}
}複製代碼
數據結構與算法-二叉查找樹
node