数据结构与算法设计中的代码实现(1-3)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、基础数据结构

1.1 线性表

1.1.1 顺序表

#include<iostream>
#include<cstdlib>
using namespace std;
#define MAXSIZE 100
typedef struct{
	int data[MAXSIZE];
	int length;
	int capacity;
} SeqList;

SeqList* L;

SeqList * initList(){
	L=(SeqList *)malloc(sizeof(SeqList));
	L->length=0;
	L->capacity=MAXSIZE;
	return L;
}

int GetData(SeqList *L, int i){
	return L->data[i-1];
}

int Locate(SeqList * L,int data){
	if(L->length<=0){
		return -1;
	}
	else{
		for(int i=0;i<L->length;i++){
			if(L->data[i]==data){
				return i;
			}
		}
	}
	return -1;
}

int Delete(SeqList *L,int i){
	if(i>=L->length||i<0){
		return -1;
	}
	else{
		for(int j=i;j<L->length-1;j++){
			L->data[j]=L->data[j+1];
		}
		L->length--;
		return 1;
	}
}


int Insert(SeqList * L,int x) {
	if(L->length>=L->capacity){
		return -1;
	}
	else{
		L->data[L->length]=x;
		L->length++;
		return 1;
	}
}
 
void PrintList(SeqList * L){
	cout<<"List: ";
	for(int i=0;i<L->length;i++){
		cout<<L->data[i]<<" ";
	}
	cout<<endl;
} 
 
int main(){
	int a,b;
	cin>>a>>b;
	L=initList(); 
	Insert(L,a);
	Insert(L,b);
	int i=Locate(L,a);
	cout<<i<<endl;
	Delete(L,i);
	PrintList(L);
} 

1.1.2 单链表

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct Node{
	int data;
	Node * next;
} LinkList;


LinkList* L;

LinkList * initLinkList(){
	L=(LinkList *)malloc(sizeof(LinkList));
	L->next=NULL;
	return L;
}


void HeadInsert(LinkList * L,int data){
	LinkList *p=(LinkList *)malloc(sizeof(LinkList));
	p->next=L->next;
	p->data=data;
	L->next=p;
}



void TailInsert(LinkList *L,int data){
	LinkList *p,*q;
	p=L->next;
	while(p->next!=NULL){
		p=p->next;
	}
	q=(LinkList *)malloc(sizeof(LinkList));
	q->next=NULL;
	q->data=data;
	p->next=q;
}


int GetLength(LinkList *L){
	LinkList *p;
	p=L;
	int count=0;
	while(p->next!=NULL){
		count++;
		p=p->next;
	}
	return count;
}

int Insert(LinkList *L,int i,int data){
	if(i>GetLength(L)){
		return -1;
	}
	else{
		int j=0;
		LinkList *p,*q;
		p=L;
		while(j<i){
			j++;
			q=p;
			p=p->next;
		}
		LinkList * k=(LinkList *)malloc(sizeof(LinkList));
		k->data=data;
		k->next=p;
		q->next=k;
		return 1;
	}
}

int FindLocation(LinkList *L, int data){
	LinkList *p;
	int count=0;
	p=L;
	while(p->next!=NULL){
		p=p->next;
		count++;
		if(p->data==data){
			return count;
		}
	}
	return -1;
}



void Delete(LinkList * L,int i){
	int count=0;
	LinkList *p,*q;
	p=L;
	while(p->next!=NULL){
		q=p;
		p=p->next;
		count++;
		if(count==i){
			break;
		}
	}
	q->next=p->next;
	free(p);
}
 
 
void PrintList(LinkList * L){
	LinkList * p=L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
} 
 
int main(){
	int a,b;
	cin>>a>>b;
	L=initLinkList(); 
	HeadInsert(L,a);
	PrintList(L);
	TailInsert(L,b);
	PrintList(L);
	Delete(L,1);
	PrintList(L);	
} 

1.1.3 双向循环链表

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct Node{
	int data;
	Node * next;
	Node * pre;
} LinkList;


LinkList* L;

LinkList * initLinkList(){
	L=(LinkList *)malloc(sizeof(LinkList));
	L->next=L;
	L->pre=L;
	return L;
}

int GetLength(LinkList *L){
	LinkList *p;
	p=L;
	int count=0;
	if(L->next==L){
		return 0;
	}
	else{
			while(p->next!=L){
	        	p=p->next;
	        	count++;
     	}
		return count;	
	}
}

void HeadInsert(LinkList * L,int data){
	LinkList *p=(LinkList *)malloc(sizeof(LinkList));
	p->next=L->next;
	p->data=data;
	L->next=p;
	p->pre=L;
}



void TrailInsert(LinkList *L,int data){
	LinkList *p,*q;
	p=L;
	while(p->next!=L){
		p=p->next;
	}
	q=(LinkList *)malloc(sizeof(LinkList));
	q->next=L;
	q->data=data;
	p->next=q;
	q->pre=p;
}




int Insert(LinkList *L,int i,int data){
	if(i>GetLength(L)){
		return -1;
	}
	else{
		int j=0;
		LinkList *p,*q;
		p=L;
		while(j<i){
			j++;
			q=p;
			p=p->next;
		}
		LinkList * k=(LinkList *)malloc(sizeof(LinkList));
		k->data=data;
		k->next=p;
		k->pre=q;
		p->pre=k;
		q->next=k;
		return 1;
	}
}

int FindLocation(LinkList *L, int data){
	LinkList *p;
	int count=0;
	p=L;
	while(p->next!=L){
		p=p->next;
		count++;
		if(p->data==data){
			return count;
		}
	}
	return -1;
}



void Delete(LinkList * L,int i){
	int count=0;
	LinkList *p,*q;
	p=L;
	while(p->next!=L){
		q=p;
		p=p->next;
		count++;
		if(count==i){
			break;
		}
	}
	q->next=p->next;
	p->next->pre=q;
	free(p);
}
 
 
void PrintList(LinkList * L){
	LinkList * p=L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
} 
 
int main(){
	int a,b;
	cin>>a>>b;
	L=initLinkList();
	HeadInsert(L,a);
	TrailInsert(L,b);
	int length=GetLength(L);
	cout<<length<<endl;
} 

1.1.4 静态链表

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct Node{
	int data;
	int cursor;
} StaticList;

int * av;
int * start;
StaticList* L;

StaticList * initStaticList(){
	L=(StaticList *)malloc(MAXSIZE*sizeof(StaticList));
	for(int i=0;i<MAXSIZE-1;i++){
	   	L[i]->cursor=i+1;
	}
	L[MAXSIZE-1]->cursor=-1;
	*av=0;
	start=NULL;
	return L;
}

int GetLength(){
	if(start==NULL){
		return 0;
	}
	else{
		int count=1;
		int * p=*start;
		while(L[*p]->cursor!=-1){
			*p=L[*p]->cursor;
			count++;
		}
	}
	return count;
}


int Insert(StaticList *L,int i,int data){
	int count=0;
	if(i>GetLength()){
		return -1;
	}
	int * p=*start;
	while(L[*p]->cursor!=-1&&count!=i){
		*p=L[*p]->cursor;
		count++;
	}
	L[*av]->data=data;
	L[*av]->cursor=L[*p]->cursor;
	L[*p]->cursor=*av;
	int * temp=L[*av]->cursor;
	*av=*temp;	
	return 1;	
}

int Delete(StaticList * L,int i){
	int count=0;
	if(i>GetLength()){
		return -1;
	}
	int * p=*start;
	
	while(L[*p]->cursor!=-1&&count<i-1){
		*p=L[*p]->cursor;
		count++;
	}
	*temp=L[*p]->cursor;
	L[*p]->cursor=L[*temp]->cursor;
	L[*temp]->cursor=*av;
	*av=*temp;
	return 1;	
	
}

1.2 栈

1.2.1 顺序栈

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct node{
    int data[MAXSIZE];
	int top;
	int capacity;	
	
}SeqStack;

SeqStack * S;

SeqStack * initStack(){
	S=(SeqStack *) malloc(sizeof(SeqStack));
	S->top=-1;
	S->capacity=MAXSIZE;
	return S;
}

int GetLength(SeqStack * S){
	return S->top+1;
}

int isFull(SeqStack *S){
	return S->top==S->capacity-1;
}
int isEmpty(SeqStack *S){
	return S->top==-1;
}

int Push(SeqStack *S,int data){
	if(isFull(S)){
		return -1;
	}
	S->top++; 
	S->data[S->top]=data;
	return 1;
}

int Pop(SeqStack * S){
	if(isEmpty(S)){
		return -1;
	}
	int x=S->data[S->top];
	S->top--;
	return x;
}

int GetTop(SeqStack *S){
	if(isEmpty(S)){
		return -1;
	}
	return S->data[S->top];
}

int main(){
	S=initStack();
	Push(S,1);
	cout<<GetTop(S)<<endl;
	Pop(S);
	cout<<isEmpty(S)<<endl;
}

1.2.2 双端栈

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct node{
    int data[MAXSIZE];
	int top1;
	int top2;
	int capacity;	
	
}DStack;

DStack * S;

DStack * initStack(){
	S=(DStack *)malloc(sizeof(DStack));
	S->capacity=MAXSIZE;
	S->top1=-1;
	S->top2=MAXSIZE;
	return S;
}

int isEmpty(DStack * S){
	return S->top1==-1&&S->top2==MAXSIZE;
	
}

int isFull(DStack *S){
	return S->top1==S->top2-1;
}

int Push1(DStack * S,int data){
	if(isFull(S)){
		return -1;
	} 
	S->top1++;
	S->data[S->top1]=data;
	return 1;
}
int Push2(DStack * S,int data){
	if(isFull(S)){
		return -1;
	}
	S->top2--;
	S->data[S->top2]=data;
	return 1;
}


int Pop1(DStack * S){
	if(isEmpty(S)){
		return -1;
	}
	int x=S->data[S->top1];
	S->top1--;
	return x;
}

int Pop2(DStack * S){
	if(isEmpty(S)){
		return -1;
	}
	int x=S->data[S->top2];
	S->top2++;
	return x;
}

int GetTop1(DStack * S){
	if(isEmpty(S)){
		return -1;
	}
	return S->data[S->top1];
}

int GetTop2(DStack * S){
	if(isEmpty(S)){
		return -1;
	}
	return S->data[S->top2];
}

int main(){
	S=initStack();
	Push1(S,1);
	cout<<GetTop1(S)<<endl;
	cout<<isEmpty(S)<<endl;
}

1.2.3 链栈

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct node{
    int data;
    node * next;
}LinkStack;

LinkStack * top;

LinkStack * initLinkStack(){
	top=(LinkStack *)malloc(sizeof(LinkStack));
	top->next=NULL;
	return top;
}


void Push(LinkStack * top,int data){
	LinkStack * p=(LinkStack *)malloc(sizeof(LinkStack));
	p->data=data;
	p->next=top->next;
	top->next=p;
}


int Pop(LinkStack * top){
	int data=top->next->data;
	LinkStack * p=top->next;
	top->next=top->next->next;
	free(p);
	return data;
}


int main(){
	top=initLinkStack();
	Push(top,1);
	cout<<Pop(top)<<endl;
	return 0;
}

1.2.4 多链栈

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct node{
    int data;
    node * next;
}LinkStack;

LinkStack * top[MAXSIZE];

LinkStack * initLinkStack(){
	for(int i=0;i<MAXSIZE;i++){
		top[i]=(LinkStack *)malloc(sizeof(LinkStack));
		top[i]->next=NULL;
	}
    return top[MAXSIZE];
}


void Push(LinkStack * top,int data,int i){
	LinkStack * p=(LinkStack *)malloc(sizeof(LinkStack));
	p->data=data;
	p->next=top[i]->next;
	top[i]->next=p;
}


int Pop(LinkStack * top,int i){
	int data=top[i]->next->data;
	LinkStack * p=top[i];
	top[i]->next=top[i]->next->next;
	free(p);
	return data;
}

1.3 队列

1.3.1 顺序循环队列

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct node{
	int data[MAXSIZE];
	int front;
	int rear; 
}SeqQueue;

SeqQueue * Q;

SeqQueue * initSeqQueue(){
	Q=(SeqQueue *)malloc(sizeof(SeqQueue));
	Q->front=0;
	Q->rear=0; 
}


int isEmpty(SeqQueue * Q){
	return Q->front==Q->rear;
}

int isFull(SeqQueue * Q){
	return (Q->rear+1)%MAXSIZE==Q->front;
}

int EnterQueue(SeqQueue *Q,int data){
	if(isFull(Q)){
		return -1;
	}
	else{
		Q->data[Q->rear]=data;
		Q->rear++;
	}
	return 1;
}

int DeleteQueue(SeqQueue * Q){
	if(isEmpty(Q)){
		return -1;
	}
	else{
	    int data=Q->data[Q->front];
	    Q->front++;
	    return data;
	}
}

int main(){
	Q=initSeqQueue();
	EnterQueue(Q,2);
	EnterQueue(Q,1);
	cout<<DeleteQueue(Q)<<endl;
}

1.3.2 链式队列

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct node{
	int data;
	node * next;
}SeqQueueNode;

typedef struct{
	SeqQueueNode * front;
	SeqQueueNode * rear;
	int length;
}SeqQueue;


SeqQueue * Q;

SeqQueue * initSeqQueue(){
	Q=(SeqQueue *)malloc(sizeof(SeqQueue));
	Q->front=(SeqQueueNode * )malloc(sizeof(SeqQueueNode));
	Q->rear=(SeqQueueNode * )malloc(sizeof(SeqQueueNode));
	Q->front->next=NULL;
	Q->rear->next=NULL; 
	Q->length=0;
}


int isEmpty(SeqQueue * Q){
	return 	Q->length==0;
}


int EnterQueue(SeqQueue *Q,int data){
	SeqQueueNode* p=(SeqQueueNode *)malloc(sizeof(SeqQueueNode));
	p->data=data;
	if(Q->length==0){
		Q->front->next=p;
	}
	Q->rear->next=p;
	Q->length++;
	return 1;
}

int DeleteQueue(SeqQueue * Q){
	if(isEmpty(Q)){
		return -1;
	}
	if(Q->length==1){
		int data=Q->front->next->data;
		SeqQueueNode * p=Q->front->next;
		Q->front->next=Q->rear->next=NULL;
		Q->length--; 
		free(p);
		return data;
	}
	else{
		int data=Q->front->next->data;
		Q->front->next=Q->front->next->next;
		Q->length--; 
		return data;
		 
	}
}

int main(){
	Q=initSeqQueue();
	EnterQueue(Q,2);
	EnterQueue(Q,1);
	cout<<DeleteQueue(Q)<<endl;
}

1.4 树

1.4.1 二叉树

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;

typedef struct Node{
	char data;
	Node * Lchild;
	Node * Rchild;
}BiTree;


BiTree * T;

void PreOrder(BiTree * T){
	if(T!=NULL){
		cout<<T->data;
		PreOrder(T->Lchild);
		PreOrder(T->Rchild); 
	}
}


void PostOrder(BiTree * T){
	if(T!=NULL){
		PostOrder(T->Lchild);
		PostOrder(T->Rchild); 
		cout<<T->data;
	
	}
}


void InOrder(BiTree * T){
	if(T!=NULL){
		InOrder(T->Lchild);
		cout<<T->data;
	    InOrder(T->Rchild); 
	}
}

BiTree * PreCreateTree(){
    char ch;
    scanf("%C",&ch);
    getchar();
    if(ch == '.')
        return NULL;
    else{
        BiTree * tree=(BiTree *)malloc(sizeof(BiTree));
        tree->data=ch;
        tree->Lchild=PreCreateTree();
        tree->Rchild=PreCreateTree();
        return tree;
    }
}


int PostTreeDepth(BiTree * T){
	int hr,hl,max;
	if(T!=NULL){
	
	hl=PostTreeDepth(T->Lchild);
	hr=PostTreeDepth(T->Rchild);
	return hr>hl?hr+1:hl+1;
} 
else{
	return 0;
}
}


int main(){
	T=PreCreateTree();
	int h=PostTreeDepth(T);
	cout<<h;
} 

补充:先序遍历等的非递归方法。
非递归的中序遍历:

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct Node{
	char data;
	Node * Lchild;
	Node * Rchild;
}BiTree;
BiTree * T;
typedef struct node{
    BiTree * data[MAXSIZE];
	int top;
	int capacity;	
	
}SeqStack;

SeqStack * S;

SeqStack * initStack(){
	S=(SeqStack *) malloc(sizeof(SeqStack));
	S->top=-1;
	S->capacity=MAXSIZE;
	return S;
}

int GetLength(SeqStack * S){
	return S->top+1;
}

int isFull(SeqStack *S){
	return S->top==S->capacity-1;
}
int isEmpty(SeqStack *S){
	return S->top==-1;
}

int Push(SeqStack *S,BiTree * data){
	if(isFull(S)){
		return -1;
	}
	S->top++; 
	S->data[S->top]=data;
	return 1;
}
BiTree * Pop(SeqStack * S){
	if(isEmpty(S)){
		return NULL;
	}
	BiTree* x=S->data[S->top];
	S->top--;
	return x;
}
BiTree * GetTop(SeqStack *S){
	if(isEmpty(S)){
		return -NULL;
	}
	return S->data[S->top];
}

BiTree * PreCreateTree(){
    char ch;
    scanf("%C",&ch);
    getchar();
    if(ch == '.')
        return NULL;
    else{
        BiTree * tree=(BiTree *)malloc(sizeof(BiTree));
        tree->data=ch;
        tree->Lchild=PreCreateTree();
        tree->Rchild=PreCreateTree();
        return tree;
    }
}
void Inorder(BiTree * T){
	S=initStack();
	BiTree *p=T;
	while(p!=NULL||!isEmpty(S)){
		if(p!=NULL){
		Push(S,p);
		p=p->Lchild;
	}
	    else{
	    	cout<<1; 
	    	p=Pop(S);
	    	cout<<p->data;
	    	p=p->Rchild;
		}
	}
}
int main(){
	T=PreCreateTree();
	Inorder(T);
} 

非递归的前序遍历:

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;


typedef struct Node{
	char data;
	Node * Lchild;
	Node * Rchild;
}BiTree;


BiTree * T;



typedef struct node{
    BiTree * data[MAXSIZE];
	int top;
	int capacity;	
	
}SeqStack;

SeqStack * S;

SeqStack * initStack(){
	S=(SeqStack *) malloc(sizeof(SeqStack));
	S->top=-1;
	S->capacity=MAXSIZE;
	return S;
}

int GetLength(SeqStack * S){
	return S->top+1;
}

int isFull(SeqStack *S){
	return S->top==S->capacity-1;
}
int isEmpty(SeqStack *S){
	return S->top==-1;
}

int Push(SeqStack *S,BiTree * data){
	if(isFull(S)){
		return -1;
	}
	S->top++; 
	S->data[S->top]=data;
	return 1;
}

BiTree * Pop(SeqStack * S){
	if(isEmpty(S)){
		return NULL;
	}
	BiTree* x=S->data[S->top];
	S->top--;
	return x;
}

BiTree * GetTop(SeqStack *S){
	if(isEmpty(S)){
		return -NULL;
	}
	return S->data[S->top];
}



BiTree * PreCreateTree(){
    char ch;
    scanf("%C",&ch);
    getchar();
    if(ch == '.')
        return NULL;
    else{
        BiTree * tree=(BiTree *)malloc(sizeof(BiTree));
        tree->data=ch;
        tree->Lchild=PreCreateTree();
        tree->Rchild=PreCreateTree();
        return tree;
    }
}


void Preorder(BiTree * T){
	S=initStack();
	BiTree *p=T;
	while(p!=NULL||!isEmpty(S)){
		if(p!=NULL){
		Push(S,p);
		cout<<p->data;
		p=p->Lchild;
	}
	 
	    else{

	    	p=Pop(S);
	    	
	    	p=p->Rchild;
		}
	}
}
int main(){
	T=PreCreateTree();
	Preorder(T);
} 

非递归的后续遍历

#include<iostream>
#include<cstdlib>
#define MAXSIZE 100
using namespace std;
typedef struct Node{
	char data;
	Node * Lchild;
	Node * Rchild;
}BiTree;
BiTree * T;
typedef struct node{
    BiTree * data[MAXSIZE];
	int top;
	int capacity;	
	
}SeqStack;

SeqStack * S;

SeqStack * initStack(){
	S=(SeqStack *) malloc(sizeof(SeqStack));
	S->top=-1;
	S->capacity=MAXSIZE;
	return S;
}

int GetLength(SeqStack * S){
	return S->top+1;
}

int isFull(SeqStack *S){
	return S->top==S->capacity-1;
}
int isEmpty(SeqStack *S){
	return S->top==-1;
}

int Push(SeqStack *S,BiTree * data){
	if(isFull(S)){
		return -1;
	}
	S->top++; 
	S->data[S->top]=data;
	return 1;
}

BiTree * Pop(SeqStack * S){
	if(isEmpty(S)){
		return NULL;
	}
	BiTree* x=S->data[S->top];
	S->top--;
	return x;
}

BiTree * GetTop(SeqStack *S){
	if(isEmpty(S)){
		return -NULL;
	}
	return S->data[S->top];
}

BiTree * PreCreateTree(){
    char ch;
    scanf("%C",&ch);
    getchar();
    if(ch == '.')
        return NULL;
    else{
        BiTree * tree=(BiTree *)malloc(sizeof(BiTree));
        tree->data=ch;
        tree->Lchild=PreCreateTree();
        tree->Rchild=PreCreateTree();
        return tree;
    }
}
void Postorder(BiTree * T){
	S=initStack();
	BiTree *p=T;
	BiTree *q=NULL;
	while(p!=NULL||!isEmpty(S)){
		if(p!=NULL){
		Push(S,p);
		p=p->Lchild;
	}
	    else{
	    	p=GetTop(S);
	    	if(p->Rchild==NULL||p->Rchild==q){
	    		cout<<p->data;
	    		q=p;
	    		p=Pop(S);
	    		p=NULL;
			}
			else{
	    	p=p->Rchild;
		}
	}
	}
}
int main(){
	T=PreCreateTree();
	Postorder(T);
} 

1.4.2 线索二叉树

中序线索二叉树:

#include<iostream>
#include<cstdlib>
using namespace std;

typedef struct Node{
	char data;
	Node * Lchild;
	int Ltag;
	Node * Rchild;
	int Rtag;
}BiTree;


BiTree * T;


BiTree * PreCreateTree(){
    char ch;
    scanf("%C",&ch);
    getchar();
    if(ch == '.')
        return NULL;
    else{
        BiTree * tree=(BiTree *)malloc(sizeof(BiTree));
        tree->data=ch;
        tree->Lchild=PreCreateTree();
        tree->Ltag=0;
        tree->Rchild=PreCreateTree();
        tree->Rtag=0;
        return tree;
    }
}

BiTree * pre;

void Inthread(BiTree *T){
	if(T!=NULL){
	Inthread(T->Lchild);
	if(T->Lchild==NULL){
		T->Ltag=1;
		T->Lchild=pre;
	}
	if(pre!=NULL&&pre->Rchild==NULL){
		pre->Rtag=1;
		pre->Rchild=T;
	}
	pre=T;
	Inthread(T->Rchild);
}
}

BiTree * Orderfirst(BiTree *T){
	BiTree *p=T;
	if(p!=NULL){
		while(p->Ltag==0){
			p=p->Lchild;
		}
	}
	return p;
}

BiTree* InNext(BiTree * p){
	if(p->Rtag==1){
	    return p->Rchild;	
	}
	else{
		BiTree * q=p->Rchild; 
		while(q->Ltag==0){
			q=q->Lchild;
		}
		return q;
	}
	
}

void Order(BiTree *T){
	BiTree *p=Orderfirst(T);
	while(p!=NULL){
		cout<<p->data;
		p=InNext(p);
	}
}


void InsertNodeR(BiTree * p,BiTree * q){
	if(p->Rtag==0){
		BiTree *k=p->Rchild;
		while(k->Ltag==0){
			k=k->Lchild;
		}
		q->Rtag=0;
		q->Rchild=p->Rchild;
		q->Ltag=1;
		q->Lchild=p;
		p->Rchild=r;
		k->Lchild=q;
	}
	else{
		p->Rtag=0;
		q->Rtag=1;
		q->Rchild=p->Rchild;
		p->Rchild=q;
		q->Ltag=1;
		q->Lchild=p;
	}
} 

int main(){
	T=PreCreateTree();
	pre=NULL;
	Inthread(T);
	Order(T); 
} 

类似的先序:

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct Node{
	char data;
	Node * Lchild;
	int Ltag;
	Node * Rchild;
	int Rtag;
}BiTree;
BiTree * T;
BiTree * PreCreateTree(){
    char ch;
    scanf("%C",&ch);
    getchar();
    if(ch == '.')
        return NULL;
    else{
        BiTree * tree=(BiTree *)malloc(sizeof(BiTree));
        tree->data=ch;
        tree->Lchild=PreCreateTree();
        tree->Ltag=0;
        tree->Rchild=PreCreateTree();
        tree->Rtag=0;
        return tree;
    }
}
BiTree * pre;
void Prethread(BiTree *T){
	if(T!=NULL){
	if(T->Lchild==NULL){
		T->Ltag=1;
		T->Lchild=pre;
	}
	if(pre!=NULL&&pre->Rchild==NULL){
		pre->Rtag=1;
		pre->Rchild=T;
	}
	pre=T;
	Prethread(T->Lchild);
	Prethread(T->Rchild);
}
}
int main(){
	T=PreCreateTree();
	pre=NULL;
	Prethread(T);
} 

1.4.3 树

#include<iostream>
#include<cstdlib>
using namespace std;
#include<stack>
typedef struct Node{
	char data;
	Node * firstChild;
	Node * sibling;
}Tree;
Tree * T;
Tree * createTree(char * s){
	int childType=0;
	int top=-1;
	Tree * p,*q;
	Tree * T[100];
	for(int i=0;s[i]!='\0';i++){
		if(isalpha(s[i])){
			p=(Tree *)malloc(sizeof(Tree));
			p->data=s[i];
			p->firstChild=NULL;
			p->sibling=NULL;
			if(childType==1)
			{
				T[top]->firstChild=p;
			}
			if(childType==2){
				q=T[top]->firstChild;
				while(q->sibling!=NULL){
					q=q->sibling;
				}
				q->sibling=p;
			}
		}
		else if(s[i]=='('){
			top++;
			T[top]=p;
			childType=1;
		}
		else if(s[i]==','){
			childType=2;
		}
		else if(s[i]==')'){
			top--;
		}	
	}
	return T[0];
}
void PreOrder(Tree * T){
	if(T!=NULL){
		cout<<T->data;
		PreOrder(T->firstChild);
		PreOrder(T->sibling);
   }
}
void PostOrder(Tree * T){
	if(T!=NULL){
		PreOrder(T->firstChild);
		PreOrder(T->sibling);
		cout<<T->data;
   }
}
int main(){
	
    char str[100]="A(B(D,E,F),C(G))";	
	T=createTree(str);
	PostOrder(T);
}

1.5 图

上一篇:mysql 数据丢失更新的解决方法


下一篇:数据结构与算法