链表实现 二叉树

文章目录

  • 一、二叉树的链表结构
  • 二、遍历二叉树
    • 1.前序遍历
    • 2.中序遍历
    • 3.后序遍历
  • 三、链表二叉树的部分功能
  • 四、相关的leetcode题
  • 总结

一、链表结构构建二叉树

头文件

#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

二叉树的结构

typedef int STDatatype;
typedef struct Stree
{
STDatatype *data;//数据
struct STree *left;//左子树
struct STree *right;//右子树
}BTNode;

二叉树的节点(浅试一下6个节点)

BTNode* CreateTree()
{
	BTNode * n1 = (BTNode*)malloc(sizeof(BTNode));
	assert(n1);
	BTNode * n2 = (BTNode*)malloc(sizeof(BTNode));
	assert(n2);
	BTNode * n3 = (BTNode*)malloc(sizeof(BTNode));
	assert(n3);
	BTNode * n4 = (BTNode*)malloc(sizeof(BTNode));
	assert(n4);
	BTNode * n5 = (BTNode*)malloc(sizeof(BTNode));
	assert(n5);
	BTNode * n6 = (BTNode*)malloc(sizeof(BTNode));
	assert(n6);
	n1->data = 1;
	n2->data = 2;
	n3->data = 3;
	n4->data = 4;
	n5->data = 5;
	n6->data = 6;

	n1->left = n2;
	n1->right = n4;
	n2->left = n3;
	n2->right = NULL;
	n3->left = NULL;
	n3->right = NULL;
	n4->left = n5;
	n4->right = n6;
	n5->left = NULL;
	n5->right = NULL;
	n6->left = NULL;
	n6->right = NULL;
	
	return n1;
}

上面构建出来的二叉树如下图所示(有点丑别建议吼)

二叉树的销毁

TreeDestroy(BTNode* root)//销毁
{
	if (root == NULL)
	{
		return;
	}
	TreeDestroy(root->left);
	TreeDestroy(root->right);
	free(root);
}

二、遍历二叉树

由于遍历二叉树的顺序不同,则打印出来的内容也不同。

前序遍历:按照 根->左子树->右子树的顺序遍历 

 preorder(BTNode* root)//前序遍历
{
	if (root == NULL)
	{
		printf("NULL");
		return ;
	}
	printf("%d ", root->data);
	preorder(root->left);
	preorder(root->right);
}

中序遍历:按照 左子树->根->右子树的顺序遍历

(如图所示一共十八个步骤)

 aforder(BTNode*root)//中序遍历
{
	if (root == NULL)
	{
		printf("NULL");//遇到空就返回
		return ;
	}
	aforder(root->left);//左子树
	printf("%d ", root->data);//根
	aforder(root->right);//右子树
}

 

后序遍历:按照 左子树->右子树->根的顺序遍历 

 

 laorder(BTNode* root)//后序遍历
{
	if (root == NULL)
	{
		printf("NULL");
		return ;
	}
	laorder(root->left);
	laorder(root->right);
	printf("%d ", root->data);
}

三、链表二叉树的部分功能

求节点(度)的个数

int TreeSize(BTNode* root)
{
	return root == NULL ? 0 :
		TreeSize(root->left) +TreeSize(root->right)+1;

}

求叶子节点的个数

int Leafsize(BTNode* root)
{
	if (root == NULL)
	
		return 0;
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}
	return Leafsize(root->left) + Leafsize(root->right);

}

求树的高度

int Treehigh(BTNode* root)
{
	if (root == NULL)
		return 0;
	int lt = Treehigh(root->left);
	int rt = Treehigh(root->right);
	return lt > rt ? lt + 1 : rt + 1;

}

求第k层的节点个数

int Ktreesize(BTNode* root, int k)//求第k层节点个数
{
	assert(k > 0);
	if (root == NULL)//为空返回0
	{
		return 0;
	}
	if (k == 1)
	{
		return 1;
	}
	return Ktreesize(root->left,k-1) +Ktreesize(root->right,k-1);
//设第一层节点(第一个节点)为第0个,那么就是左子树第一个节点就是第1个节点,则传k-1
	
}

求第k层的节点个数

BTNode*  TreeFind(BTNode* root, STDatatype X)//在二叉树找相应节点-返回该
{
	if (root == NULL)
	
		return NULL;
	
	if (root->data == X)//返回该节点地址,主函数用%p来接收
	{
		return root;
	}
	//先去左树找
	BTNode*lret= TreeFind(root->left, X);
	if (lret)
	
		return lret;
	
	//找不到再去右树找
		BTNode*rret=TreeFind(root->right, X);
		if (rret)
			return rret;

		return NULL;//最后没找到要返回空
}

层序遍历二叉树(广度优先遍历)

void TreelevelOrder(BTNode* root)//用排列构建二叉树并打印-层序遍历
{
	Queue q;
	QueueInit(&q);
	if (root)
	{
		QueuePush(&q, root);
	}
	while (!QueueEmpty(&q))
	{
	
		
		BTNode* front = QueueFront(&q);
		QueuePop(&q);

		
			printf("%d ", front->data);
		
		if(front->left)//一定要判断是不是空
		QueuePush(&q, front->left);
		if(front->right)//一定要判断是不是空
		QueuePush(&q, front->right);
	
	}
	printf("\n");
	QueueDestory(&q);
	}

判断二叉树是不是完全二叉树

isTreecomplete(BTNode* root)
{
	Queue q;
	QueueInit(&q);
	if (root)//push根结点进队列
	{
		QueuePush(&q, root);
	}
	while (!QueueEmpty(&q))//结点不为空
	{
		BTNode* front = QueueFront(&q);.//记录结点
		QueuePop(&q);//pop
		if (front == NULL)
		{
			break;// 当遇到空可以判断是不是完全二叉树
		}
			QueuePush(&q, front->left);//push左子树的结点
		
			QueuePush(&q, front->right);//push右子树的结点
	}

	//遇到空,若后面全是空则为完全二叉树
	//遇到空,若后面有非空则不为完全二叉树
	while (!QueueEmpty(&q))
	{
		BTNode* front = QueueFront(&q);
		QueuePop(&q);
		if (front != NULL)
		{
			QueueDestory(&q);
			return false;
		}
	}
	return true;
}

 

四、相关的leetcode题

二叉树的前序遍历

力扣https://leetcode.cn/problems/binary-tree-preorder-traversal/submissions/

 void preorder(struct TreeNode*root,int* a,int* pi)
 {
     if(root==NULL)
     return;

a[*pi]=root->val;
(*pi)++;
     preorder(root->left,a,pi);
     preorder(root->right,a,pi);
 }
 int Treesize(struct TreeNode*root)
 {
     if(root==NULL)
     return 0;

     return Treesize(root->left)+Treesize(root->right)+1;
 }
int* preorderTraversal(struct TreeNode* root, int* returnSize){
int n=Treesize(root);//由于该题要返回一个节点个数的值,所以先遍历一遍记录节点个数
int* a=(int*)malloc(sizeof(int)*n);//开辟一块空间(数组形式)作为接收root的值
int i=0;//由于要遍历a数组所以需要下标
preorder(root,a,&i);//前序遍历
*returnSize=n;//最后返回节点个数的值
return a;//返回数值-以前序遍历的结构返回root

}

相同的树(判断题)

力扣https://leetcode.cn/problems/same-tree/submissions/

bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p==NULL && q==NULL)//假设两颗树都为空-true
return true;
if(p==NULL|| q==NULL ||p->val!=q->val )//两颗树一棵为空一棵不为空,或者两棵树的值不相同-false
return false;

return  isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);//然后到子树判断

return true;//最后剩下的情况都为true
}

另一棵树的子树

力扣https://leetcode.cn/problems/subtree-of-another-tree/submissions/

bool isSameTree(struct TreeNode* p, struct TreeNode* q){
if(p==NULL && q==NULL)//假设两颗树都为空-true
return true;
if(p==NULL|| q==NULL ||p->val!=q->val )//两颗树一棵为空一棵不为空,或者两棵树的值不相同-false
return false;

return  isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);//然后到子树判断

return true;//最后剩下的情况都为true
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
if(root==NULL)//如果root为空-false
return false;

if(isSameTree(root,subRoot)==true)//如果现在的树的结构相同-true
return true;

return isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot);//然后让子树去比较
}

单值二叉树

力扣https://leetcode.cn/problems/univalued-binary-tree/submissions/

bool isUnivalTree(struct TreeNode* root){
if(root==NULL)//要么一开始就为空,要么遍历到最后的节点都不被return false则为true
return true;
if(root->left!=NULL && root->val!=root->left->val)//左子树节点不为空且值与根结点不相等
return false;
if(root->right!=NULL && root->val!=root->right->val)//右子树节点不为空且值与根结点不相等
return false;

return isUnivalTree(root->left) && isUnivalTree(root->right);//到左子树,右子树去遍历
}

总结

二叉树对于我来说是个不小的挑战,学完基本结构之后要去刷刷leetcode题巩固一下,如果你刚好对二叉树感到疑惑,不妨看看我上面写的内容吧!!!

如果你觉得以上内容对你有帮助的话,不妨给我个小小的👍吧!!!

  • 18
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
链表实现二叉树是一种常见的数据结构,可以用于存储和操作树形结构的数据。下面是C语言链表实现二叉树的方法: 定义结构体: ```c typedef struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; } TreeNode; ``` 初始化: ```c TreeNode *root = NULL; ``` 先序遍历创建二叉树方法一: ```c void createTree(TreeNode **node) { int data; scanf("%d", &data); if (data == -1) { *node = NULL; } else { *node = (TreeNode *)malloc(sizeof(TreeNode)); (*node)->data = data; createTree(&((*node)->left)); createTree(&((*node)->right))); } } ``` 测试: ```c int main() { createTree(&root); return 0; } ``` 方法二:根据所给的字符串序列创建 ```c void createTree(TreeNode **node, char *str, int *index) { if (str[*index] == '\0' || str[*index] == '#') { *node = NULL; (*index)++; } else { *node = (TreeNode *)malloc(sizeof(TreeNode)); (*node)->data = str[*index] - '0'; (*index)++; createTree(&((*node)->left), str, index); createTree(&((*node)->right), str, index); } } ``` 其他方法: - 树的结点大小 ```c int size(TreeNode *node) { if (node == NULL) { return 0; } else { return size(node->left) + 1 + size(node->right); } } ``` - 树的高 ```c int height(TreeNode *node) { if (node == NULL) { return 0; } else { int leftHeight = height(node->left); int rightHeight = height(node->right); return (leftHeight > rightHeight) ? (leftHeight + 1) : (rightHeight + 1); } } ``` - 判断树是否为空 ```c int isEmpty(TreeNode *node) { return node == NULL; } ``` - 找某个结点的左孩子和右孩子 ```c TreeNode *leftChild(TreeNode *node) { if (node == NULL) { return NULL; } else { return node->left; } } TreeNode *rightChild(TreeNode *node) { if (node == NULL) { return NULL; } else { return node->right; } } ``` - 找某个结点的父节点 ```c TreeNode *parent(TreeNode *root, TreeNode *node) { if (root == NULL || root == node) { return NULL; } else if (root->left == node || root->right == node) { return root; } else { TreeNode *p = parent(root->left, node); if (p != NULL) { return p; } else { return parent(root->right, node); } } } ``` - 查找元素 ```c TreeNode *find(TreeNode *node, int data) { if (node == NULL) { return NULL; } else if (node->data == data) { return node; } else { TreeNode *p = find(node->left, data); if (p != NULL) { return p; } else { return find(node->right, data); } } } ``` - 拷贝树 ```c TreeNode *copy(TreeNode *node) { if (node == NULL) { return NULL; } else { TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode)); newNode->data = node->data; newNode->left = copy(node->left); newNode->right = copy(node->right); return newNode; } } ``` - 清空树 ```c void clear(TreeNode **node) { if (*node != NULL) { clear(&((*node)->left)); clear(&((*node)->right)); free(*node); *node = NULL; } } ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值