数据结构/数据结构与算法实验二 二叉树相关算法实现

简介: 数据结构/数据结构与算法实验二 二叉树相关算法实现

1.实验题目

1.【功能1】按先序次序建立一棵二叉树,以‘#’表示空。

2.【功能2】中序遍历二叉树,输出遍历序列。

3.【功能3】后序遍历二叉树,输出遍历序列。

4.【功能4】求出二叉树的深度并输出。

5.【功能5】求出二叉树的叶子数目并输出。

6.【功能6】以栈为辅助存储结构实现二叉树的先序非递归算法,输出二叉树的先序非递归遍历序列。

7.【功能7】以队列为辅助存储结构实现二叉树的层次遍历算法,输出二叉树的层次遍历序列。

8.【功能8】以栈为辅助存储结构实现二叉树的中序非递归算法,输出二叉树的中序非递归遍历序列。

2.实验要求

1、二叉树以二叉链表为存储结构。

2、主程序测试数据

 (1)输入以下字符串,先序建立二叉树:ABC##DE#G##F###

 (2)输出中序遍历序列应为:CBEGDFA

 (3)输出后序遍历序列应为:CGEFDBA

 (4)输出二叉树的深度应为:5

 (5)输出二叉树的叶子数目应为:3

 (6)输出二叉树的先序非递归遍历序列应为:ABCDEGF

 (7)输出二叉树的层次遍历序列应为:ABCDEFG

 (8)输出二叉树的中序非递归遍历序列应为:CBEGDFA

2、可以在教材定义的基础上,增加你的算法为成员函数。

3.算法思路

1.按先序次序建立一棵二叉树,以‘#’表示空

 利用递归的办法。

 首先建立一个根结点。

 函数开始时,先检测数据。若数据为#号,则使其左子树和右子树为空。若该数据为正常的字符,则生成左结点和右结点。

 输入下一个数据,并以该数据递归地生成左子树。在左子树构建完毕后再输入数据,以该数据递归地生成右子树。至此,以先序次序建立的二叉树完成了。

 这样的二叉树有这样几种结点:根结点:可以直接被getroot访问。无效的结点:数据域为#,指针域为空。数据结点:数据域记录了真实数据。但其指针域不为空,可能指向另一个数据结点,也可能指向无效的结点。

4.求二叉树的深度

 利用递归的办法。

 求子二叉树的深度。在双亲结点上,取最深的子二叉树的深度值,并+1,可得以该结点构建的子二叉树的深度。在叶结点上,二叉树深度值为1.

5.求出二叉树的叶子数目

 定义一个属性记录二叉树的叶子数目。利用递归先序遍历的办法。

 正常的先序遍历中我们需要访问结点并输出数值。但是,在求叶子结点的过程中,我们只需判断该结点是否为叶子结点。若该结点的左右结点的数据域均为#,则说明该结点就是叶子结点。

6.二叉树的非递归先序遍历

 利用栈作为辅助存储结构,实现二叉树的非递归先序遍历。

 定义一个栈,并将根结点入栈。定义一个指针。当栈非空且指针指向不为空时,进行循环:先不断访问左结点,并将每个访问到的结点入栈,直到访问到空结点。此时,若栈非空,取栈顶并访问其右结点。将该过程循环,就可以实现二叉树的非递归先序遍历。

8.二叉树的非递归中序遍历

 利用栈作为辅助存储结构,实现二叉树的非递归中序遍历。

 定义一个栈,并将根结点入栈。定义一个指针。当栈非空且指针指向不为空时,进行循环:先不断访问左结点,并将每个访问到的结点入栈,直到访问到空结点。此时,若找到空结点,将该结点出栈。取栈顶元素,访问其右结点。

3.功能演示

4.总结

附录:源代码

bintree.h

#pragma once
#include"bintreenode.h"
#include"stack.h"
#include"queue.h"
#include<iostream>
#include<Windows.h>
using namespace std;
template<class elemtype>
class bintree
{
private:
  bintreenode<elemtype>* root;
  mutable int leafnum;
  mutable int height;
public:
  int getleafnum() {
    return leafnum;
  }
  bintreenode<elemtype>* getroot() {
    return this->root;
  }
  bintree() {
    root = new bintreenode<elemtype>();
    leafnum = 0;
    height = 0;
  }
  void generatebintree(elemtype ch,bintreenode<elemtype> *n) {
    n->data = ch;
    if (ch == '#') {
      n->leftchild = NULL;
      n->rightchild = NULL;
      return;
    }//数据域为#,则没有孩子
    n->leftchild = new bintreenode<elemtype>(NULL, NULL, NULL);//数据域不为#,则正常构建孩子结点
    n->rightchild = new bintreenode<elemtype>(NULL, NULL, NULL);//构建孩子结点
    elemtype newch;
    cin >> newch;
    generatebintree(newch, n->leftchild);//先根据数据构建左子树
    cin >> newch;
    generatebintree(newch, n->rightchild);//再根据数据构建右子树
    return;
  }
  void inorder(bintreenode<elemtype>* r) const {
    if (r != NULL) {
      inorder(r->leftchild);
      if(r->data!='#')
        cout << r->data;
      inorder(r->rightchild);
    }
    return;
  }
  void postorder(bintreenode<elemtype>* r) const {
    if (r != NULL) {
      postorder(r->leftchild);
      postorder(r->rightchild);
      if(r->data!='#')
        cout << r->data;
    }
    return;
  }
  ~bintree() {
    deletetree(root);//析构函数
  }
  void deletetree(bintreenode<elemtype>* r) {
    if (r != NULL) {
      deletetree(r->leftchild);
      deletetree(r->rightchild);
      delete r;//后序遍历的办法delete树
    }
    return;
  }
  void countleaf(bintreenode<elemtype>* r) const {
    if (r->data == '#')
      return;
    if (r->leftchild->data == '#' && r->rightchild->data == '#') {
      leafnum++;//如果左右结点都是#,则说明这个结点就是叶结点
      return;
    }
    if (r != NULL) {
      countleaf(r->leftchild);
      countleaf(r->rightchild);//不是叶节点,则递归的计算
    }
  }
  int countheight(bintreenode<elemtype>* r) const {
    if (r->data == '#' )
      return 0;//空结点不算深度
    else 
      return max(countheight(r->leftchild), countheight(r->rightchild)) + 1;//选最深的子树,深度+1
  }
  void stackpreorder() const {
    stack<bintreenode<elemtype>* > nodestack(100);
    bintreenode<elemtype>* p=root;
    while (!nodestack.isempty() || p != NULL) {//当栈非空,p不指向空结点时,继续循环
      while (p != NULL) {
        if (p->data != '#')
          cout << p->data;
        nodestack.push(p);//将其入栈
        p = p->leftchild;//找到最左的结点
      }
      if (!nodestack.isempty()) {
        p = nodestack.gettop();//取栈顶,访问栈顶的右子树
        nodestack.pop(p);
        p = p->rightchild;
      }
    }
  }
  void levelorder() const {//层次遍历
    queue<bintreenode<elemtype>*> nodequeue;
    bintreenode<elemtype>* p;
    if (root != NULL)
      nodequeue.enqueue(root);
    while (!nodequeue.isempty()) {//当队列非空时,继续运行
      nodequeue.delqueue(p);//将队头出队
      if (p->data != '#')
        cout << p->data;
      if (p->leftchild != NULL)
        nodequeue.enqueue(p->leftchild);//将p左结点入队
      if (p->rightchild != NULL)
        nodequeue.enqueue(p->rightchild);//将p右结点入队
    }
  }
  void stackinorder()const {//非递归中序遍历
    stack<bintreenode<elemtype>*> nodestack;
    bintreenode<elemtype>* p=root;
    while (!nodestack.isempty()||p->data!='#') {
      if (p->data != '#') {
        nodestack.push(p);
        p = p->leftchild;//找到最左结点,不断将访问到的结点入栈
      }
      else {
        nodestack.pop(p);//栈顶出栈,访问右子树
        if (p->data != '#')
          cout << p->data;
        p = p->rightchild;
      }
    }
  }
};

bintreenode.h

#pragma once
#include<Windows.h>
template<class elemtype>
struct bintreenode
{
public:
  elemtype data;
  bintreenode<elemtype>* leftchild;
  bintreenode<elemtype>* rightchild;
  bintreenode() {
    leftchild = NULL;
    rightchild = NULL;
    data = NULL;
  }
  bintreenode(const elemtype& d, bintreenode<elemtype>* l = NULL, bintreenode<elemtype>* r = NULL) {
    data = d;
    leftchild = l;
    rightchild = r;
  }
};

queue.h

#pragma once
#define status int
#define SUCCESS 1
#define ERROR 0
#include<Windows.h>
template <class elemtype>
class queue
{
protected:
  int front, rear;
  int maxsize;
  elemtype* elems;
public:
  queue(int size=100) {
    maxsize = size;
    if (elems != NULL) delete[]elems;
    elems = new elemtype[maxsize];
    rear = front = 0;
  }
  ~queue() {
    delete[]elems;
  }
  status enqueue(const elemtype& e) {
    if ((rear + 1) % maxsize == front)
      return ERROR;
    else {
      elems[rear]= e;
      rear = (rear + 1) % maxsize;
      return SUCCESS;
    }
  }
  status delqueue(elemtype& e) {
    if (!isempty()) {
      e = elems[front];
      front = (front + 1) % maxsize;
      return SUCCESS;
    }
    else
      return 0;
  }
  bool isempty() {
    return rear == front;
  }

stack.h

#pragma once
#define status int
#define ERROR 0
#define SUCCESS 1
#include<Windows.h>
template <typename elemtype>
class stack
{
private:
  int top;
  int maxsize;
  elemtype* elems;
public:
  stack(int size = 100) {
    top = -1;
    maxsize = size;
    elems = new elemtype[maxsize];
  }
  ~stack(){
    delete []elems;
  }
  status push(const elemtype e){
    if (top == maxsize)
      return ERROR;
    else
      elems[++top] = e;
    return SUCCESS;
  }
  status pop(elemtype &e){
    if (top == -1)
      return ERROR;
    else
      e = elems[top--];
    return SUCCESS;
  }
  elemtype gettop() {
    if (isempty())
      return NULL;
    else
      return elems[top];
  }
  bool isempty(){
    return (top == -1);
  }
};

main.cpp

#include<iostream>
#include"bintree.h"
#include"bintreenode.h"
using namespace std;
int main() {
  bintree<char> tree;
//  tree.root = new bintreenode<char>();
  char ch;
  cout << "请输入你的树:";
  cin >> ch;
  tree.generatebintree(ch, tree.getroot());
  cout << "中序遍历:";
  tree.inorder(tree.getroot());
  cout << endl;
  cout << "后序遍历:";
  tree.postorder(tree.getroot());
  tree.countleaf(tree.getroot());
  cout << endl;
  cout << "叶结点数目"<<tree.getleafnum()<<endl;
  cout << "二叉树深度:" << tree.countheight(tree.getroot())<< endl;
  cout << "非递归先序遍历";
  tree.stackpreorder();
  cout << endl;
  cout << "非递归层次遍历:";
  tree.levelorder();
  cout << endl;
  cout << "非递归中序遍历";
  tree.stackinorder();
  cout << endl;
  cout << "运行结束";
}


目录
相关文章
|
3天前
|
存储 算法 Java
算法系列之数据结构-二叉树
树是一种重要的非线性数据结构,广泛应用于各种算法和应用中。本文介绍了树的基本概念、常见类型(如二叉树、满二叉树、完全二叉树、平衡二叉树、B树等)及其在Java中的实现。通过递归方法实现了二叉树的前序、中序、后序和层次遍历,并展示了具体的代码示例和运行结果。掌握树结构有助于提高编程能力,优化算法设计。
32 9
 算法系列之数据结构-二叉树
|
2天前
|
算法 Java
算法系列之数据结构-二叉搜索树
二叉查找树(Binary Search Tree,简称BST)是一种常用的数据结构,它能够高效地进行查找、插入和删除操作。二叉查找树的特点是,对于树中的每个节点,其左子树中的所有节点都小于该节点,而右子树中的所有节点都大于该节点。
43 22
|
1月前
|
存储 机器学习/深度学习 算法
C 408—《数据结构》算法题基础篇—链表(下)
408考研——《数据结构》算法题基础篇之链表(下)。
88 29
|
1月前
|
存储 算法 C语言
C 408—《数据结构》算法题基础篇—链表(上)
408考研——《数据结构》算法题基础篇之链表(上)。
99 25
|
1月前
|
存储 人工智能 算法
C 408—《数据结构》算法题基础篇—数组(通俗易懂)
408考研——《数据结构》算法题基础篇之数组。(408算法题的入门)
72 23
|
2月前
|
存储 算法 测试技术
【C++数据结构——树】二叉树的遍历算法(头歌教学实验平台习题) 【合集】
本任务旨在实现二叉树的遍历,包括先序、中序、后序和层次遍历。首先介绍了二叉树的基本概念与结构定义,并通过C++代码示例展示了如何定义二叉树节点及构建二叉树。接着详细讲解了四种遍历方法的递归实现逻辑,以及层次遍历中队列的应用。最后提供了测试用例和预期输出,确保代码正确性。通过这些内容,帮助读者理解并掌握二叉树遍历的核心思想与实现技巧。
55 2
|
4月前
|
C语言
【数据结构】栈和队列(c语言实现)(附源码)
本文介绍了栈和队列两种数据结构。栈是一种只能在一端进行插入和删除操作的线性表,遵循“先进后出”原则;队列则在一端插入、另一端删除,遵循“先进先出”原则。文章详细讲解了栈和队列的结构定义、方法声明及实现,并提供了完整的代码示例。栈和队列在实际应用中非常广泛,如二叉树的层序遍历和快速排序的非递归实现等。
381 9
|
4月前
|
存储 算法
非递归实现后序遍历时,如何避免栈溢出?
后序遍历的递归实现和非递归实现各有优缺点,在实际应用中需要根据具体的问题需求、二叉树的特点以及性能和空间的限制等因素来选择合适的实现方式。
64 1
|
2月前
|
存储 C语言 C++
【C++数据结构——栈与队列】顺序栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现顺序栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 1.初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储
158 77
|
7天前
|
算法 调度 C++
STL——栈和队列和优先队列
通过以上对栈、队列和优先队列的详细解释和示例,希望能帮助读者更好地理解和应用这些重要的数据结构。
17 4