【数据结构】遍历二叉树(递归思想)-->赋源码

简介: 【数据结构】遍历二叉树(递归思想)-->赋源码

前言

二叉树遍历是指按照一定的顺序访问二叉树中的每个节点,使得每个节点恰好被访问一次。遍历是二叉树上最重要的运算之一,是二叉树上进行其他运算的基础。

一、二叉树遍历概念

二叉树遍历分类

  • 前序遍历 :根节点–>左子树–>右子树。在前序遍历时,首先访问根节点,然后依次访问左子树和右子树。
  • 中序遍历 : 左子树–>根节点–>右子树。在中序遍历时,首先访问左子树,然后访问根节点,最后访问右子树。
  • 后序遍历:左子树–>右子树–>根节点。在后序遍历时,首先访问左子树和右子树,然后访问根节点。
  • 层序遍历: 由顶层到底层,一层一层遍历。

二叉树其他操作

树节点的个数树深度树 k 层的个数查找节点

二、二叉树遍历实现

我们以下面为例子:

image.png

2.1 二叉树建立

1.定义一个结构体,分别有左右两个指针。

2.为每一个节点创建孔家。

3.创建二叉树,并如上图连接。

//定义结构体

typedef int BTTypeData;

typedef struct BinaryTree
{
  BTTypeData data;

  struct BinaryTree* left;
  struct BinaryTree* right;
}BinaryTree;

//创建空间

BinaryTree* BuyBinaryTree(BTTypeData x)
{
  BinaryTree* node = (BinaryTree*)malloc(sizeof(BinaryTree));
  if (node == NULL)
  {
    perror("malloc fail");
    return NULL;
  }

  node->data = x;
  node->left = NULL;
  node->right = NULL;

  return node;
}

//建树

BinaryTree* CreateBinaryTree()
{

  BinaryTree* node1 = BuyBinaryTree(1);
  BinaryTree* node2 = BuyBinaryTree(2);
  BinaryTree* node3 = BuyBinaryTree(3);
  BinaryTree* node4 = BuyBinaryTree(4);
  BinaryTree* node5 = BuyBinaryTree(5);
  BinaryTree* node6 = BuyBinaryTree(6);
  BinaryTree* node7 = BuyBinaryTree(7);
  BinaryTree* node8 = BuyBinaryTree(8);
  BinaryTree* node9 = BuyBinaryTree(9);

  node1->left = node2;
  node1->right = node4;
  node2->left = node3;
  node4->left = node5;
  node4->right = node6;
  node3->right = node7;
  node6->left = node8;
  node6->right = node9;


  return node1;
}

2.2 前序遍历

在递归实现中,前序遍历的基本思想是对于每一个节点,先访问该节点,然后对其左子树进行前序遍历,最后对其右子树进行前序遍历。如果当前节点为空,则直接返回。这种方法的优点是代码简洁明了,易于理解,但缺点是可能导致栈溢出,特别是在处理深度较大的二叉树时。

遍历结果:1–> 2–> 3 –>7 –>4 –>5 –>6–> 8 –>9

//前序遍历
void PreOrder(BinaryTree* root)
{
  
  
  if (root == NULL)
  {
    //printf("NULL ");
    return;
  }

  printf("%d ", root->data);
  PreOrder(root->left);
  PreOrder(root->right);
}

2.2 中序遍历

首先对左子树进行中序遍历,然后访问根节点,最后对右子树进行中序遍历。

遍历结果:3 –>7–>2–> 1–> 5–> 4–>8–> 6–> 9

void InOrder(BinaryTree* root)
{


  if (root == NULL)
  {
    //printf("NULL ");
    return;
  }

  InOrder(root->left);
  printf("%d ", root->data);
  InOrder(root->right);

}

2.3 后序遍历

递归函数首先访问左子树,然后访问右子树,最后访问根节点。如果当前节点为空,则直接返回。

遍历结果:7–> 3–> 2–> 5–> 8 –>9 –>6 –>4 –>1

//后序遍历
void PostOrder(BinaryTree* root)
{


  if (root == NULL)
  {
    //printf("NULL ");
    return;
  }

  PostOrder(root->left);
  PostOrder(root->right);
  printf("%d ", root->data);

}

2.4 层序遍历

在二叉树的层序遍历是指按照树的层次顺序,从上到下、从左到右逐层访问二叉树的节点。这种遍历方式可以帮助我们了解二叉树的结构布局,特别是在处理树状数据结构时非常有用。

利用队列的特点,有关队列可参考 栈和队列

  • 将根节点入队。
  • 当队列不为空时,从队列中取出一个节点,访问该节点。
  • 将该节点的左右子节点(如果存在)入队。
  • 重复步骤2和3,直到队列为空。

遍历结果:1–> 2 –>4 –>3 –>5 –>6 –>7 –>8 –>9

//层序遍历

void LevelOrder(BinaryTree* root)
{
  if (root == NULL)
  {
    printf("NULL ");
    return;
  }
  Queue TBT;
  QueueInit(&TBT);

  if (root)
    QueuePush(&TBT, root);
  
  while (!QueueEmpty(&TBT))
  {
    BinaryTree* front = QueueTop(&TBT);
    QueuePop(&TBT);
    printf("%d ", front->data);

    if (front->left)
      QueuePush(&TBT, front->left);
    if (front->right)
      QueuePush(&TBT, front->right);

  }

  QueueDestroy(&TBT);

}

2.5 二叉树的节点个数

利用递归的方法,左右子树调用,如果该节点为NULL 便会返回0,否则返回1。

//树的结点个数

int TreeSize(BinaryTree* root)
{

  return root == 0 ? 0:TreeSize(root->left) + TreeSize(root->right) + 1;

}

2.6 二叉树的深度

利用 leftright记录左右子树的个数,然后比较 选择较大的一个。

如图:

//树的高度

int TreeHeight(BinaryTree* root)
{
  if (root == NULL)
  {
    return 0;
  }

  int left = TreeHeight(root->left) ;
  int right = TreeHeight(root->right);

  return left > right ? left + 1 : right + 1;

}

2.7 二叉树第K层的个数

假设查找第三层,K为3 ,每次递归K–,知道K== 1 的时候 返回1。

//层的个数

int TreeKLevel(BinaryTree* root, int k)
{
  if (root == NULL)
  {
    return 0;
  }

  if (k == 1)
  {
    return 1;
  }


  return TreeKLevel(root->left, k - 1) + TreeKLevel(root->right, k - 1);


}

2.8 二叉树查找结点

节点的查找,如果节点为NULL饭后NULL,如果此节点的data等于x,返回节点的地址。

//查找节点

BinaryTree* TreeFind(BinaryTree* root, BTTypeData x)
{

  if (root == NULL)
  {
    return NULL;
  }

  if (root->data == x)
  {
    return root;
  }

  BinaryTree* lret = TreeFind(root->left, 7);
  if (lret)
    return lret;
  BinaryTree* rret = TreeFind(root->right, 7);
  if (rret)
    return rret;
  return NULL;
}

源码

queue.c

#define _CRT_SECURE_NO_WARNINGS

#include "queue.h"



//初始化
void QueueInit(Queue* ps)
{
  assert(ps);

  ps->head = ps->tail = NULL;

  ps->szie = 0;

}

//销毁
void QueueDestroy(Queue* ps)
{
  assert(ps);
  QNode* cur = ps->head;
  while (cur)
  {
    QNode* next = cur->next;
    free(cur);
    cur = next;
  }

  ps->head = ps->tail = NULL;
  ps->szie = 0;
}

//入队
void QueuePush(Queue* ps,QDataType x)
{
  assert(ps);

  QNode* newcode = (QNode*)malloc(sizeof(QNode));
  if (newcode == NULL)
  {
    perror("malloc fail");
    return ;
  }
  newcode->next = NULL;
  newcode->data = x;

  if (ps->head == NULL)
  {
    ps->head = ps->tail = newcode;
    
  }
  else

  {
    
    ps->tail->next = newcode;
    ps->tail = newcode;
  }

  ps->szie++;

}

//删除
void QueuePop(Queue* ps)
{
  assert(ps);
  assert(ps->head != NULL);
  assert(!QueueEmpty(ps));

  if (ps->head->next == NULL)
  {
    free(ps->head);
    ps->head = ps->tail = NULL;
  }
  else
  {
    QNode* next = ps->head->next;
    free(ps->head);
    ps->head = next;

  }
  ps->szie--;
}

//大小
int QueueSize(Queue* ps)
{
  assert(ps);

  return ps->szie;
}

//判空队
bool QueueEmpty(Queue* ps)
{
  assert(ps);

  return ps->szie == 0;
}

//出队头
QDataType QueueTop(Queue* ps)
{
  assert(ps);
  assert(!QueueEmpty(ps));

  return ps->head->data;
}

//出队尾
QDataType QueueBack(Queue* ps)
{
  assert(ps);

  return ps->tail->data;
}



queue.h

#pragma once

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

typedef struct BinaryTree* QDataType;

typedef struct QNode
{
  struct QNode* next;
  QDataType data;

}QNode;

typedef struct Queue
{
  QNode*head;
  QNode*tail;
   
  int szie;
}Queue;


//单链表的实现,FIFO

//初始化
void QueueInit(Queue* ps);
//销毁
void QueueDestroy(Queue* ps);
//入队
void QueuePush(Queue* ps, QDataType x);
//删除
void QueuePop(Queue* ps);
//大小
int QueueSize(Queue* ps);
//判空队
bool QueueEmpty(Queue* ps);
//出队头
QDataType QueueTop(Queue* ps);
//出队尾
QDataType QueueBack(Queue* ps);

travelling_binary_tree

#define _CRT_SECURE_NO_WARNINGS

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

#include "queue.h"

//定义结构体

typedef int BTTypeData;

typedef struct BinaryTree
{
  BTTypeData data;

  struct BinaryTree* left;
  struct BinaryTree* right;
}BinaryTree;

//创建空间

BinaryTree* BuyBinaryTree(BTTypeData x)
{
  BinaryTree* node = (BinaryTree*)malloc(sizeof(BinaryTree));
  if (node == NULL)
  {
    perror("malloc fail");
    return NULL;
  }

  node->data = x;
  node->left = NULL;
  node->right = NULL;

  return node;
}

//建树

BinaryTree* CreateBinaryTree()
{

  BinaryTree* node1 = BuyBinaryTree(1);
  BinaryTree* node2 = BuyBinaryTree(2);
  BinaryTree* node3 = BuyBinaryTree(3);
  BinaryTree* node4 = BuyBinaryTree(4);
  BinaryTree* node5 = BuyBinaryTree(5);
  BinaryTree* node6 = BuyBinaryTree(6);
  BinaryTree* node7 = BuyBinaryTree(7);
  BinaryTree* node8 = BuyBinaryTree(8);
  BinaryTree* node9 = BuyBinaryTree(9);

  node1->left = node2;
  node1->right = node4;
  node2->left = node3;
  node4->left = node5;
  node4->right = node6;
  node3->right = node7;
  node6->left = node8;
  node6->right = node9;


  return node1;
}

//前序遍历
void PreOrder(BinaryTree* root)
{
  
  
  if (root == NULL)
  {
    //printf("NULL ");
    return;
  }

  printf("%d ", root->data);
  PreOrder(root->left);
  PreOrder(root->right);
}

//中序遍历

void InOrder(BinaryTree* root)
{


  if (root == NULL)
  {
    //printf("NULL ");
    return;
  }

  InOrder(root->left);
  printf("%d ", root->data);
  InOrder(root->right);

}

//后序遍历
void PostOrder(BinaryTree* root)
{


  if (root == NULL)
  {
    //printf("NULL ");
    return;
  }

  PostOrder(root->left);
  PostOrder(root->right);
  printf("%d ", root->data);

}

//层序遍历

void LevelOrder(BinaryTree* root)
{
  if (root == NULL)
  {
    printf("NULL ");
    return;
  }
  Queue TBT;
  QueueInit(&TBT);

  if (root)
    QueuePush(&TBT, root);
  
  while (!QueueEmpty(&TBT))
  {
    BinaryTree* front = QueueTop(&TBT);
    QueuePop(&TBT);
    printf("%d ", front->data);

    if (front->left)
      QueuePush(&TBT, front->left);
    if (front->right)
      QueuePush(&TBT, front->right);

  }

  QueueDestroy(&TBT);

}

//树的结点个数

int TreeSize(BinaryTree* root)
{

  return root == 0 ? 0:TreeSize(root->left) + TreeSize(root->right) + 1;

}

//树的高度

int TreeHeight(BinaryTree* root)
{
  if (root == NULL)
  {
    return 0;
  }

  int left = TreeHeight(root->left) ;
  int right = TreeHeight(root->right);

  return left > right ? left + 1 : right + 1;

}

//层的个数

int TreeKLevel(BinaryTree* root, int k)
{
  if (root == NULL)
  {
    return 0;
  }

  if (k == 1)
  {
    return 1;
  }


  return TreeKLevel(root->left, k - 1) + TreeKLevel(root->right, k - 1);


}

//查找节点

BinaryTree* TreeFind(BinaryTree* root, BTTypeData x)
{

  if (root == NULL)
  {
    return NULL;
  }

  if (root->data == x)
  {
    return root;
  }

  BinaryTree* lret = TreeFind(root->left, 7);
  if (lret)
    return lret;
  BinaryTree* rret = TreeFind(root->right, 7);
  if (rret)
    return rret;
  return NULL;
}

int main()
{
  BinaryTree* root = CreateBinaryTree();


  PreOrder(root);
  printf("\n");
  InOrder(root);
  printf("\n");
  PostOrder(root);
  printf("\n");
  LevelOrder(root);
  printf("\n");


  printf("TreeSize : %d\n", TreeSize(root));

  printf("TreeHeight : %d\n", TreeHeight(root));

  printf("TreeKLevel : %d\n", TreeKLevel(root, 3));

  printf("TreeFind : %p\n", TreeFind(root, 1));




  return 0; 
}

}

//查找节点

BinaryTree* TreeFind(BinaryTree* root, BTTypeData x)

{

if (root == NULL)
{
  return NULL;
}

if (root->data == x)
{
  return root;
}

BinaryTree* lret = TreeFind(root->left, 7);
if (lret)
  return lret;
BinaryTree* rret = TreeFind(root->right, 7);
if (rret)
  return rret;
return NULL;

}

int main()

{

BinaryTree* root = CreateBinaryTree();

PreOrder(root);
printf("\n");
InOrder(root);
printf("\n");
PostOrder(root);
printf("\n");
LevelOrder(root);
printf("\n");


printf("TreeSize : %d\n", TreeSize(root));

printf("TreeHeight : %d\n", TreeHeight(root));

printf("TreeKLevel : %d\n", TreeKLevel(root, 3));

printf("TreeFind : %p\n", TreeFind(root, 1));




return 0; 


目录
打赏
0
4
4
0
36
分享
相关文章
Java 集合面试题从数据结构到 HashMap 源码剖析详解及长尾考点梳理
本文深入解析Java集合框架,涵盖基础概念、常见集合类型及HashMap的底层数据结构与源码实现。从Collection、Map到Iterator接口,逐一剖析其特性与应用场景。重点解读HashMap在JDK1.7与1.8中的数据结构演变,包括数组+链表+红黑树优化,以及put方法和扩容机制的实现细节。结合订单管理与用户权限管理等实际案例,展示集合框架的应用价值,助你全面掌握相关知识,轻松应对面试与开发需求。
109 3
|
4月前
|
算法系列之数据结构-二叉树
树是一种重要的非线性数据结构,广泛应用于各种算法和应用中。本文介绍了树的基本概念、常见类型(如二叉树、满二叉树、完全二叉树、平衡二叉树、B树等)及其在Java中的实现。通过递归方法实现了二叉树的前序、中序、后序和层次遍历,并展示了具体的代码示例和运行结果。掌握树结构有助于提高编程能力,优化算法设计。
101 9
 算法系列之数据结构-二叉树
【数据结构进阶】AVL树深度剖析 + 实现(附源码)
在深入探讨了AVL树的原理和实现后,我们不难发现,这种数据结构不仅优雅地解决了传统二叉搜索树可能面临的性能退化问题,还通过其独特的平衡机制,确保了在任何情况下都能提供稳定且高效的查找、插入和删除操作。
287 19
|
4月前
|
【数据结构进阶】红黑树超详解 + 实现(附源码)
本文深入探讨了红黑树的实现原理与特性。红黑树是一种自平衡二叉搜索树,通过节点着色(红/黑)和特定规则,确保树的高度接近平衡,从而实现高效的插入、删除和查找操作。相比AVL树,红黑树允许一定程度的不平衡,减少了旋转调整次数,提升了动态操作性能。文章详细解析了红黑树的性质、插入时的平衡调整(变色与旋转)、查找逻辑以及合法性检查,并提供了完整的C++代码实现。红黑树在操作系统和数据库中广泛应用,其设计兼顾效率与复杂性的平衡。
500 3
|
6月前
|
【C++数据结构——树】二叉树的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现二叉树的基本运算。​ 相关知识 创建二叉树 销毁二叉树 查找结点 求二叉树的高度 输出二叉树 //二叉树节点结构体定义 structTreeNode{ intval; TreeNode*left; TreeNode*right; TreeNode(intx):val(x),left(NULL),right(NULL){} }; 创建二叉树 //创建二叉树函数(简单示例,手动构建) TreeNode*create
156 12
|
6月前
|
C++
【C++数据结构——树】二叉树的性质(头歌实践教学平台习题)【合集】
本文档介绍了如何根据二叉树的括号表示串创建二叉树,并计算其结点个数、叶子结点个数、某结点的层次和二叉树的宽度。主要内容包括: 1. **定义二叉树节点结构体**:定义了包含节点值、左子节点指针和右子节点指针的结构体。 2. **实现构建二叉树的函数**:通过解析括号表示串,递归地构建二叉树的各个节点及其子树。 3. **使用示例**:展示了如何调用 `buildTree` 函数构建二叉树并进行简单验证。 4. **计算二叉树属性**: - 计算二叉树节点个数。 - 计算二叉树叶子节点个数。 - 计算某节点的层次。 - 计算二叉树的宽度。 最后,提供了测试说明及通关代
141 10
【C++数据结构——树】二叉树的遍历算法(头歌教学实验平台习题) 【合集】
本任务旨在实现二叉树的遍历,包括先序、中序、后序和层次遍历。首先介绍了二叉树的基本概念与结构定义,并通过C++代码示例展示了如何定义二叉树节点及构建二叉树。接着详细讲解了四种遍历方法的递归实现逻辑,以及层次遍历中队列的应用。最后提供了测试用例和预期输出,确保代码正确性。通过这些内容,帮助读者理解并掌握二叉树遍历的核心思想与实现技巧。
207 2
【C++数据结构——图】图的遍历(头歌教学实验平台习题) 【合集】
本文介绍了图的遍历算法,包括深度优先遍历(DFS)和广度优先遍历(BFS)。深度优先遍历通过递归方式从起始节点深入探索图,适用于寻找路径、拓扑排序等场景;广度优先遍历则按层次逐层访问节点,适合无权图最短路径和网络爬虫等应用。文中提供了C++代码示例,演示了如何实现这两种遍历方法,并附有测试用例及结果,帮助读者理解和实践图的遍历算法。
261 0
|
8月前
|
非递归实现后序遍历时,如何避免栈溢出?
后序遍历的递归实现和非递归实现各有优缺点,在实际应用中需要根据具体的问题需求、二叉树的特点以及性能和空间的限制等因素来选择合适的实现方式。
184 58
栈区的非法访问导致的死循环(x64)
这段内容主要分析了一段C语言代码在VS2022中形成死循环的原因,涉及栈区内存布局和数组越界问题。代码中`arr[15]`越界访问,修改了变量`i`的值,导致`for`循环条件始终为真,形成死循环。原因是VS2022栈区从低地址到高地址分配内存,`arr`数组与`i`相邻,`arr[15]`恰好覆盖`i`的地址。而在VS2019中,栈区先分配高地址再分配低地址,因此相同代码表现不同。这说明编译器对栈区内存分配顺序的实现差异会导致程序行为不一致,需避免数组越界以确保代码健壮性。
21 0
栈区的非法访问导致的死循环(x64)
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问