二叉树及二叉树的遍历

二叉树:
定义:二叉树是n(n≥0)个结点的有限集,它或为空树(n=0),或由一个根结点和两棵分别称为左子树和右子树的互不相交的二叉树构成

特点:每个结点至多有二棵子树(即不存在度大于2的结点),二叉树的子树有左、右之分,且其次序不能任意颠倒。

注意区分二叉树、树、度为2的有序树:

度值的区别:二叉树的度不超过2,但不一定是2。对于二叉树的子树而言,要么是根的左子树,要么是根的右子树,即使只有一棵子树也要区分是左是右。度为2的有序树中,当一个结点有两棵子树时有左右之分,而只有一棵子树时就无左右之分。

性质:

1.若二叉树的层次从i开始,则在二叉树的第i层最多有个结点。(i>1)

2.高度为k的二叉树最多有个结点。(k≥1)

3.对任何一棵二叉树,如果其叶结点个数为n,度为2的非叶结点个数为,则有。

4.具有n个结点的完全二叉树的高度为+1。

5.对于具有n个结点的完全二叉树,如果按照从上到下和从左到右的顺序对二叉树中的所有结点从1开始顺序编号,则对于任意的序号为i的结点有:
(1)若i=1,则i无双亲结点,若i>1,则i的双亲结点为i/2
(2)若2*i>n,则i无左孩子,若2*i<n,则i结点的左孩子结点为2*i
(3)若2*i+1 >n,则i无右孩子,若2*i+1≤n,则i的右孩子结点为2*i+1

两种特殊的二叉树:

1.满二叉树:深度为k且有个结点的二叉树
2.完全二叉树 :每个结点都与等高的满二叉树中结点层序编号一一对应的二叉树

深度为k的完全二叉树在k-1层上一定是满二叉树。

叶子结点只能出现在最下两层,且最下层的叶子结点都集中在二叉树的左部。

完全二叉树中如果有度为1的结点,只可能有一个,且该结点只有左孩子。

通俗来说,完全二叉树从根结点到倒数第二层满足完美二叉树,最后一层可以不完全填充,其叶子结点都靠左对齐。满二叉树必为完全二叉树,而完全二叉树不一定是满二叉树。
 

代码如下:

#include<stdio.h>
#include<malloc.h>
#define QUEUE_SIZE 5
 
/**
 * Binary tree node.
 */
typedef struct BTNode
{
	char element;
	BTNode* left;
	BTNode* right;
}BTNode, * BTNodePtr;
 
/**
 *A queue with a number of pointers.
 */
typedef struct BTNodePtrQueue
{
	BTNodePtr* nodePtrs;
	int front;
	int rear;
}BTNodePtrQueue, * QueuePtr;
 
/**
 *Initialize the queue.
 */
QueuePtr initQueue()
{
	QueuePtr resultQueuePtr = (QueuePtr)malloc(sizeof(struct BTNodePtrQueue));
	resultQueuePtr->nodePtrs = (BTNodePtr*)malloc(sizeof(BTNodePtr) * QUEUE_SIZE);
	resultQueuePtr->front = 0;
	resultQueuePtr->rear = 1;
	return resultQueuePtr;
}//of initQueuePtr
 
/**
 * Is the queue empty?
 */
bool isQueueEmpty(QueuePtr paraQueuePtr)
{
	if ((paraQueuePtr->front + 1) % QUEUE_SIZE == paraQueuePtr->rear)
	{
		return true;
	}//of if
 
	return false;
}//of isQueueEmpty
 
/**
 *Add a pointer to the queue.
 */
void enqueue(QueuePtr paraQueuePtr, BTNodePtr paraBTNodePtr)
{
	printf("front = %d, rear = %d.\n", paraQueuePtr->front, paraQueuePtr->rear);
	if ((paraQueuePtr->rear + 1) % QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE)
	{
		printf("Error, trying to enqueue %c.queue full.\n", paraBTNodePtr->element);
		return;
	}//of if
	paraQueuePtr->nodePtrs[paraQueuePtr->rear] = paraBTNodePtr;
	paraQueuePtr->rear = (paraQueuePtr->rear + 1) % QUEUE_SIZE;
	printf("enqueue %c ends.\n", paraBTNodePtr->element);
}//of enqueue
 
/**
 *Remove an element from the queue and return.
 */
BTNodePtr dequeue(QueuePtr paraQueuePtr)
{
	if (isQueueEmpty(paraQueuePtr))
	{
		printf("Error, empty queue.\n");
		return NULL;
	}//of if
 
	paraQueuePtr->front = (paraQueuePtr->front + 1) % QUEUE_SIZE;
 
	printf("dequeue %c ends.\n", paraQueuePtr->nodePtrs[paraQueuePtr->front]->element);
	return paraQueuePtr->nodePtrs[paraQueuePtr->front];
}//of dequeue
 
/**
 *Construct a BTNode using the given char.
 */
BTNodePtr constructBTNode(char paraChar)
{
	BTNodePtr resultPtr = (BTNodePtr)malloc(sizeof(BTNode));
	resultPtr->element = paraChar;
	resultPtr->left = NULL;
	resultPtr->right = NULL;
	return resultPtr;
}//of constructBTNode
 
/**
 * Construct a binary tree using the given string.
 */
BTNodePtr stringToBTree(char* parastring)
{
	int i;
	char ch;
 
	//Use a queue to manage the pointers
	QueuePtr tempQueuePtr = initQueue();
 
	BTNodePtr resultHeader;
	BTNodePtr tempParent, tempLeftChild, tempRightChild;
	i = 0;
	ch = parastring[i];
	resultHeader = constructBTNode(ch);
	enqueue(tempQueuePtr, resultHeader);
 
	while (!isQueueEmpty(tempQueuePtr))
	{
		tempParent = dequeue(tempQueuePtr);
 
		//The left child.
		i++;
		ch = parastring[i];
		if (ch == '#')
		{
			tempParent->left = NULL;
		}
		else
		{
			tempLeftChild = constructBTNode(ch);
			enqueue(tempQueuePtr, tempLeftChild);
			tempParent->left = tempLeftChild;
		}//of if
 
		//The right child.
		i++;
		ch = parastring[i];
		if (ch = '#')
		{
			tempParent->right = NULL;
		}
		else
		{
			tempRightChild = constructBTNode(ch);
			enqueue(tempQueuePtr, tempRightChild);
			tempParent->right = tempRightChild;
		}//of if
	}//of while
 
	return resultHeader;
}//of stringToBTree
 
/**
 *Levelwise.
 */
void levelwise(BTNodePtr paraTreePtr)
{
	//Use a queue to manage the pointers
	char tempString[100];
	int i = 0;
	QueuePtr tempQueuePtr = initQueue();
	BTNodePtr tempNodePtr;
	enqueue(tempQueuePtr, paraTreePtr);
	while (!isQueueEmpty(tempQueuePtr))
	{
		tempNodePtr = dequeue(tempQueuePtr);
 
		//For output
		tempString[i] = tempNodePtr->element;
		i++;
 
		if (tempNodePtr->left != NULL)
		{
			enqueue(tempQueuePtr, tempNodePtr->left);
		}//of if
		if (tempNodePtr->right != NULL)
		{
			enqueue(tempQueuePtr, tempNodePtr->right);
		}//of if
	}//of while
	tempString[i] = '\0';
 
	printf("Levelwise: %s\n", tempString);
}//of levelwise
 
/**
 *Preorder.
 */
void preorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//of if
 
	printf("%c", tempPtr->element);
	preorder(tempPtr->left);
	preorder(tempPtr->right);
}//of preorder
 
/**
 *Inorder.
 */
void inorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//of if
 
	inorder(tempPtr->left);
	printf("%c", tempPtr->element);
	inorder(tempPtr->right);
}//of inorder
 
/**
 *Postorder.
 */
void postorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//of if
 
	postorder(tempPtr->left);
	postorder(tempPtr->right);
	printf("%c", tempPtr->element);
}//of postorder
 
/**
 *The entrance.
 */
int main()
{
	BTNodePtr tempHeader;
	tempHeader = constructBTNode('c');
	printf("There is only one node. Preorder visit: ");
	preorder(tempHeader);
	printf("\n");
 
	char* tempString = "acde#bf######";
	tempHeader = stringToBTree(tempString);
	printf("Preorder: ");
	preorder(tempHeader);
	printf("\n");
	printf("Inorder: ");
	inorder(tempHeader);
	printf("\n");
	printf("Postorder: ");
	postorder(tempHeader);
	printf("\n");
	printf("Levelwise: ");
	levelwise(tempHeader);
	printf("\n");
 
	return 1;
}//of main

先序遍历:

void preorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//of if
 
	printf("%c", tempPtr->element);
	preorder(tempPtr->left);
	preorder(tempPtr->right);
}//of preorder

中序遍历:

void inorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//of if
 
	inorder(tempPtr->left);
	printf("%c", tempPtr->element);
	inorder(tempPtr->right);
}//of inorder
 

后序遍历:

void postorder(BTNodePtr tempPtr)
{
	if (tempPtr == NULL)
	{
		return;
	}//of if
 
	postorder(tempPtr->left);
	postorder(tempPtr->right);
	printf("%c", tempPtr->element);
}//of postorder

运行结果:

二叉树n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互不相交的、分别称为根结点的左子树和右子树组成。

先序遍历:先访问根,再先序访问左子树,最后先序访问右子树。

中序遍历:先中序访问左子树,然后访问根,最后中序访问右子树。

后序遍历:先后序访问左子树,再后序访问右子树,最后访问根。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值