递归
前序遍历
void preorderTraversal(Binarytree biTree){
if (biTree == nullptr) {
return;
}
cout<<biTree->data<<" ";
preorderTraversal(biTree->left);
preorderTraversal(biTree->right);
}
中序遍历
void inorderTraversal(Binarytree biTree){
if (biTree == nullptr) {
return;
}
inorderTraversal(biTree->left);
cout<<biTree->data<<" ";
inorderTraversal(biTree->right);
}
后序遍历
void postorderTraversal(Binarytree biTree){
if (biTree == nullptr) {
return;
}
postorderTraversal(biTree->left);
postorderTraversal(biTree->right);
cout<<biTree->data<<" ";
}
树的定义
typedef struct BitNode{
int data;
BitNode* left;
BitNode* right;
}BitNode,*Binarytree;
typedef struct BitNodeStackBit{
BitNode* node;
BitNodeStackBit* next;
}
BitNodeStackBit,*BitNodeStack;
void initBitNodeStack(BitNodeStack& bitNodeStack){
bitNodeStack = new BitNodeStackBit;
bitNodeStack->node =nullptr;
bitNodeStack->next =nullptr;
}
void pushBitNode(BitNodeStack& bitNodeStack,BitNode *newNode){
BitNodeStackBit* p = new BitNodeStackBit{newNode,bitNodeStack};
bitNodeStack = p;
}
BitNode* popBitNode(BitNodeStack& bitNodeStack){
if (bitNodeStack == nullptr) {
PerException("the stack is empty");
}
BitNodeStackBit* p = bitNodeStack;
bitNodeStack = bitNodeStack->next;
BitNode* cur = p->node;
delete p;
return cur;
}
BitNode* peepBitNode(BitNodeStack& bitNodeStack){
if (bitNodeStack == nullptr) {
PerException("the stack is empty");
}
BitNodeStackBit* p = bitNodeStack;
BitNode* cur = p->node;
return cur;
}
BitNode* getPopBitNode(BitNodeStack& bitNodeStack){
return popBitNode(bitNodeStack);
}
bool isEmpty(BitNodeStack & bitNodeStack){
if (bitNodeStack->node == nullptr) {
return true;
}
else {
return false;
}
}
非递归
前序遍历
void preorderTraversalN(Binarytree bitTree){
BitNodeStack stack;
initBitNodeStack(stack);
while (bitTree || !isEmpty(stack)) {
while(bitTree){
cout<<bitTree->data<<" ";
pushBitNode(stack, bitTree);
bitTree = bitTree->left;
}
if (!isEmpty(stack)) {
BitNode*p = getPopBitNode(stack);
bitTree = p->right;
}
}
}
中序遍历
void inorderTraversalN(Binarytree biTree){
BitNodeStack stack = new BitNodeStackBit;
initBitNodeStack(stack);
while (biTree|| !isEmpty(stack)) {
while (biTree) {
pushBitNode(stack, biTree);
biTree = biTree->left;
}
if (!isEmpty(stack)) {
BitNode* p = getPopBitNode(stack);
biTree = p->right;
cout<<p->data<<" ";
}
}
}
后序遍历—根右左思想
string change(string str){
string newStr ="";
for (int i =(int)str.length()-1 ; i>=0; i--)
{
newStr += str[i];
}
return newStr;
}
void postorderTraversalNF(Binarytree biTree){
BitNodeStack stack;
initBitNodeStack(stack);
string res ="";
while (biTree || !isEmpty(stack)) {
while (biTree) {
res += to_string(biTree->data);
res += " ";
pushBitNode(stack, biTree);
biTree = biTree->right;
}
if (!isEmpty(stack)) {
BitNode *p = getPopBitNode(stack);
biTree = p->left;
}
}
cout<<change(res).substr(1,res.length());
}
后序遍历—标记思想
void postorderTraversalND(Binarytree biTree){
BitNodeStack stack1;
LinkStack stack2;
initBitNodeStack(stack1);
initStack(stack2);
while (biTree ||!isEmpty(stack1)) {
while (biTree) {
pushBitNode(stack1, biTree);
push(stack2, 0);
biTree = biTree->left;
}
while (!isEmpty(stack1) && getTop(stack2) == 1) {
pop(stack2);
cout<<popBitNode(stack1)->data<<" ";
}
if (!isEmpty(stack1)) {
BitNode* p = peepBitNode(stack1);
biTree = p->right;
pop(stack2);
push(stack2, 1);
}
}
}
栈的定义
typedef struct LSNode{
int data;
LSNode* next;
}LSNode ,*LinkStack;
void initStack(LinkStack &top){
top =nullptr;
}
void clearStack(LinkStack &top){
while (top != nullptr) {
LSNode* p =top;
top = top->next;
delete p;
}
}
void push(LinkStack &top,int data){
LSNode*p = new LSNode{data,top};
top = p;
}
int pop(LinkStack &top){
if (top == nullptr) {
PerException("the stack is empty");
}
int res = 0;
LSNode* p = top;
top = top->next;
res = p->data;
return res;
}
int getTop(LinkStack top){
if (top == nullptr) {
PerException("the stack is empty");
}
return top->data;
}
int length(LinkStack top){
int count =0;
LSNode* p =top;
while (p!=nullptr) {
p = p->next;
count++;
}
return count;
}
bool isEmpty(LinkStack top){
if (top == nullptr) {
return true;
}
return false;
}
bool isFull(LinkStack top){
return false;
}
测试数据
int main(){
BitNode head = {.data = 0};
BitNode head1 = {.data = 1};
BitNode head2 = {.data = 2};
BitNode head3 = {.data = 3};
BitNode head4 = {.data = 4};
BitNode head5 = {.data = 5};
BitNode head6 = {.data = 6};
BitNode head7 = {.data = 7};
head.left = &head1;
head.right = &head2;
head.left->left = &head3;
head.right->left = &head4;
head.right->right = &head5;
head.left->left->right = &head6;
head.left->left->left = &head7;
Binarytree biTree = &head;
printTree(biTree);
preorderTraversal(biTree);
cout<<endl;
preorderTraversalN(biTree);
inorderTraversal(biTree);
cout<<endl;
inorderTraversalN(biTree);
postorderTraversal(biTree);
cout<<endl;
postorderTraversalNF(biTree);
cout<<endl;
postorderTraversalND(biTree);
return 0;
}
宏观打印二叉树
string getSpace(int num) {
string space = " ";
string buf = "";
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf;
}
void printInOrder(Binarytree head, int height, string to, int len) {
if (head == nullptr) {
return;
}
printInOrder(head->right, height + 1, "v", len);
string val = to ;
val += to_string(head->data);
val += to;
int lenM = (int)val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
cout<<getSpace(height * len) + val<<endl;
printInOrder(head->left, height + 1, "^", len);
}
void printTree(Binarytree head) {
cout<<"Binary Tree:";
printInOrder(head, 0, "H", 17);
cout<< endl;
}