여러 화장품 브랜드 가격으로 구성된 바이너리 트리를 설정하고 재귀 및 비 재귀 방법을 사용하여 데이터를 탐색하고 탐색 상황에 따라 인쇄합니다.

실험 명

이진 트리 적용

실험 제목

여러 화장품 브랜드 가격으로 구성된 바이너리 트리를 설정하고 재귀 및 비 재귀 방법을 사용하여 데이터를 탐색하고 탐색 상황에 따라 인쇄합니다. 특정 기능은 다음과 같습니다.
(1) 사전 주문 순회에 따라 이진 트리를 생성하고 각 노드 값은 화장품 브랜드 가격입니다.
(2) 재귀 방법을 사용하여 사전 주문, 중간 주문 및 사후에 이진 트리를 순회합니다.
-주문 ; (3) 비 재귀 방법을 사용하여 이진 트리의 사전 주문, 중간 주문 및 사후 주문을 탐색합니다.

암호

#define _CRT_SECURE_NO_WARNINGS   
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define MAXSIZE 30
typedef int  datatype;
typedef struct node
{
    
    
	datatype data;
	struct node *lchild, *rchild;
}bintnode;
typedef bintnode *bintree;
bintree root;
typedef struct stack
{
    
    
	bintree data[100];
	int tag[100];
	int top;
	char name[20];
}seqstack;
void push(seqstack *s, bintree  t) //进栈
{
    
    
	s->data[s->top] = t; s->top++;
}
bintree pop(seqstack *s)   //出栈
{
    
    
	if (s->top != 0)
	{
    
    
		s->top--;
		return (s->data[s->top]);
	}
	else return NULL;
}
bintree creatbintree()  //创建 
{
    
    
	int ch;
	scanf("%d", &ch);
	bintree t;
	if (ch == -1)
	{
    
    
		t = NULL;
	}
	else
	{
    
    
		t = (bintnode *)malloc(sizeof(bintnode));
		t->data = ch;
		t->lchild = creatbintree();
		t->rchild = creatbintree();
	}
	return t;
}
/*===================递归遍历===================*/
void preorder(bintree t)  //前序遍历递归算法 
{
    
    
	if (t)
	{
    
    
		printf("%d ", t->data);
		preorder(t->lchild);
		preorder(t->rchild);
	}
}
void preorder1(bintree t)
{
    
    

}
void inorder(bintree t)  //中序遍历 
{
    
    
	if (t)
	{
    
    
		inorder(t->lchild);
		printf(" %d ",t->data);
		inorder(t->rchild);
	}
}
void postorder(bintree t)  //后序遍历 
{
    
    
	if (t)
	{
    
    
		postorder(t->lchild);
		postorder(t->rchild);
		printf("%d ", t->data);
	}
}
/*==================非递归遍历===================*/
void preorderl2(bintree t)  //前序
{
    
    
	seqstack s;
	s.top = 0;
	while ((t) || (s.top != 0))
	{
    
    
		if (t)
		{
    
    
			printf("%d ", t->data);
			push(&s, t);
			t = t->lchild;
		}
		else
		{
    
    
			t = pop(&s);
			t = t->rchild;
		}
	}
}
void inorderl2(bintree t) //中序
{
    
    
	seqstack  s;
	s.top = 0;
	while ((t != NULL) || (s.top != 0))
	{
    
    
		if (t)
		{
    
    
			push(&s, t);
			t = t->lchild;
		}
		else
		{
    
    
			t = pop(&s);
			printf("%d ", t->data);
			t = t->rchild;
		}
	}
}
void postoride2(bintree t)
{
    
    
	seqstack s;
	s.top = 0;
	while ((t) || (s.top != 0))
	{
    
    
		if (t)
		{
    
    
			s.data[s.top - 1] = t;
			s.tag[s.top] = 0;
			s.top++;
			t = t->lchild;
		}
		else
		{
    
    
			if (s.tag[s.top - 1] == 1)
			{
    
    
				s.top--;
				t = s.data[s.top];
				printf("%d ", t->data);
				t = NULL;
			}
			else
			{
    
    
				t = s.data[s.top - 1];
				s.tag[s.top - 1] = 1;
				t = t->rchild;
			}
		}
	}
}
int main()
{
    
    
	printf("输入化妆品的品牌价格\n");
	root = creatbintree();
	printf("==========递归实现二叉树的遍历==========\n");
	printf("前序遍历:输出化妆品的品牌价格\n");
	preorder(root);
	printf("\n");
	printf("中序遍历:输出化妆品的品牌价格\n");
	inorder(root);
	printf("\n");
	printf("后序遍历:输出化妆品的品牌价格\n");
	postorder(root);
	printf("\n");
	printf("==========非递归实现二叉树的遍历==========\n");
	printf("前序遍历:输出化妆品的品牌价格\n");
	preorderl2(root);
	printf("\n");
	printf("中序遍历:输出化妆品的品牌价格\n");
	inorderl2(root);
	printf("\n");
	printf("后序遍历:输出化妆品的品牌价格\n");
	postoride2(root);
	return 0;
	//  19  10  3 -1  4  -1  -1  4  5  -1  -1  -1  5  -1  -1
}

스크린 샷 실행 :

여기에 사진 설명 삽입

추천

출처blog.csdn.net/weixin_45629285/article/details/111461627