举例讲解C语言程序中对二叉树数据结构的各种遍历方式

所属分类: 软件编程 / C 语言 阅读数: 65
收藏 0 赞 0 分享

二叉树遍历的基本思想

二叉树的遍历本质上其实就是入栈出栈的问题,递归算法简单且容易理解,但是效率始终是个问题。非递归算法可以清楚的知道每步实现的细节,但是乍一看不想递归算法那么好理解,各有各的好处吧。接下来根据下图讲讲树的遍历。

201649152648498.jpg (456×317)

1、先序遍历:先序遍历是先输出根节点,再输出左子树,最后输出右子树。上图的先序遍历结果就是:ABCDEF

 2、中序遍历:中序遍历是先输出左子树,再输出根节点,最后输出右子树。上图的中序遍历结果就是:CBDAEF

3、后序遍历:后序遍历是先输出左子树,再输出右子树,最后输出根节点。上图的后序遍历结果就是:CDBFEA

其中,后序遍历的非递归算法是最复杂的,我用了一个标识符isOut来表明是否需要弹出打印。因为只有当节点的左右子树都打印后该节点 才能弹出栈打印,所以标识isOut为1时打印,isOut初始值为0,这主要是为了处理非叶子节点。由后序遍历的原理决定,左右子树都被打印该节点才能打印,所以该节点肯定会被访问2次,第一次的时候不要打印,第二次打印完右子树的时候打印。叶子节点打印完后将isOut置为1。(纯粹是自己想的,应该还有逻辑更简单的算法)
        
实例       
构造和遍历

#include <stdio.h> 
#include <stdlib.h> 
 
typedef struct _NODE//节点结构 
{ 
  struct _NODE* leftChild; 
  int value; 
  struct _NODE* rightChild; 
} NODE, *PNODE; 
 
PNODE createNode(int value){//创建一个新节点 
  PNODE n = (PNODE)malloc(sizeof(NODE)); 
  n->value = value; 
  n->leftChild = NULL; 
  n->rightChild = NULL; 
  return n; 
} 
 
PNODE insertLeftChild(PNODE parent, int value){//在指定节点上插入左节点 
  return (parent->leftChild = createNode(value)); 
} 
 
PNODE insertRightChild(PNODE parent, int value){//在指定节点上插入左节点 
  return (parent->rightChild = createNode(value)); 
} 
 
void createBTree(PNODE root, int i){//向树中插入一些元素    
  if (i == 0)                              
  {                             
    return;                         
  }                             
  else{ 
    PNODE l = insertLeftChild(root, i * 10 + 1); 
    PNODE r = insertRightChild(root, i * 10 + 2); 
    createBTree(l, --i); 
    createBTree(r, i); 
  } 
} 
 
void printDLR(PNODE root){//先序遍历:对每一刻子树都是根->左->右的顺序 
  if (root == NULL) 
  { 
    return; 
  } 
  printf("%-4d", root->value); 
  printDLR(root->leftChild); 
  printDLR(root->rightChild); 
} 
 
void printLDR(PNODE root){//中序遍历: 
  if (root == NULL) 
  { 
    return; 
  } 
  printLDR(root->leftChild); 
  printf("%-4d", root->value); 
  printLDR(root->rightChild); 
} 
 
void printLRD(PNODE root){//后序遍历 
  if (root == NULL) 
  { 
    return; 
  } 
  printLRD(root->leftChild); 
  printLRD(root->rightChild); 
  printf("%-4d", root->value); 
} 
 
void main(){ 
  PNODE root = createNode(0);//创建根节点 
  createBTree(root, 3); 
   
  printf("先序遍历: "); 
  printDLR(root);//遍历 
  printf("\n中序遍历: "); 
   
  printLDR(root); 
  printf("\n后序遍历: "); 
   
  printLRD(root); 
  printf("\n"); 
} 

201649152221356.jpg (546×169)

执行结果:

201649152333006.jpg (570×119)

先序遍历:

201649152351080.jpg (546×169)

中序遍历:

201649152406969.jpg (546×169)

后序遍历:

201649152423441.jpg (546×169)

C++中可以使用类模板,从而使节点值的类型可以不止限定在整型:

#include <iostream.h> 
 
template <class T> class Node//节点类模板 
{ 
public: 
  Node(T value):value(value)//构造方法 
  { 
    leftChild = 0;  
    rightChild = 0; 
  } 
  Node* insertLeftChild(T value);//插入左孩子,返回新节点指针 
  Node* insertRightChild(T vallue);//插入右孩子 
  void deleteLeftChild();//删左孩子 
  void deleteRightChild();//删右孩子 
  void showDLR();//先序遍历 
  void showLDR();//中序遍历 
  void showLRD();//后序遍历 
protected: 
  T value;//节点值 
  Node* leftChild;//左孩子指针 
  Node* rightChild;//右孩子指针 
private: 
}; 
 
template <class T> Node<T>* Node<T>::insertLeftChild(T value){//插入左孩子 
  return (this->leftChild = new Node(value)); 
} 
 
template <class T> Node<T>* Node<T>::insertRightChild(T value){//插入右孩子 
  return (this->rightChild = new Node(value)); 
} 
 
template <class T> void Node<T>::deleteLeftChild(){//删除左孩子 
  delete this->leftChild; 
  this->leftChild = 0; 
} 
 
template <class T> void Node<T>::deleteRightChild(){//删除右孩子 
  delete this->rightChild; 
  this->rightChild = 0; 
} 
 
template <class T> void Node<T>::showDLR(){//先序遍历 
  cout<<this->value<<" "; 
  if (leftChild) 
  { 
    leftChild->showDLR(); 
  } 
  if (rightChild) 
  { 
    rightChild->showDLR(); 
  } 
} 
 
template <class T> void Node<T>::showLDR(){//中序遍历 
  if (leftChild) 
  { 
    leftChild->showLDR(); 
  } 
  cout<<this->value<<" "; 
  if (rightChild) 
  { 
    rightChild->showLDR(); 
  } 
} 
 
template <class T> void Node<T>::showLRD(){//后序遍历 
  if (leftChild) 
  { 
    leftChild->showLRD(); 
  } 
  if (rightChild) 
  { 
    rightChild->showLRD(); 
  } 
  cout<<this->value<<" "; 
} 
 
template <class T> void createSomeNodes(Node<T>* root, int i, T base){//构建一个二叉树 
  if (i == 0) 
  { 
    return; 
  } 
  Node<T>* l = root->insertLeftChild(i + base); 
  Node<T>* r = root->insertRightChild(i + base); 
  createSomeNodes(l, --i, base); 
  createSomeNodes(r, i, base); 
} 
 
template <class T> void showTest(Node<T>* root){//显示各种遍历方式结果 
  cout<<"先序遍历: "; 
  root->showDLR(); 
  cout<<endl<<"中序遍历: "; 
  root->showLDR(); 
  cout<<endl<<"后序遍历: "; 
  root->showLRD(); 
  cout<<endl; 
} 
 
void main(){ 
  Node<int> *root1 = new Node<int>(0); 
  createSomeNodes(root1, 3, 0); 
  cout<<"整型:"<<endl; 
  showTest(root1); 
 
  Node<char> *root2 = new Node<char>('a'); 
  createSomeNodes(root2, 3, 'a'); 
  cout<<"字符型:"<<endl; 
  showTest(root2); 
 
  Node<float> *root3 = new Node<float>(0.1f); 
  createSomeNodes(root3, 3, 0.1f); 
  cout<<"浮点型:"<<endl; 
  showTest(root3); 
} 

201649152439055.jpg (578×259)

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多