数据结构学习笔记(10)链式二叉树

完整代码+测试函数

目录

Tree.h

Tree.c

Test.c


Tree.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
typedef char DataType;

//定义一个结点类型
typedef struct Node
{
	DataType data;
	struct Node* leftChild;
	struct Node* rightChild;
}TreeNode;

//初始化树
void TreeInitiate(TreeNode** root);
//左插入孩子结点(返回原根节点的左孩子结点的指针)
TreeNode* LeftInsert(TreeNode* current, DataType x);
//右插入孩子结点(返回原根节点的右孩子节点的指针)
TreeNode* RightInsert(TreeNode* current, DataType x);
//删除左子树(删除当前节点的所有左子树,返回原根节点)
TreeNode* LeftDelete(TreeNode* current);
删除右子树(删除当前节点的所有右子树,返回原根节点)
TreeNode* RightDelete(TreeNode* current);

//前序遍历
void PreOrder(TreeNode* root, void visit(DataType item));
//中序遍历
void InOrder(TreeNode* root, void visit(DataType item));
//后续遍历
void PostOrder(TreeNode* root, void visit(DataType item));
//撤销
void Destory(TreeNode** root);

//打印二叉树
void PrintTree(TreeNode* root, int n);
//查找数据元素
TreeNode* Search(TreeNode* root, DataType x);

Tree.c

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include"Tree.h"


//初始化树
void TreeInitiate(TreeNode** root)
{
	*root = (TreeNode*)malloc(sizeof(TreeNode));
	(*root)->leftChild = NULL;
	(*root)->rightChild = NULL;
}

//左插入
TreeNode* LeftInsert(TreeNode* current, DataType x)
{
	TreeNode* s, * p;
	if (current == NULL)
	{
		return NULL;
	}
	p = current->leftChild;
	s = (TreeNode*)malloc(sizeof(TreeNode));
	s->data = x;
	s->leftChild = p;
	s->rightChild = NULL;
	current->leftChild = s;
	return current->leftChild;
}

//右插入
TreeNode* RightInsert(TreeNode* current, DataType x)
{
	TreeNode* s, * p;
	if (current == NULL)
	{
		return NULL;
	}
	p = current->rightChild;
	s = (TreeNode*)malloc(sizeof(TreeNode));
	s->data = x;
	s->rightChild = p;
	s->leftChild = NULL;
	current->rightChild = s;
	return current->rightChild;
}
//删除左子树
TreeNode* LeftDelete(TreeNode* current)
{
	if (current == NULL || current->leftChild == NULL)
	{
		return NULL;
	}
	else
	{
		Destory(&current->leftChild);
		current->leftChild == NULL;
		return current;
	}
}
//删除右子树
TreeNode* RightDelete(TreeNode* current)
{
	if (current == NULL || current->rightChild == NULL)
	{
		return NULL;
	}
	else
	{
		Destory(&current->rightChild);
		current->rightChild == NULL;
		return current;
	}
}


//前序遍历
void PreOrder(TreeNode* root, void visit(DataType item))
{
	if (root != NULL)
	{
		visit(root->data);
		PreOrder(root->leftChild, visit);
		PreOrder(root->rightChild, visit);
	}
}
//中序遍历
void InOrder(TreeNode* root, void visit(DataType item))
{
	if (root != NULL)
	{
		InOrder(root->leftChild, visit);
		visit(root->data);
		InOrder(root->rightChild, visit);
	}
}
//后序遍历
void PostOrder(TreeNode* root, void visit(DataType item))
{
	if (root != NULL)
	{
		PostOrder(root->leftChild, visit);
		PostOrder(root->rightChild, visit);
		visit(root->data);
	}
}
//撤销操作
void Destory(TreeNode** root)
{
	if ((*root) != NULL && (*root)->leftChild != NULL)
	{
		Destory(&(*root)->leftChild);
	}
	if ((*root) != NULL && (*root)->rightChild != NULL)
	{
		Destory(&(*root)->rightChild);
	}
	free(*root);
}

//打印二叉树
//把二叉树逆时针旋转90°,按照二叉树的凹入表示法打印二叉树
//打印二叉树root,n为缩进层数,初始值为0
void PrintTree(TreeNode* root, int n)
{
	int i;
	if (root == NULL)
	{
		return;
	}
	PrintTree(root->rightChild, n + 1);
	//访问根节点
	for (i = 0; i < n - 1; i++)
	{
		printf("   ");
	}
	if(n>0)
	{
		printf("---");
		printf("%c\n", root->data);
	}
	PrintTree(root->leftChild, n + 1);
}

//查找数据元素
//查找数据元素x是否在二叉树中
//查找到则返回该结点指针,未查找到则返回空指针
TreeNode* Search(TreeNode* root, DataType x)
{
	//初始标记为空指针
	TreeNode* find = NULL;
	if (root != NULL)
	{
		//根结点就是所查结点
		if (root->data == x)
		{
			find = root;
		}
		else
		{
			find = Search(root->leftChild, x);//在左子树中查找
			if (find == NULL)
			{
				find = Search(root->rightChild, x);//在右子树中查找
			}
		}
	}
	return find;
}

Test.c

#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
#include<malloc.h>
#include"Tree.h"

//访问操作函数
void visit(DataType item)
{
	printf("%c   ", item);
}

打印二叉树
把二叉树逆时针旋转90°,按照二叉树的凹入表示法打印二叉树
打印二叉树root,n为缩进层数,初始值为0
//void PrintTree(TreeNode* root, int n)
//{
//	int i;
//	if(root==NULL)
//		{
//			return;
//		}
//	PrintTree(root->rightChild, n + 1);
//	//访问根节点
//	for (i = 0; i < n - 1; i++)
//	{
//		printf("---");
//		printf("%c\n", root->data);
//	}
//	PrintTree(root->leftChild, n + 1);
//}
//
查找数据元素
查找数据元素x是否在二叉树中
查找到则返回该结点指针,未查找到则返回空指针
//TreeNode* Search(TreeNode* root, DataType x)
//{
//	//初始标记为空指针
//	TreeNode* find = NULL;
//	if(root!=NULL)
//		{
//		//根结点就是所查结点
//		if (root->data == x)
//		{
//			find = root;
//		}
//		else
//		{
//			find = Search(root->leftChild,x);//在左子树中查找
//			if (find == NULL)
//			{
//				find = Search(root->rightChild,x);//在右子树中查找
//			}
//		}
//		}
//	return find;
//}

void main(void)
{
	TreeNode* root, * p, * find;
	char x = 'E';
	//初始化树
	TreeInitiate(&root);
	//插入树中的元素
	p = LeftInsert(root, 'A');
	p = LeftInsert(p, 'B');
	p = LeftInsert(p, 'D');
	p = RightInsert(p, 'G');
	p = RightInsert(root->leftChild, 'C');
	RightInsert(p, 'E');
	RightInsert(p, 'F');
	//打印二叉树
	PrintTree(root, 0);
	//三种遍历树
	printf("前序遍历:");
	PreOrder(root->leftChild, visit);
	printf("\n中序遍历:");
	InOrder(root->leftChild, visit);
	printf("\n后序遍历:");
	PostOrder(root->leftChild, visit);
	//查找数据元素E是否在树中
	find = Search(root, x);
	if (find != NULL)
	{
		printf("\n数据元素%c在树中", x);
	}
	else
	{
		printf("\n数据元素%c不在树中", x);
	}
	Destory(&root);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值