在vs2011版本中除錯通過。
#include "stdafx.h"
#include"stack.h"
//#include//標準庫中定義的棧
#includeusing namespace std;
#define max_len 15
void create_tree(treenode **head, char *pdata)
sop[j] = top;
k = 1;
break;
case ',':
k = 2;
break;
case ')':
k = 0;
j--;
if(j < 0)
break;
case ' ':
break;
default:
top = (treenode *)malloc(sizeof(treenode));
if(top == null)
top->left = null;
top->right = null;
top->data = pdata[i];
if(null == *head)
if(k == 1)
if(k == 2)
}i++;
}}void show_tree(treenode *head)
else
printf(")");}}
}void pre_order(treenode *root)
else
}}void mid_order(treenode *root)
if(!stackempty(s))
}}void post_order(treenode *root)
current = gettop(s);
if(current->right == null || current->right->data == prenode->data)
prenode = current;
current = pop(s);
current = null;
} else
} }int _tmain(int argc, _tchar* argv)
標頭檔案:stack.h的定義
#pragma once
#include "stdafx.h"
#include#define ok 1
#define true 1
#define error 0
#define false 0
#define overflow -2
#define stack_intt_size 100
#define stack_init_increment 20
#define status int
#define elemtype treenode
typedef struct treenode
treenode;
typedef struct
sqstack;
/* 棧的操作
status initstatck(sqstack &s) 初始化棧
status destorystatck(sqstack &s) 銷毀棧
status clearstack(sqstack &s) 清除棧
bool stackempty(sqstack s) 棧是否為空
int stacklength(sqstack s) 棧的長度
status gettop(sqstack s,selemtype &e) 得到棧頂
status push(sqstack &s,selemtype e) 壓棧
status pop(sqstack &s,selemtype &e) 出棧
void displaystack(sqstack s); 顯示棧內的元素
*/status initstatck(sqstack &s)
status destorystatck(sqstack &s)
bool stackempty(sqstack s)
int stacklength(sqstack s)
elemtype *gettop(sqstack s)
else }
status push(sqstack &s,elemtype e)
else }
elemtype *pop(sqstack &s)
else
return (--s.top);
}status clearstack(sqstack &s)
void displaystack(sqstack s)
樹的遍歷遞迴非遞迴
1先序 遞迴 class solution public void b list list,treenode tree 非遞迴 class solution else return list 2中序 遞迴 class solution public void b list list,treenode...
樹的非遞迴遍歷
1.中根遍歷 思路 一直遍歷左子樹 p p left 直到p為空。此時訪問棧頂元素,棧頂元素出棧。開始遍歷右子樹p p right 遍歷右子樹的左子樹 出棧時訪問 1.definition for a binary tree node.2.struct treenode 7.class soluti...
樹的非遞迴遍歷
include using namespace std 1 2 3 4 5 6 7 8 void pre order treenode root cout root left pre order root right void in order treenode root in order root...