C语言实现二叉树遍历实验

1Problem description

Create binary tree as follow (Figure-1) in computer, write out the functions of inOrder , preOrder , postOrder and levelOrder, and use them to traversal the binary tree. And compute the leaf number and height of the binary tree.

Hint: You may choose suitable representation, such as linked representation is often used.

Figure-1 a binary tree

2Steps and restrict conditions

Step 1.  Write the function of create to create a tree by input data. The input format of the binary tree in Figure-1 is as : ABD#G###CE##FH###.

Step 2.   Write recursive version functions of inOrder, preOrder and postOrder to traversal the tree.

Step 3.   Select a suitable representation to implement stack ADT, which must at least has five basic operations: IsFull, IsEmpty, Push and Pop. The element type in the stack is pointer of node.

Step 4.  Implement Queue ADT represented by circular queue, which has seven basic operations: IsEmpty, IsFull, AddQ, DeleteQ. The element type in the queue is pointer of node.

Step 5.  Write an iterative version of inorder, the name is iterInorder() to inorder traversal tree.

Step 6. Write a function for level order traversal of binary tree, the name is levelOrder.

Step 7. Write a function to compute the leaf number of the binary tree, the name is leaf.

Step 8. Write a function to compute the height of the binary tree, the name is height.

#pragma warning(disable: 4996)
#include<stdio.h>
#include<stdlib.h>
#define Maxsize 100

typedef struct TNode* Tree;
typedef struct SNode* Stack;
typedef struct QNode* Queue;
struct TNode {
	char data;
	Tree Left;
	Tree Right;
};

struct SNode {
	Tree data[Maxsize];
	int top;
};

struct QNode {
	Tree data[Maxsize];
	int front;
	int rear;
};
//Tree
Tree CreatTree() {
	char t;
	scanf("%c", &t);
	//	getchar();
	Tree tTree;

	if (t != '#') {
		tTree = (Tree)malloc(sizeof(struct TNode));
		tTree->data = t;
		tTree->Left = CreatTree();
		tTree->Right = CreatTree();
		return tTree;
	}
	return NULL;
}
void inOrder(Tree tTree) {
	if (tTree != NULL) {
		inOrder(tTree->Left);
		printf("%c ", tTree->data);
		inOrder(tTree->Right);
	}
}
void preOrder(Tree tTree) {
	if (tTree != NULL) {
		printf("%c ", tTree->data);
		preOrder(tTree->Left);
		preOrder(tTree->Right);
	}
}
void postOrder(Tree tTree) {
	if (tTree != NULL) {
		postOrder(tTree->Left);
		postOrder(tTree->Right);
		printf("%c ", tTree->data);
	}
}
//Stack
Stack CreatStack() {
	Stack s = (Stack)malloc(sizeof(struct SNode));
	s->top = -1;
	return s;
}
int isFull(Stack tStack) {
	if (tStack->top == Maxsize - 1) {
		return 1;
	}
	return 0;
}
int isEmpty(Stack tStack) {
	if (tStack->top == -1) {
		return 1;
	}
	return 0;
}
void Push(Stack tStack, Tree tTree) {
	if (!isFull(tStack)) {
		tStack->top++;
		tStack->data[tStack->top] = tTree;
	}
}
Tree Pop(Stack tStack) {
	if (!isEmpty(tStack)) {
		Tree tTree = tStack->data[tStack->top];
		tStack->top--;
		return tTree;
	}
}
void inOrderByStack(Tree tTree) {
	Tree t;
	t = tTree;
	Stack tStack = CreatStack();
	while (tTree != NULL || !isEmpty(tStack)) {
		while (tTree != NULL) {
			Push(tStack, tTree);
			tTree = tTree->Left;
		}
		if (!isEmpty(tStack)) {
			tTree = Pop(tStack);
			printf("%c ", tTree->data);
			tTree = tTree->Right;
		}
	}
}
//Queue
Queue CreatQueue() {
	Queue tQueue;
	tQueue = (Queue)malloc(sizeof(struct QNode));
	tQueue->front = 0;//指向队头的前一个元素
	tQueue->rear = 0;
	return tQueue;
}
int isEmptyQ(Queue tQueue) {
	if (tQueue->front == tQueue->rear) {
		return 1;
	}
	return 0;
}
int isFullQ(Queue tQueue) {
	if (tQueue->front == (tQueue->rear+1) % Maxsize) {
		return 1;
	}
	return 0;
}
void AddQ(Queue tQueue,Tree tTree) {
	if (!isFullQ(tQueue)) {
		tQueue->rear = (tQueue->rear + 1) % Maxsize;
		tQueue->data[tQueue->rear] = tTree;
	}
}
Tree DeleteQ(Queue tQueue) {
	if (!isEmptyQ(tQueue)) {
		tQueue->front = (tQueue->front + 1) % Maxsize;
		return tQueue->data[tQueue->front];
	}
}
void LevelOrder(Tree tTree) {
	Queue tQueue;
	if (tTree == NULL) {
		return;
	}
	tQueue = CreatQueue();
	AddQ(tQueue, tTree);
	while (!isEmptyQ(tQueue)) {
		tTree = DeleteQ(tQueue);
		printf("%c ", tTree->data);
		if (tTree->Left != NULL) {
			AddQ(tQueue, tTree->Left);
		}
		if (tTree->Right != NULL) {
			AddQ(tQueue, tTree->Right);
		}
	}
}
//leaf
int totalLeaf = 0;
void Leaf(Tree tTree) {
	if (tTree != NULL) {
		if (tTree->Left == NULL && tTree->Right == NULL) {
			totalLeaf++;
		}
		Leaf(tTree->Left);
		Leaf(tTree->Right);
	}
}
//height
int Height(Tree tTree) {
	int hL, hR, hMax;
	if (tTree != NULL) {
		hL = Height(tTree->Left);
		hR = Height(tTree->Right);
		if (hL >= hR) {
			hMax = hL;
		}
		else {
			hMax = hR;
		}
		return hMax + 1;
	}
	return 0;
}
int main() {
	Tree PtrT;
	PtrT = CreatTree();
	//first and second
	printf("inOrder:");
	inOrder(PtrT);
	printf("\n");
	printf("preOrder:");
	preOrder(PtrT);
	printf("\n");
	printf("postOrder:");
	postOrder(PtrT);
	printf("\n");
	//third
	printf("inOrderByStack:");
	inOrderByStack(PtrT);
	printf("\n");
	//forth
	printf("LevelOrder:");
	LevelOrder(PtrT);
	printf("\n");
	//fifth
	printf("totalLeaf:");
	Leaf(PtrT);
	printf("%d \n", totalLeaf);
	//sixth
	printf("height:");
	printf("%d \n",Height(PtrT));

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值