Data structure, binary tree traversal

The first is the creation of a binary tree

typedef struct BTreeNode
{
    TelernType data;      //树的数据域为字符型
    struct BTreeNode* LChild;         //*左孩子指针
    struct BTreeNode* RChild;         //*右孩子指针
}BTree;



BTree* CreatBTree() //创建二叉树
{
    BTree* T; 
    char x;
    //printf("创建二叉树请输入根节点:");
    scanf("%c", &x); //字符串接收
    getchar();
    if (x == '0') //当左右子树不存在时用0代替
        T = NULL;
    else
    {
        T = (BTree*)malloc(sizeof(BTree));  //开辟一个动态空间强制转换成BTree类型
        T->data = x;
        printf(" 请输入 %c 结点的左孩子:", T->data);
        T->LChild = CreatBTree();
        printf(" 请输入 %c 结点的右孩子:", T->data);
        T->RChild = CreatBTree();
    }
    return T;
}

Preorder traversal of a binary tree

void PreOrder(BTree* T)
{
	if (T == NULL)
	{
		return;
	}
	printf("%c ", T->data);
	PreOrder(T->LChild);
	PreOrder(T->RChild);

}

Find the height of a binary tree (recursively):

int getDept(BTree*T)
{
	int LD, RD;
	if (T == NULL)
		return 0;
	else
	{
		LD = getDept(T->LChild);
		RD = getDept(T->RChild);
		return (RD > LD ? RD : LD) + 1;
	}
}

You can also use iteration to find the height of a binary tree: (Choose one of the two)

int getDept(BTree* T) {
	if (T == NULL) { //如果为空则返回0
		return 0;
	}
	int height = 0;       //计算树的高度,遍历完一层+1
	BTree* queue[100];    // 初始化队列,队列空间足够大
	int front = 0, rear = 0; //
	queue[rear++] = T;
	while (front < rear) { //初始条件非空
		int size = rear - front;    // 当前层节点数
		while (size--) {
			BTree* T = queue[front++];
			if (T->LChild) {
				queue[rear++] = T->LChild;    // 将左子节点加入队列中
			}
			if (T->RChild) {
				queue[rear++] =T->RChild;   // 将右子节点加入队列中
			}
		}
		height++;    // 每遍历完一层,树高度加1
	}
	return height;
}

Count the number of leaf nodes

int count = 0;
void leafnum(BTree* T)
{
	if (T)        //树不空
	{
		if (T->LChild == NULL && T->RChild == NULL)
			count++;
		leafnum(T->LChild);
		leafnum(T->RChild);
	}
}

Main function: used to select binary tree operations:

void main()
{
	BTree* T = NULL;
	int k;

	do
	{
		printf("\n\n\n\n");
		printf("\t\t\t  树 子系统\n");
		printf("\t\t******************************\n");
		printf("\t\t*        1----建二叉树    *\n");
		printf("\t\t*        2----前序遍历    *\n");
		printf("\t\t*        3----中序遍历    *\n");
		printf("\t\t*        4----后序遍历    *\n");
		printf("\t\t*        5----求树高度      *\n");
		printf("\t\t*        6----叶子个数     *\n");
		printf("\t\t*        0----返  回    *\n");
		printf("\t\t******************************\n");
		printf("\t\t 请选择菜单项(0-5):");
		scanf("%d", &k); getchar();
		if (k == 1)
		{
			//建立二叉树
			printf("\n 请输入此树的根结点:");
			T =CreatBTree();				
		}
		else if (k == 2)
		{
			printf("\n   此树前序遍历的顺序:");
			PreOrder(T);
	
		}
		else if (k == 3)
		{
			printf("\n   此树中序遍历的顺序:");
			InOrder(T);
			
		}
		else if (k == 4)       //查找线性表中元素值为x的位置
		{
			printf("\n   此树后序遍历的顺序:");
			postOrder(T);

		}
		else if (k == 5)        //求树的高度
		{
			int h;
			h = getDept(T);
			printf("\n此树的高度是:%d",h);
		}
		else if (k == 6)        //输出链表
		{
			leafnum(T);
			printf("\n此树叶子结点个数是:%d",count);
		}
	} while (k != 0);
}

Guess you like

Origin blog.csdn.net/apple_71040140/article/details/130494621