C 언어의 두 갈래 트리를 층별로 인쇄하고 깊이를 우선적으로 옮겨다니며 두 갈래 트리가 대칭인지 여부
5141 단어 C 언어
#include
#include
#include
//
typedef struct tree{
int data;
struct tree *pLeft;
struct tree *pRight;
}Tree;
//
typedef struct queue{
struct node *pFront;
struct node *pBehind;
int size;
}Queue;
//
typedef struct stack{
struct node *pBottom;
struct node *pTop;
int size;
}Stack;
//
typedef struct node{
struct tree * data;
struct node * next;
}Node;
//
Tree * create(Tree * pRoot, int val){
if(pRoot == NULL){
pRoot = (Tree *)malloc(sizeof(Tree));
pRoot->data = val;
pRoot->pLeft = NULL;
pRoot->pRight = NULL;
}else{
if(val < pRoot->data){
pRoot->pLeft = create(pRoot->pLeft, val);
}else{
pRoot->pRight = create(pRoot->pRight, val);
}
}
return pRoot;
}
//
Tree * createTree(Tree * pRoot){
for(int i=1; i<=15; i++){
int val = rand()%100+1000;
pRoot = create(pRoot, val);
}
return pRoot;
}
//
void tarverse(Tree * pRoot){
if(pRoot != NULL){
tarverse(pRoot->pLeft);
printf("%d
", pRoot->data);
tarverse(pRoot->pRight);
}
}
//
void pretarverse(Tree * pRoot){
if(pRoot != NULL){
printf("%d
", pRoot->data);
tarverse(pRoot->pLeft);
tarverse(pRoot->pRight);
}
}
//
Queue * init(){
Queue * pQueue = (Queue *)malloc(sizeof(Queue));
if(pQueue == NULL){
printf("init queue failed!
");
exit(0);
}
pQueue->pFront = NULL;
pQueue->pBehind = NULL;
pQueue->size = 0;
return pQueue;
}
//
int empty(Queue * pQueue){
if(pQueue->size == 0){
return 1;
}
return 0;
}
//
void in(Queue * pQueue, Tree * pTree){
Node * pNode = (Node *)malloc(sizeof(Node));
if(pNode == NULL){
printf("queue in data failed
");
exit(0);
}
pNode->data = pTree;
pNode->next = NULL;
if(empty(pQueue)){
pQueue->pFront = pNode;
}else{
pQueue->pBehind->next = pNode;
}
pQueue->pBehind = pNode;
pQueue->size += 1;
}
//
Tree * out(Queue * pQueue){
if(empty(pQueue)){
printf("queue is empty
");
exit(0);
}
Node * pNode = pQueue->pFront;
pQueue->pFront = pNode->next;
pQueue->size -= 1;
Tree * pTree = pNode->data;
delete pNode;
pNode = NULL;
return pTree;
}
Tree * pop(Queue * pQueue){
if(empty(pQueue)){
printf("queue is empty!
");
exit(0);
}
Tree * pNode = pQueue->pBehind->data;
return pNode;
}
void qtarverse(Queue * pQueue){
if(empty(pQueue)){
printf("queue is empty!
");
exit(0);
}
Node * pNode = pQueue->pFront;
while(pNode != NULL){
printf("%d
", pNode->data);
pNode = pNode->next;
}
}
//
void printLevel(Tree * pRoot){
if(pRoot == NULL){
printf("binary tree is empty
");
exit(0);
}
Tree * pTree;
Queue * pQueue = init();
in(pQueue, pRoot);
while(!empty(pQueue)){
pTree = out(pQueue);
printf("%d
", pTree->data);
if(pTree->pLeft != NULL){
in(pQueue, pTree->pLeft);
}
if(pTree->pRight != NULL){
in(pQueue, pTree->pRight);
}
}
}
Stack * stackInit(){
Stack * pStack = (Stack *)malloc(sizeof(Stack));
if(pStack == NULL){
printf("init stack is failed!
");
exit(0);
}
pStack->pBottom = NULL;
pStack->pTop = NULL;
pStack->size = 0;
return pStack;
}
int stackEmpty(Stack * pStack){
if(pStack->size == 0){
return 1;
}
return 0;
}
void stackIn(Stack * pStack, Tree * pTree){
Node * pNode = (Node *)malloc(sizeof(Node));
if(pNode == NULL){
printf("stack in number failed!
");
exit(0);
}
pNode->data = pTree;
if(stackEmpty(pStack)){
pNode->next = NULL;
pStack->pBottom = pNode;
pStack->pTop = pNode;
}else{
pNode->next = pStack->pTop;
pStack->pTop = pNode;
}
pStack->size += 1;
}
Tree * stackOut(Stack * pStack){
if(stackEmpty(pStack)){
printf("stack is empty
");
exit(0);
}
Node * pNode = pStack->pTop;
Tree * pTree = pNode->data;
pStack->pTop = pNode->next;
pStack->size -= 1;
delete pNode;
pNode = NULL;
return pTree;
}
//
void dfs(Tree * pRoot){
if(pRoot == NULL){
printf("binary tree is empty!
");
exit(0);
}
Stack * pStack = stackInit();
stackIn(pStack, pRoot);
while(!stackEmpty(pStack)){
Tree * pTree = stackOut(pStack);
printf("%d
", pTree->data);
if(pTree->pRight != NULL){
stackIn(pStack, pTree->pRight);
}
if(pTree->pLeft != NULL){
stackIn(pStack, pTree->pLeft);
}
}
}
//
int judgeTreeIsSym(Tree * pRoot01, Tree * pRoot02){
if(pRoot01 == NULL && pRoot02 == NULL){
return 1;
}
if(pRoot01 == NULL || pRoot02 == NULL){
return 0;
}
if(judgeTreeIsSym(pRoot01->pLeft, pRoot02->pRight) && judgeTreeIsSym(pRoot01->pRight, pRoot02->pLeft)){
return 1;
}
return 0;
}
int main(){
Tree * pRoot = NULL;
pRoot = createTree(pRoot);
tarverse(pRoot);
printf("
");
printLevel(pRoot);
printf("
");
dfs(pRoot);
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C 언어 구현 천둥 제거 게임 상세 정보먼저 작은 메뉴를 표시하고 게임을 할지 여부를 선택하십시오.사용자가 종료를 선택하면 프로그램 실행이 끝나고, 사용자가 게임을 선택하면 지뢰 제거 위치 좌표를 입력하라는 메시지가 표시됩니다.사용자가 입력한 좌표가 바둑...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.