二叉树的创建、层次遍历、递归遍历、非递归遍历

来源:互联网 发布:zbrush mac 百度云 编辑:程序博客网 时间:2024/06/05 15:57

在层次遍历中使用的队列文件如下:

头文件://Queue.h

#pragma once#ifndef NOD_H_#define NOD_H_struct tree_node{int data;tree_node *lchild, *rchild;};#endifstruct queue_node{tree_node * data;queue_node *next;};class Queue{queue_node *front, *rear;public:Queue();~Queue();void en(tree_node * c);tree_node * out();int Empty();};

Queue.cpp

#include "Queue.h"#include <stdlib.h>#include <stdio.h>Queue::Queue(){front=rear = NULL;}Queue::~Queue(){}void Queue::en(tree_node * c){queue_node *temp;temp = (queue_node*)malloc(sizeof(queue_node));temp->data = c;temp->next = NULL;if (rear == NULL){rear = temp;front = rear;}else{//temp->next = rear;//rear = temp;rear->next = temp;rear = temp;}}tree_node * Queue::out(){if (front == NULL)printf("队列为空");else{queue_node *temp;temp = front;tree_node * c = temp->data;front = front->next;if (front == NULL)  rear = NULL;return c;}}int Queue::Empty(){return(front == NULL);}

非递归遍历中使用的堆栈文件如下:

Stack.h

#pragma once#ifndef NOD_H_#define NOD_H_struct tree_node{int data;tree_node *lchild, *rchild;};#endifstruct stack_node{tree_node * data;struct stack_node *next;};class Stack{/*char space[100];int top;*/stack_node *head;public:Stack();~Stack();void push(tree_node * c);tree_node * pop();int isEmpty();};
Stack.cpp

#include "Stack.h"#include <stdlib.h>Stack::Stack(){//top = -1;head = NULL;}Stack::~Stack(){}void Stack::push(tree_node * c){//top++;//space[top] = c;stack_node *temp;temp = (stack_node*)malloc(sizeof(stack_node));temp->data = c;temp->next = head;head = temp;}tree_node * Stack::pop(){//char c = space[top];//top--;//return c;stack_node *temp;temp = head;head = head->next;tree_node * c = temp->data;return c;}int Stack::isEmpty(){return (head == NULL);//return( -1 == top);}

主程序如下:

#include <stdlib.h>#include <stdio.h>#include "Stack.h"#include "Queue.h"tree_node * createTree();  //创建树void level(tree_node * t);  //层次遍历void pre(tree_node* t); //前序递归遍历void mid(tree_node* t);  //中序递归遍历void post(tree_node* t);  //后序递归遍历void PreOrder(tree_node * t); //前序非递归遍历//void ChooseStyle(int i, tree_node *root);  //选择遍历方式int main(){tree_node *root;printf("the root is:");root = createTree();printf("\n");printf("层次遍历: ");    level(root);      printf("\n");printf("前序递归: ");    pre(root);        printf("\n");printf("中序递归: ");    mid(root);        printf("\n");printf("后序递归: ");    post(root);       printf("\n");printf("前序非递归: ");  PreOrder(root);   printf("\n");system("pause");return 0;}//首先创建一个二叉树tree_node * createTree(){struct tree_node* temp = (tree_node*)malloc(sizeof(tree_node*));//创建rootint x;scanf_s("%d", &x);if (0 == x) return NULL; //递归结束语句temp->data = x;printf("%d->left: ", x);temp->lchild = createTree();//创建左子树printf("%d->right: ", x);temp->rchild = createTree();//创建右子树return temp;}void pre(tree_node* t) //递归前序遍历{if (NULL != t){printf("%-4d", t->data);pre(t->lchild);pre(t->rchild);}else return;}void mid(tree_node* t){if (NULL != t){mid(t->lchild);printf("%-4d", t->data);mid(t->rchild);}}void post(tree_node* t){if (NULL != t){post(t->lchild);post(t->rchild);printf("%-4d", t->data);}}void PreOrder(tree_node * t){Stack s; tree_node *p;s.push(t);while (0==s.isEmpty()){p = s.pop();while (p){printf("%-4d", p->data);if (p->rchild)  s.push(p->rchild);p = p->lchild;}}}void level(tree_node * t){Queue q;tree_node *p;p = t;q.en(p);while (q.Empty()==0){p=q.out();  printf("%-4d", p->data);if (p->lchild != NULL) q.en(p->lchild);if (p->rchild != NULL) q.en(p->rchild);}}



0 0
原创粉丝点击