数据结构 C++ 二叉树

论坛 期权论坛 期权     
岚雨清则   2018-4-26 13:53   6228   2
(1)假设二叉树采用链接存储方式存储,分别编写一个二叉树先序遍历的递归算法和非递归算法。
  (2)一棵完全二叉树以顺序方式存储,设计一个递归算法,对该完全二叉树进行中序遍历。
求解,谢谢
分享到 :
0 人收藏

2 个回复

倒序浏览
2#
comeylon  1级新秀 | 2018-4-30 01:59:15
线索二叉的应用。要求:线索二叉树的建立、插入、删除、恢复线索的实现。 求呵呵,我刚好学完数据结构,试验的时候自己写了线索二叉树的头文件; ----
3#
小宝__1994  2级吧友 | 2018-4-30 01:59:16
/*
聽聽聽聽聽聽二叉树聽聽聽聽聽聽2014年5月15日
测试数据:abc聽聽de聽g聽聽f聽聽聽#
先序遍历顺序:abcdegf
中序遍历顺序:cbegdfa
后续遍历顺序:cgefdba
叶子结点是:cgf
*/
#include聽
#include聽
#include聽
#include聽
#include聽
#include聽
using聽namespace聽std;
typedef聽struct聽BINTREE
{
char聽data;
struct聽BINTREE聽*leftchild;
struct聽BINTREE聽*rightchild;
}*ptree,tree;
void聽creat_tree(ptree聽&t,string聽str)//创建二叉树
{
static聽int聽i聽=聽0;
if聽('聽'聽==聽str)
{
  i++;
  t聽=聽NULL;
}
else
{
  t聽=聽new聽tree;
  if聽(NULL聽==聽t)
  {
   coutleftchild,str);
聽聽聽聽聽聽聽聽聽聽聽聽creat_tree(t->rightchild,str);
  }
}
return;
}
void聽first_visit(ptree聽t)//先序遍历
{
if聽(NULL聽!=聽t)
{
  coutleftchild);
聽聽聽聽first_visit(t->rightchild);
}
}
void聽middle_visit(ptree聽t)//中序遍历
{
if聽(NULL聽!=聽t)
{
  middle_visit(t->leftchild);
  coutrightchild);
}
}
void聽last_visit(ptree聽t)//后序遍历
{
if聽(NULL聽!=聽t)
{
  last_visit(t->leftchild);
  last_visit(t->rightchild);
  coutleftchild聽&&聽NULL聽==聽t->rightchild)
{
  coutleftchild)
  {
聽聽聽聽聽聽聽聽聽聽聽聽print_leaf(t->leftchild);
  }
  if聽(NULL聽!=聽t->rightchild)
  {
   print_leaf(t->rightchild);
  }
}
}
void聽destroy_tree(ptree聽t)
{
if聽(NULL聽==聽t)
{
  destroy_tree(t->leftchild);
  destroy_tree(t->rightchild);
  free(t);
}
}
bool聽is_creattree聽=聽false;
int聽main()
{
聽聽聽聽ptree聽t聽=聽NULL;
聽聽聽聽string聽str;
char聽ch聽=聽0;
cout
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:
帖子:
精华:
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP