Data Structure Exercise (Binary Tree)

Title requirements:
Create a binary tree as shown in the figure below, and complete the following functions:
(1) Define a create() function to create this binary tree
(2) Use three methods of pre-order traversal, in-order traversal, and post-order traversal. This binary tree outputs nodes in sequence;
(3) Design function nodecount() to calculate the number of nodes in this binary tree.
Each of the above steps is made into a function, and the relevant code is given.
insert image description here
Reference Code:

#include <stdio.h>
#include <string.h>
#define ElemType char
//构造结点的结构体
typedef struct BTNode{
    
    
    ElemType data;//数据域
    struct BTNode *lchild,*rchild;//左右孩子指针
}BTNode;
//初始化树的函数
BTNode* Create(){
    
    
	BTNode *p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
	
	p1=(BTNode*)malloc(sizeof(BTNode));
	p1->data='A';
	p1->lchild=NULL;
	p1->rchild=NULL;
	
	p2=(BTNode*)malloc(sizeof(BTNode));
	p2->data='B';
	p2->lchild=NULL;
	p2->rchild=NULL;
	
	p3=(BTNode*)malloc(sizeof(BTNode));
	p3->data='C';
	p3->lchild=NULL;
	p3->rchild=NULL;
	
	p4=(BTNode*)malloc(sizeof(BTNode));
	p4->data='D';
	p4->lchild=NULL;
	p4->rchild=NULL;
	
	p5=(BTNode*)malloc(sizeof(BTNode));
	p5->data='E';
	p5->lchild=NULL;
	p5->rchild=NULL;
	
	p6=(BTNode*)malloc(sizeof(BTNode));
	p6->data='F';
	p6->lchild=NULL;
	p6->rchild=NULL;
	
	p7=(BTNode*)malloc(sizeof(BTNode));
	p7->data='G';
	p7->lchild=NULL;
	p7->rchild=NULL;
	
	p8=(BTNode*)malloc(sizeof(BTNode));
	p8->data='H';
	p8->lchild=NULL;
	p8->rchild=NULL;
	
	p9=(BTNode*)malloc(sizeof(BTNode));
	p9->data='I';
	p9->lchild=NULL;
	p9->rchild=NULL;
	
	//连接
	p1->lchild=p2;
	p2->lchild=p4;
	p2->rchild=p5;
	p5->lchild=p7;
	p7->rchild=p9;
	
	p1->rchild=p3;
	p3->lchild=p6;
	p6->rchild=p8;
	
	 return p1;
}

//先序遍历 
void  PreorderTraverse(BTNode *T){
    
    
	if(T!=NULL){
    
    
		printf("%c,",T->data);
		PreorderTraverse(T->lchild);
		PreorderTraverse(T->rchild);
	}
}
//中序遍历
void  InorderTraverse(BTNode *T){
    
    
	if(T!=NULL){
    
    
		InorderTraverse(T->lchild);
		printf("%c,",T->data);
		InorderTraverse(T->rchild);
	}
}
//后续遍历
void  PostorderTraverse(BTNode *T){
    
    
	if(T!=NULL){
    
    
		PostorderTraverse(T->lchild);
		PostorderTraverse(T->rchild);
		printf("%c,",T->data);
    }
}

//计算二叉树结点个数
int count=0;
void nodecount(BTNode *T){
    
    
	if(T!=NULL){
    
    
		count++;
        nodecount(T->lchild);
        nodecount(T->rchild);
    }
}

int main() {
    
    
	BTNode* BinTree;
    
    //初始化二叉树
	BinTree=Create();
	
	printf("先序遍历:");
	PreorderTraverse(BinTree);
    printf("\n中序遍历:");
	InorderTraverse(BinTree);
    printf("\n后序遍历:");
    PostorderTraverse(BinTree);
    
    nodecount(BinTree);
    printf("\n二叉树的结点个数为:%d",count);
    printf("\n");
    system("pause");
    return 0;
    
}

Running results:
1. Preorder traversal:
insert image description here

2. In-order traversal:
insert image description here

3. Post-order traversal:
insert image description here

4. Count the number of nodes:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_46220576/article/details/123094299