二叉树的实现-C语言

创建 | 销毁 | 遍历 | 树的高度 | 叶子节点数 | 打印叶子节点

目录

二叉树的实现-C语言

在创建二叉树中,用到了双指针(**),用单指针也可以(*);

//创建二叉树
void binarytree_create(Tree* root) {
	int a = 0;
	printf("输入接节点数值 (当输入100,当前节点创建完成)\n");
	scanf("%d", &a);
	if (a == 100) {
		root = NULL;
	}
	else {
		root = (Tnode*)malloc(sizeof(Tnode));
		if (root == NULL) {
			return;
		}
		root->data = a;
		printf("create %d 的左孩子", a);
		binarytree_create(root->lchild);
		printf("create %d 的右孩子", a);
		binarytree_create(root->rchild);
	}
}

...

int main(){
...
binarytree_create(root);
...
}

整体代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include<assert.h>

typedef struct treeNode {
	int data;
	struct treeNode* lchild;
	struct treeNode* rchild;
}Tnode,Tree;

//创建二叉树
void binarytree_create(Tree** root) {
	int a = 0;
	printf("输入接节点数值 (当输入100,当前节点创建完成)\n");     
	scanf_s("%d", &a);
	if (a == 100) {
		*root = NULL;
	}
	else {
		*root = (Tnode*)malloc(sizeof(Tnode));
		if (*root == NULL) {
			return;
		}
		(*root)->data = a;
		printf("create %d 的左孩子", a);
		binarytree_create(&(*root)->lchild);
		printf("create %d 的右孩子", a);
		binarytree_create(&(*root)->rchild);
	}
}

//销毁二叉树
void binarytree_destory(Tree* root) {
	if (root == NULL) {
		return;
	}
	binarytree_destory(root->lchild);
	binarytree_destory(root->rchild);
	free(root);
}

//先序遍历 根->左->右
void binarytree_preorder(Tree* root) {
	if (root == NULL) {
		return;
	}
	printf(" %d ", root->data);
	binarytree_preorder(root->lchild);
	binarytree_preorder(root->rchild);
	return;
}

//中序遍历 左->根->右
void binarytree_inorder(Tree* root) {
	if (root == NULL) {
		return;
	}
	binarytree_inorder(root->lchild);
	printf(" %d ", root->data);
	binarytree_inorder(root->rchild);
	return;
}

//后序遍历 左->右->根
void binarytree_postorder(Tree* root) {
	if (root == NULL) {
		return;
	}
	binarytree_postorder(root->lchild);
	binarytree_postorder(root->rchild);
	printf(" %d ", root->data);
	return;
}

//广度  -- 需要用到队列
/*创建队,入队,出队*/
/*
void binarytree_levelorder(Tree* root){
	list_queue* queue = NULL;
	Tnode* node = NULL;
	if(root == NULL){
	return;
	}
	queue = list_queue_create();
	//根节点先入队
	list_queue_enqueue(queue, root);
	while(!list_queue_is_empty(queue)){
		list_queue_dequeue(queue,&node);
		if(node->lchild != NULL){
			list_queue_enqueue(queue,node->lchild);
		}
		if(node->rchild != NULL){
			list_queue_enqueue(queue,node->rchild);
		}
	}
	free(queue);
}
*/

//打印叶子节点
void binarytree_printfleaf(Tree* root) {
	if (root == NULL) {
		return;
	}
	if ((root->lchild == NULL) && (root->rchild == NULL)) {
		printf(" %d ", root->data);
	}
	else {
		binarytree_printfleaf(root->lchild);
		binarytree_printfleaf(root->rchild);
	}
}

//打印叶子节点的个数
int binarytree_getleafnum(Tree* root) {
	if (root == NULL) {
		return 0;
	}
	if (root->lchild == NULL && root->rchild == NULL) {
		return 1;
	}
	return binarytree_getleafnum(root->lchild) + binarytree_getleafnum(root->rchild);
}

//打印树的高度
int binarytree_gethigh(Tree* root) {
	int lhigh = 0;
	int rhigh = 0;
	if (root == NULL) {
		return 0;
	}
	lhigh = binarytree_gethigh(root->lchild);
	rhigh = binarytree_gethigh(root->rchild);

	return ((lhigh > rhigh) ? (lhigh + 1) : (rhigh + 1));
}

int main() {
	Tree* root = NULL;

	binarytree_create(&root);
	binarytree_preorder(root);
	binarytree_destory(root);

	return 0;
}

上一篇:福报厂面试之树的遍历、节点统计、树高计算


下一篇:c语言:二叉排序树的操作