二叉树-C语言

#include<stdio.h>
#include<stdlib.h>
typedef struct BiTNode{
	char data;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
int count,x; 
int CreateBiTree(BiTree *T)
{//链式创建二叉树
	char ch;
	getchar();//吞回车 
	scanf("%c",&ch);
	if(ch =='#')
    {
		*T = NULL;  //递归结束,建空树
	}
	else
	{
		if(!(*T= (BiTree)malloc(sizeof(BiTNode))) )
			exit(0);
	    (*T)->data=ch;
		CreateBiTree(&(*T)->lchild);
		CreateBiTree(&(*T)->rchild);
	}
	return 1;
}

int PreOrderTraverse(BiTree T)
{//前序遍历
	if(T == NULL)
		return 1;
	else
	{
		printf("%c",T->data);
		PreOrderTraverse(T->lchild);
		PreOrderTraverse(T->rchild);
	}
	return 1;
}

int InOrderTraverse(BiTree T)
{//中序遍历
    if(T == NULL) return 1;
    else{
        InOrderTraverse(T->lchild);
        printf("%c",T->data);
        InOrderTraverse(T->rchild);
    }
}

int PostOrderTraverse(BiTree T)
{//后序遍历
    if(T == NULL) return 1;
    else
	{
        PostOrderTraverse(T->lchild);
        PostOrderTraverse(T->rchild);
        printf("%c",T->data);
    }
}

int CountLeaf(BiTree root) //采用先序遍历的递归算法
{//统计二叉树中叶子结点的个数
    if(root != NULL) //非二叉树的条件,还可以写成if(root)
    {
        if(!root->lchild && !root->rchild) //是叶子结点则统计并打印
        {
            count++;
            printf("%c\n",root->data);
        }
        CountLeaf(root->lchild); //递归遍历左子树,直到叶子处
        CountLeaf(root->rchild); //递归遍历右子树,直到叶子处
    }
    return 1;
}

int Depth(BiTree T)
{
    if(T == NULL) return 0;
    else{
        int m = Depth(T->lchild);
        int n = Depth(T->rchild);
        if(m >= n)
            return m+1;
        else
            return n+1;
    }
}

int main()
{
	BiTree T;
	int i;
	    printf("1----链式创建二叉树\n");
		printf("2----先序遍历\n");
		printf("3----中序遍历\n");
		printf("4----后序遍历\n");
		printf("5----计算二叉树的深度\n");
		printf("6----统计二叉树中叶子结点的个数\n");
		printf("若退出,请输入一个负数!\n\n");
		
	while ( i>=0 )
	{	
        printf("请输入操作代码:");
		scanf("%d", &i);
		if(i == 1)
		{
			    CreateBiTree(&T);
				printf("链式创建二叉树成功\n");
		}
		else if(i == 2)
		{
			if(PreOrderTraverse(T) )
				printf("先序遍历成功\n");
		}
		else if(i == 3)
		{
			if( InOrderTraverse(T) )
				printf("中序遍历成功\n");
		}
		else if(i == 4)
		{
			if( PostOrderTraverse(T) )
				printf("后序遍历成功\n");
		}
		else if(i == 5)
		{
			if( CountLeaf(T) )
            {
                printf("统计成功!二叉树中叶子结点的个数:%d\n",count);
            }

		}
		else if(i == 6)
		{
			if( Depth(T) )
            {
                x= Depth(T);
                printf("深度为:%d\n",x);
            }

		}
		else if(i < 0)
        {
            printf("退出成功!\n");
            break;
        }

	}

	return 0;
}
/*
 注意点:
  1. BiTNode :指的是结构体,相当于结点
     BiTree :属于指针类型,可以将结点连接起来,所以在定义头结点时,使用指针指向头结点;
  2. C语言中,想要指针实现实参和形参值的一致,Bitree*,(*T)->data,(*T):是指向头结点的指针;
  3. getchar():吞并换行符(类似于回车之类的) 
*/ 






  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
平衡二叉树是一种特殊的二叉树,它的左右子树的高度差不超过1。AVL树是一种自平衡的二叉搜索树,它的高度始终保持在O(log n)。 下面是C语言实现平衡二叉树(AVL树)的代码: ``` #include <stdio.h> #include <stdlib.h> /* 定义平衡二叉树节点结构体 */ struct AVLNode { int data; // 存储的数据 int height; // 节点高度 struct AVLNode *leftChild; // 左子树 struct AVLNode *rightChild; // 右子树 }; /* 获取节点高度 */ int getHeight(struct AVLNode *node) { if (node == NULL) { return -1; } else { return node->height; } } /* 获取节点平衡因子 */ int getBalanceFactor(struct AVLNode *node) { if (node == NULL) { return 0; } else { return getHeight(node->leftChild) - getHeight(node->rightChild); } } /* 更新节点高度 */ void updateHeight(struct AVLNode *node) { node->height = 1 + (getHeight(node->leftChild) > getHeight(node->rightChild) ? getHeight(node->leftChild) : getHeight(node->rightChild)); } /* 右旋操作 */ struct AVLNode *rotateRight(struct AVLNode *node) { struct AVLNode *newRoot = node->leftChild; node->leftChild = newRoot->rightChild; newRoot->rightChild = node; updateHeight(node); updateHeight(newRoot); return newRoot; } /* 左旋操作 */ struct AVLNode *rotateLeft(struct AVLNode *node) { struct AVLNode *newRoot = node->rightChild; node->rightChild = newRoot->leftChild; newRoot->leftChild = node; updateHeight(node); updateHeight(newRoot); return newRoot; } /* 插入操作 */ struct AVLNode *insert(struct AVLNode *root, int data) { if (root == NULL) { root = (struct AVLNode *) malloc(sizeof(struct AVLNode)); root->data = data; root->height = 0; root->leftChild = NULL; root->rightChild = NULL; } else if (data < root->data) { root->leftChild = insert(root->leftChild, data); if (getHeight(root->leftChild) - getHeight(root->rightChild) == 2) { if (data < root->leftChild->data) { root = rotateRight(root); } else { root->leftChild = rotateLeft(root->leftChild); root = rotateRight(root); } } } else if (data > root->data) { root->rightChild = insert(root->rightChild, data); if (getHeight(root->rightChild) - getHeight(root->leftChild) == 2) { if (data > root->rightChild->data) { root = rotateLeft(root); } else { root->rightChild = rotateRight(root->rightChild); root = rotateLeft(root); } } } updateHeight(root); return root; } /* 中序遍历 */ void inOrderTraversal(struct AVLNode *root) { if (root != NULL) { inOrderTraversal(root->leftChild); printf("%d ", root->data); inOrderTraversal(root->rightChild); } } int main() { struct AVLNode *root = NULL; int data[] = {5, 2, 8, 1, 3, 6, 9}; int len = sizeof(data) / sizeof(data[0]); int i; for (i = 0; i < len; i++) { root = insert(root, data[i]); } inOrderTraversal(root); return 0; } ``` 以上代码实现了平衡二叉树的插入和中序遍历操作。在插入操作中,根据插入节点的值和当前节点的值的大小关系,不断递归向左或向右子树进行插入操作,并在递归返回时更新节点高度和进行平衡操作。在平衡操作中,根据节点的平衡因子进行旋转操作,使树重新平衡。在中序遍历操作中,按照左子树、根节点、右子树的顺序遍历树中的节点,输出节点的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值