数据结构二叉树这快学的云里雾里,所以就写歌c++树的类来将这写东西全部封装起来,想用那个直接调用方法,我决免费将这个大类提供给大家,提供学习上的参考,少走弯路,由于代码比较多,我就将各方法的功能做了注释,如果那有什么不懂的,可以交流。线面就是源代码了~~~
tree.h
#ifndef _TREE_H
#define _TREE_H
class tree{
public:
tree(){
}
~tree(){}
tree*input();
void firstNdigui();
void midNdigui();
void lastNdigui();
void getThTree();
tree* getMcur(tree*root);
void FthreadTree(tree*root);
void Mprint(tree*root);
void MthreadTree(tree*root);
tree*getNext(tree*p);
void Fprint(tree*root);
private:
int data ;
tree* root;
tree*l ;
int tag ;
int tag_r;
tree * pre ;
tree*r ;
};
#endif
trees.h
#ifndef _TREES_H
#define _TREES_H
#include"tree.h"
#include<stack>
#include<stdio.h>
#include<iostream>
using namespace std ;
//创建扩展先序二叉树
tree*tree::input(){
tree *t;
int a ;
cin>>a ;
if(a == -1){
return NULL ;
}
else{
t= new tree() ;
t->data =a ;
t->tag = 0 ;
t->tag_r = 0 ;
t->l = input();
t->r = input();
}
return t ;
}
//先序非递归遍历二叉树
void tree::firstNdigui(){
root= input();
stack<tree*>s ;
tree* q ;
q = root ;
while(q!=NULL||!s.empty()){
if(q != NULL){
cout<<q->data<<endl ;
s.push(q);
q= q->l ;
}
else{
q=s.top();
q=q->r ;
s.pop();
}
}
}
//中序非递归遍历二叉树
void tree::midNdigui(){
root = input();
stack<tree*>s ;
tree * q ;
q = root ;
while(q!=NULL||!s.empty()){
if(q!= NULL) {
s.push(q);
q= q->l;
}
else{
q= s.top();
cout<<q->data<<endl ;
q = q->r ;
s.pop();
}
}
}
//后续非递归
void tree::lastNdigui(){
root= input();
stack<tree*>s;
tree* q= root ;
while(q!= NULL ||!s.empty()){
while(q!= NULL){
s.push(q);
q= q->l ;
}
if(!s.empty()){
q = s.top();
if(q->tag==0){
q->tag = 1 ;
q= q->r;
}
else{
cout<<q->data<<endl;
s.pop();
q = NULL;
}
}
}
}
//获取线索二叉树并打印
void tree::getThTree(){
pre = NULL ;
pre= new tree();
tree * root = input();
//解注释用中序线索化二叉树
//MthreadTree(root);
//Mprint(root);
//先序线索化二叉树
FthreadTree(root);
Fprint(root);
}
//先序线索化二叉树
void tree::FthreadTree(tree * root){
if(root){
if(root->l == NULL){
root->l = pre ;
root->tag = 1;
}
if(pre != NULL&& pre->r == NULL){
pre->r = root ;
pre->tag_r = 1 ;
}
pre= root ;
if(root->tag == 0)
FthreadTree(root->l);
if(root->tag_r == 0){
FthreadTree(root->r);
}
}
}
//先序线索二叉树遍历
void tree::Fprint(tree *root){
tree* p = root;
while(p){
cout<<p->data<<endl ;
if(p->tag== 0){
p = p->l;
}
else{
p = p->r ;
}
}
}
//中序线索化
void tree::MthreadTree(tree*root){
if(root){
MthreadTree(root->l);
if(root->l == NULL) {
root->l = pre ;
root->tag = 1 ;
}
if(pre!= NULL&&pre->r == NULL ){
pre->r = root ;
pre->tag_r = 1 ;
}
pre = root ;
MthreadTree(root->r);
}
}
//打印中序线索化二叉树
void tree::Mprint(tree *root){
tree * p ;
p = getMcur(root);
while(p){
cout<<p->data<<endl;
p = getNext(p);
if(p->r== NULL){
cout<<p->data<<endl;
break;
}
}
}
//获取中序线索二叉树下一个节点
tree*tree::getNext(tree *p){
if(p->tag_r == 1){
p = p->r ;
}
else
for(p = p->r ;p->tag !=1 ; p= p->l);
return p ;
}
//获取中序线索二叉树当前节点
tree* tree::getMcur(tree * root){
tree* q = root;
if(q ==NULL){
return NULL ;
}
while(q->tag != 1)q= q->l ;
return q ;
}
#endif
tree.cpp
#include<iostream>
#include"trees.h"
#include"tree.h"
#include<list>
using namespace std;
int main(){
tree tt ;
cout<<"先序非递归遍历二叉树:"<<endl ;
tt.firstNdigui();
cout<<"后续非递归遍历二叉树:"<<endl ;
//使用后序非递归进行遍历二叉树
tt.lastNdigui();
cout<<"中序非递归遍历二叉树:"<<endl ;
tt.midNdigui();
cout<<"线索化二叉树:(先序和中序)"<<endl;
tt.getThTree();
}
运行截图:
线索化二叉树这块展示的是先序线索化结果,中序可以解注释,在运行。树这块就是暂时用的少,长时间不接触,多看代码。否则真会忘~~~