【数据结构】—— 二叉树相关接口的实现

公共头文件common.h

#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>


typedef char BTDatatype;

typedef struct BinaryTreeNode
{
	BTDatatype _data;
	struct BinaryTreeNode* _left;
	struct BinaryTreeNode* _right;

}BTNode;

typedef BTNode* QDataType;
typedef struct QueueNode
{
	QDataType _data;
	struct QueueNode* _next;
}QueueNode;

typedef struct Queue
{
	QueueNode* _head;
	QueueNode* _tail;	
}Queue;

二叉树相关接口的介绍BTree.h

#ifndef __BTREE_H__
#define __BTREE_H__
#include"common.h"


//通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDatatype* array, int* pIndex);
void BinaryTreeDestory(BTNode** root);

int BinaryTreeSize(BTNode* root);
int BinaryTreeLeafSize(BTNode* root);
int BinaryTreeLevelSize(BTNode* root, int k);

BTNode* BinaryTreeFind(BTNode* root, BTDatatype x);

//遍历
void BinaryTreePrevOrder(BTNode* root);
void BinaryTreeInOrder(BTNode* root);
void BinaryTreePostOrder(BTNode* root);

//层序遍历
void  BinaryTreeLevelOrder(BTNode* root);
//判断二叉树是否为完全二叉树
int BinaryTreeComplete(BTNode* root);

void BTreeTest();

#endif //__BTREE_H__

队列头文件Queue.h(为实现二叉树的层序遍历)

#pragma once
#include"common.h"


void QueueInit(Queue* pq);
void QueueDestory(Queue* pq);
void QueuePush(Queue* pq,QDataType x);
void QueuePop(Queue* pq);

QDataType QueueFront(Queue* pq);

int QueueEmpty(Queue* pq);
int QueueSize(Queue* pq);
void QueuePrint(Queue* pq);

void QueueTest();

队列接口的实现Queue.c

#include"common.h"
#include"Queue.h"


//队列的初始化
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->_head = (QueueNode*)malloc(sizeof(QueueNode));
	assert(pq->_head);
	pq->_tail = pq->_head;
	pq->_head->_next = NULL;

}
void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->_head;
	while (cur)
	{
		QueueNode* next = cur->_next;
		free(cur);
		cur = next;
	}
	
}
void QueuePush(Queue* pq, QDataType x)
{
	assert(pq);

	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	assert(newnode);
	
	newnode->_data = x;
	newnode->_next = NULL;
	pq->_tail->_next = newnode;
	pq->_tail = newnode;
}

int QueueEmpty(Queue* pq)
{
	return pq->_head != pq->_tail;
}
int QueueSize(Queue* pq)
{
	int count = 0;
	if (QueueEmpty(pq) == 0)
		return 0;
	else
	{
		QueueNode* cur = pq->_head->_next;
		while (cur)
		{
			++count;
			cur = cur->_next;
		}
	}
	return count;
}
void QueuePop(Queue* pq)
{
	QueueNode*cur = NULL;

	assert(pq);
	if (QueueEmpty(pq) == 0)
		return;
	cur = pq->_head->_next;//保存队头的第一个元素
	if (pq->_tail == cur)//若队列中只有一个元素,让尾指针指向头指针
	{
		pq->_tail = pq->_head;
	}
	pq->_head->_next = cur->_next;//删除第一个元素
	free(cur);
}
QDataType QueueFront(Queue* pq)
{
	assert(pq);
	return pq->_head->_next->_data;
}

void QueuePrint(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->_head->_next;
	while (cur)
	{
		printf("%d ", cur->_data);
		cur = cur->_next;
	}
	printf("\n");
}
void QueueTest()
{
	Queue q;
	//QueueInit(&q);
	//QueuePush(&q, 1);
	//QueuePush(&q, 2);
	//QueuePush(&q, 3);
	printf("%d\n",QueueFront(&q));
	QueuePop(&q);
	//printf("%d\n", QueueEmpty(&q));
	//printf("%d\n", QueueSize(&q));
	//QueuePrint(&q);
	QueueDestory(&q);
}

二叉树相关接口的实现BTree.h

#define _CRT_SECURE_NO_WARNINGS
#include"common.h"
#include"BTree.h"
#include"Queue.h"

//通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDatatype* array, int* pIndex)
{
	if (array[(*pIndex)] == '#')
	{
		(*pIndex)++;
		return NULL;
	}

		BTNode* root = (BTNode*)malloc(sizeof(BTNode));
		root->_left = NULL;
		root->_right = NULL;
		root->_data = array[(*pIndex)];

		(*pIndex)++;
		root->_left = BinaryTreeCreate(array, pIndex);
		root->_right = BinaryTreeCreate(array, pIndex);

		return root;
	

}
void BinaryTreeDestory(BTNode** root)
{

}

int BinaryTreeSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	return BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right) + 1;
}
//int BinaryTreeLeafSize(BTNode* root)
//{
//	if (root == NULL)
//		return 0;
//	int LeftLeaf = 0;
//	int RightLeaf = 0;
//
//	if (root->_left == NULL && root->_right == NULL)
//		return 1;
//	LeftLeaf = BinaryTreeLeafSize(root->_left);
//	RightLeaf = BinaryTreeLeafSize(root->_right);
//
//	return LeftLeaf + RightLeaf;
//}
int BinaryTreeLeafSize(BTNode* root)
{
	if (root == NULL)
		return 0;
	if (root->_left == NULL && root->_right == NULL)
		return 1;
	return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}
int BinaryTreeLevelSize(BTNode* root, int k)
{
	if (root == NULL)
		return 0;
	if (k == 1)
		return 1;
	return BinaryTreeLevelSize(root->_left, k - 1) 
		+ BinaryTreeLevelSize(root->_right, k - 1);
}

BTNode* BinaryTreeFind(BTNode* root, BTDatatype x)
{
	BTNode* ret = NULL;
	if (root == NULL)
		return NULL;
	if (root->_data == x)
		return root;
	ret = BinaryTreeFind(root->_left, x);
	if (ret)
		return ret;
	ret = BinaryTreeFind(root->_right, x);
	if (ret)
		return ret;

	return NULL;
}

//遍历
void BinaryTreePrevOrder(BTNode* root)
{
	if (root == NULL)
		return;
	printf("%c ", root->_data);

	BinaryTreePrevOrder(root->_left);
	BinaryTreePrevOrder(root->_right);
}
void BinaryTreeInOrder(BTNode* root)
{
	if (root == NULL)
		return;
	
	BinaryTreeInOrder(root->_left);
	printf("%c ", root->_data);
	BinaryTreeInOrder(root->_right);

}
void BinaryTreePostOrder(BTNode* root)
{
	if (root == NULL)
		return;
	BinaryTreePostOrder(root->_left);
	BinaryTreePostOrder(root->_right);
	printf("%c ", root->_data);
	
}

//层序遍历
void  BinaryTreeLevelOrder(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root != NULL)
	{
		QueuePush(&q, root);
		while (QueueEmpty(&q) != 0)
		{
			BTNode* front = QueueFront(&q);
			printf("%c ", front->_data);
			QueuePop(&q);
			if (front->_left)
				QueuePush(&q, front->_left);
			if (front->_right)
				QueuePush(&q, front->_right);
		}
		printf("\n");
	}
}
//判断二叉树是否为完全二叉树
int BinaryTreeComplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root != NULL)
	{
		QueuePush(&q, root);
		while (QueueEmpty(&q) != 0)
		{
			BTNode* front = QueueFront(&q);
			QueuePop(&q);

			if (front == NULL)
				break;

			QueuePush(&q, front->_left);
			QueuePush(&q, front->_right);
		}
		while (QueueEmpty(&q) != 0)
		{
			BTNode* front = QueueFront(&q);
			if (front != NULL)
			{
				QueueDestory(&q);
				return 0;
			}
			QueuePop(&q);
		}
	}
	return 1;
}


void BTreeTest()
{
	char array[100] = "ABD##E#H##CF##G##";
	//构造二叉树
	int Index = 0;
	BTNode* tree = BinaryTreeCreate(array, &Index);
	
	printf("%d\n", BinaryTreeSize(tree));
	printf("%d\n", BinaryTreeLevelSize(tree,3));
	printf("%d\n", BinaryTreeLeafSize(tree));
	BinaryTreePrevOrder(tree);
	printf("\n");
	BinaryTreeInOrder(tree);
	printf("\n");
	BinaryTreePostOrder(tree);
	printf("\n");
	BinaryTreeLevelOrder(tree);

	printf("%d\n", BinaryTreeComplete(tree));
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * 二叉树节点ADT接口 */ package dsa; public interface BinTreePosition extends Position { //判断是否有父亲(为使代码描述简洁) public boolean hasParent(); //返回当前节点的父节点 public BinTreePosition getParent(); //设置当前节点的父节点 public void setParent(BinTreePosition p); //判断是否为叶子 public boolean isLeaf(); //判断是否为左孩子(为使代码描述简洁) public boolean isLChild(); //判断是否有左孩子(为使代码描述简洁) public boolean hasLChild(); //返回当前节点的左孩子 public BinTreePosition getLChild(); //设置当前节点的左孩子(注意:this.lChild和c.parent都不一定为空) public void setLChild(BinTreePosition c); //判断是否为右孩子(为使代码描述简洁) public boolean isRChild(); //判断是否有右孩子(为使代码描述简洁) public boolean hasRChild(); //返回当前节点的右孩子 public BinTreePosition getRChild(); //设置当前节点的右孩子(注意:this.rChild和c.parent都不一定为空) public void setRChild(BinTreePosition c); //返回当前节点后代元素的数目 public int getSize(); //在孩子发生变化后,更新当前节点及其祖先的规模 public void updateSize(); //返回当前节点的高度 public int getHeight(); //在孩子发生变化后,更新当前节点及其祖先的高度 public void updateHeight(); //返回当前节点的深度 public int getDepth(); //在父亲发生变化后,更新当前节点及其后代的深度 public void updateDepth(); //按照中序遍历的次序,找到当前节点的直接前驱 public BinTreePosition getPrev(); //按照中序遍历的次序,找到当前节点的直接后继 public BinTreePosition getSucc(); //断绝当前节点与其父亲的父子关系 //返回当前节点 public BinTreePosition secede(); //将节点c作为当前节点的左孩子 public BinTreePosition attachL(BinTreePosition c); //将节点c作为当前节点的右孩子 public BinTreePosition attachR(BinTreePosition c); //前序遍历 public Iterator elementsPreorder(); //中序遍历 public Iterator elementsInorder(); //后序遍历 public Iterator elementsPostorder(); //层次遍历 public Iterator elementsLevelorder(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值