【数据结构】二叉树

简介: 【数据结构】二叉树

1.二叉树的遍历

前序,中序,后序遍历

学习二叉树结构,最简单的方式就是遍历。所谓二叉树遍历(Traversal)是按照某种特定的规则,依次对二叉树中的节点进行相应的操作,并且每个节点只操作一次。访问结点所做的操作依赖于具体的应用问题。 遍历是二叉树上最重要的运算之一,也是二叉树上进行其它运算的基础。

  1. 前序遍历——访问根结点的操作发生在遍历其左右子树之前。
  2. 中序遍历——访问根结点的操作发生在遍历其左右子树之中(间)。
  3. 后序遍历——访问根结点的操作发生在遍历其左右子树之后。

1.前序遍历:根,左子树,右子树

2.中序遍历:左子树,根,右子树

3.后序遍历:左子树,右子树,根.

前序遍历访问数据为1,2, 3,NULL(3的左子树),NULL(3的右子树),NULL(2的右子树),4,5,NULL(5的左数),NULL(5的右树),6,NULL(6的左树),NULL(6的右树).

中序遍历访问数据为NULL(3的左子树),3,NULL(3的右子树),2,NULL(2的右子树),1,NULL(5的左子树),5,NULL(5的右子树),4,NULL(6的左子树),6,NULL(6的右子树).

后序遍历访问数据为NULL(3的左子树),NULL(3的右子树),3,NULL(2的右子树),2,NULL(5的左子树),NULL(5的右子树),5,NULL(6的左子树),NULL(6的右子树),6,4,1 .

前序遍历的实现

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

前序递归打印数据

二叉树中序遍历

// 二叉树中序遍历
void InOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  
  PreOrder(root->left);
  printf("%d  ", root->data);
  PreOrder(root->right);
}

二叉树后序遍历

void PostOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  PostOrder(root->left);
  
  PostOrder(root->right);
  printf("%d  ", root->data);
}

代码实现

include<stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct BinaryTreeNode
{
  int data;
  struct BinaryTreeNode* left;
  struct BinaryTreeNode* right;
}BTNode;
BTNode* BuyNode(int x)
{
  BTNode* node = (BTNode*)malloc(sizeof(BTNode));
  if (node == NULL)
  {
    perror("malloc fail");
    return NULL;
  }
  node->data = x;
  node->left = NULL;
  node->right = NULL;
  return node;
}
BTNode* CreatBinaryTree()
{
  BTNode* node1 = BuyNode(1);
  BTNode* node2 = BuyNode(2);
  BTNode* node3 = BuyNode(3);
    BTNode * node4 = BuyNode(4);
  BTNode* node5 = BuyNode(5);
  BTNode* node6 = BuyNode(6);
  node1->left = node2;
  node1->right = node4;
  node2->left = node3;
  node4->left = node5;
  node4->right = node6;
  return node1;
}
// 二叉树前序遍历
void PreOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  printf("%d  ",root->data);
  PreOrder(root->left);
  PreOrder(root->right);
}
// 二叉树中序遍历
void InOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  
  InOrder(root->left);
  printf("%d  ", root->data);
  InOrder(root->right);
}
// 二叉树后序遍历
void PostOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  PostOrder(root->left);
  
  PostOrder(root->right);
  printf("%d  ", root->data);
}
int main()
{
  BTNode* bk=CreatBinaryTree();
  PreOrder(bk);
  printf("\n");
  InOrder(bk);
  printf("\n");
  PostOrder(bk);
  printf("\n");
}

编译运行

二叉树结点的个数

int size = 0;//要统计每次递归的次数也就是二叉树结点的个数,定义 全局变量
// 二叉树节点个数
void BinaryTreeSize(BTNode* root)
{
  if (root == NULL)
  {
    return;
  }
  
    ++size;
  
  BinaryTreeSize(root->left);
  BinaryTreeSize(root->right);
}

求二叉树结点的个数的另一个方法,采用分治的思想。

统计学校的人数,院长问导员,导员问老师,老师问班长,班长统计各班的人数。如果遇到空,返回0,其他情况都返回左子树的人数加右子树的人数加上自己,比如老师说要向导员报告人数,老师必须向导员汇报老师左子树的学生,以及右子树的学生加上自己。

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

代码简化

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

二叉树叶结点的个数

叶结点的判断,如果该结点的左节点和右节点都为空的话,这个结点就是叶子结点,并且将该值返回

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

二叉树第k层结点个数

二叉树第k层结点个数

子问题:转换成左子树的第k-1层和右子树的第k-1层

结束条件:k==1且结点不为空

// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
  assert(k >= 1);
    if (root == NULL)
    {
      return 0;
    }
    if (k == 1)
    {
      return 1;
    }
    return  BinaryTreeLevelKSize(root->left,k-1)+ BinaryTreeLevelKSize(root->right,k -1);
}

二叉树的深度

返回左右子树中高的+1;

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

方法2:

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

比较方法1和方法2,方法一效率要慢一些,因为方法一比较完左右子树返回的值后(这里算递归了一次)还必须再次递归左子树或者右子树,递归左右子树中大的那一个,时间上会翻倍。

整体代码实现

#include<stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct BinaryTreeNode
{
  int data;
  struct BinaryTreeNode* left;
  struct BinaryTreeNode* right;
}BTNode;
BTNode* BuyNode(int x)
{
  BTNode* node = (BTNode*)malloc(sizeof(BTNode));
  if (node == NULL)
  {
    perror("malloc fail");
    return NULL;
  }
  node->data = x;
  node->left = NULL;
  node->right = NULL;
  return node;
}
BTNode* CreatBinaryTree()
{
  BTNode* node1 = BuyNode(1);
  BTNode* node2 = BuyNode(2);
  BTNode* node3 = BuyNode(3);
    BTNode * node4 = BuyNode(4);
  BTNode* node5 = BuyNode(5);
  BTNode* node6 = BuyNode(6);
  node1->left = node2;
  node1->right = node4;
  node2->left = node3;
  node4->left = node5;
  node4->right = node6;
  return node1;
}
// 二叉树前序遍历
void PreOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  printf("%d  ",root->data);
  PreOrder(root->left);
  PreOrder(root->right);
}
// 二叉树中序遍历
void InOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  
  InOrder(root->left);
  printf("%d  ", root->data);
  InOrder(root->right);
}
// 二叉树后序遍历
void PostOrder(BTNode* root)
{
  if (root == NULL)
  {
    printf("NULL  ");
    return;
  }
  PostOrder(root->left);
  
  PostOrder(root->right);
  printf("%d  ", root->data);
}
int size = 0;
// 二叉树节点个数
//void BinaryTreeSize(BTNode* root)
//{
//
//  if (root == NULL)
//  {
//    return;
//
//
//  }
//  
//    ++size;
//  
//  BinaryTreeSize(root->left);
//  BinaryTreeSize(root->right);
//
//}
//if (root == NULL)
//{
//  return 0;
//
//
//}
//
//
//
//return BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
int BinaryTreeSize(BTNode* root)
{
return root == 0 ? NULL : BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1;
  
}
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root)
{
  if (root == NULL)
  {
    return 0;
  }
  else if (root->left == NULL && root->right == NULL)
  {
    return 1;
  }
  
  return BinaryTreeLeafSize(root->left) + BinaryTreeLeafSize(root->right);
}
// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k)
{
  assert(k >= 1);
    if (root == NULL)
    {
      return 0;
    }
    if (k == 1)
    {
      return 1;
    }
    return  BinaryTreeLevelKSize(root->left,k-1)+ BinaryTreeLevelKSize(root->right,k -1);
}
//求二叉树的高度
int BTreeHeight(BTNode* root)
{
  if (root == NULL)
  {
    return 0;
  }
  int left = BTreeHeight(root->left);
  int right = BTreeHeight(root->right);
  return left > right ? left + 1 : right + 1;
}
int main()
{
  BTNode* bk=CreatBinaryTree();
  BinaryTreeSize(bk);
  PreOrder(bk);
  printf("\n");
  InOrder(bk);
  printf("\n");
  PostOrder(bk);
  printf("\n");
  printf("二叉树结点个数%d \n", BinaryTreeSize(bk));
  printf("二叉树叶子结点个数%d \n",BinaryTreeLeafSize(bk));
  printf("二叉树第k层结点数%d ", BinaryTreeLevelKSize(bk,3));
  printf("二叉树深度%d ", BTreeHeight(bk));
}

编译运行

目录
相关文章
|
1天前
【数据结构】二叉树(遍历,递归)
【数据结构】二叉树(遍历,递归
16 2
|
1天前
|
数据可视化
数据结构——lesson8二叉树的实现
本文介绍了二叉树的基本操作和实现,包括二叉树的构建、销毁、节点个数计算、叶子节点个数、第k层节点个数、查找、高度计算以及判断是否为完全二叉树的方法。通过递归和层序遍历等技巧,详细阐述了这些操作的原理和代码实现。文章以实例和图解帮助读者理解二叉树的各种特性和操作。
|
1天前
|
算法 编译器 C语言
数据结构——二叉树四种遍历的实现-3
数据结构——二叉树四种遍历的实现
数据结构——二叉树四种遍历的实现-3
|
1天前
|
存储
数据结构——二叉树四种遍历的实现-2
数据结构——二叉树四种遍历的实现
数据结构——二叉树四种遍历的实现-2
|
1天前
|
机器学习/深度学习
数据结构——二叉树四种遍历的实现-1
数据结构——二叉树四种遍历的实现
数据结构——二叉树四种遍历的实现-1
|
1天前
【数据结构】二叉树的三种遍历(非递归讲解)
【数据结构】二叉树的三种遍历(非递归讲解)
9 1
|
1天前
|
存储
【数据结构】二叉树相关oj题(一)
【数据结构】二叉树相关oj题(一)
10 1
|
1天前
|
存储 分布式数据库
[数据结构]~二叉树
[数据结构]~二叉树
|
1天前
|
C语言
【C语言/数据结构】二叉树(层序遍历|判断完全二叉树|性质)
【C语言/数据结构】二叉树(层序遍历|判断完全二叉树|性质)
280 52
|
1天前
【数据结构】二叉树-堆(top-k问题,堆排序,时间复杂度)
【数据结构】二叉树-堆(top-k问题,堆排序,时间复杂度)
17 4