一个简单的数据结构举例——二叉树及…
2015-05-30 23:47
453 查看
链接地址:BinaryTree
//头文件名:BTree.h
#include "stdafx.h"
#include "iostream"
using namespace std;
class BTree
{
public :
int data;
BTree *left;
BTree *right;
BTree(int);
BTree();
};
//cpp名:BTree.cpp
#include "stdafx.h"
#include "BTree.h"
BTree::BTree(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
BTree::BTree()
{
this->data = NULL;
this->left = NULL;
this->right = NULL;
}
// BinaryTree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "BTree.h"
#include "ctime"
void Insert(BTree*,BTree*);
void Display(BTree *head);
void FirstDisplay(BTree *p);
void LastDisplay(BTree *head);
BTree *node=NULL;
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
while (1)
{
node = NULL;
cout << "1:随机产生结点" << endl
<< "2:自定义结点(以-99999为结束)" << endl;
cin >> x;
if (1==x || 2==x)
{
switch (x)
{
case 1:goto f1; break;
case 2:goto f2; break;
}
f1:
{
srand(unsigned(time(NULL)));
cout << "产生多少个结点:";
cin >> x;
for (int i = 0; i < x; i++)
{
y = rand() % 100 - rand() % 100;
cout << y << " ";
Insert(node, new BTree(y));
}
cout << endl << endl << "前序遍历结果:";
FirstDisplay(node);
cout << endl << endl << "中序遍历结果:";
Display(node);
cout << endl << endl << "后序遍历结果:";
LastDisplay(node);
cout << endl << endl;
continue;
}
f2:
{ while (cin >> x && x!= -99999)
{
Insert(node, new BTree(x));
}
cout << endl << endl << "前序遍历结果:";
FirstDisplay(node);
cout << endl << endl << "中序遍历结果:";
Display(node);
cout << endl << endl << "后序遍历结果:";
LastDisplay(node);
cout << endl<<endl;
}
}
else continue;
}
return 0;
}
void Insert(BTree *father,BTree *son)
{
if (NULL == father)
{
node = son;
return;
}
if (father->data > son->data)
{
if (father->left != NULL)
{
Insert(father->left, son);
}
else
{
father->left = son;
}
}
else if (father->data < son->data)
{
if (father->right != NULL)
{
Insert(father->right, son);
}
else
{
father->right = son;
}
}
else
{
if (!father->left)
father->left = son;
else if (!father->right)
father->right = son;
else
Insert(father->left, son);
}
}
//中序遍历
void Display(BTree *p)
{
if (p->left)
Display(p->left);
cout << p->data << " ";
if (p->right)
Display(p->right);
else return;
}
//前序遍历
void FirstDisplay(BTree *p)
{
cout << p->data << " ";
if (p->left)
FirstDisplay(p->left);
if (p->right)
FirstDisplay(p->right);
else return;
}
//后序遍历
void LastDisplay(BTree *p)
{
if (p->left)
LastDisplay(p->left);
if (p->right)
LastDisplay(p->right);
cout << p->data << " ";
return;
}
//头文件名:BTree.h
#include "stdafx.h"
#include "iostream"
using namespace std;
class BTree
{
public :
int data;
BTree *left;
BTree *right;
BTree(int);
BTree();
};
//cpp名:BTree.cpp
#include "stdafx.h"
#include "BTree.h"
BTree::BTree(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
BTree::BTree()
{
this->data = NULL;
this->left = NULL;
this->right = NULL;
}
// BinaryTree.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "BTree.h"
#include "ctime"
void Insert(BTree*,BTree*);
void Display(BTree *head);
void FirstDisplay(BTree *p);
void LastDisplay(BTree *head);
BTree *node=NULL;
int _tmain(int argc, _TCHAR* argv[])
{
int x, y;
while (1)
{
node = NULL;
cout << "1:随机产生结点" << endl
<< "2:自定义结点(以-99999为结束)" << endl;
cin >> x;
if (1==x || 2==x)
{
switch (x)
{
case 1:goto f1; break;
case 2:goto f2; break;
}
f1:
{
srand(unsigned(time(NULL)));
cout << "产生多少个结点:";
cin >> x;
for (int i = 0; i < x; i++)
{
y = rand() % 100 - rand() % 100;
cout << y << " ";
Insert(node, new BTree(y));
}
cout << endl << endl << "前序遍历结果:";
FirstDisplay(node);
cout << endl << endl << "中序遍历结果:";
Display(node);
cout << endl << endl << "后序遍历结果:";
LastDisplay(node);
cout << endl << endl;
continue;
}
f2:
{ while (cin >> x && x!= -99999)
{
Insert(node, new BTree(x));
}
cout << endl << endl << "前序遍历结果:";
FirstDisplay(node);
cout << endl << endl << "中序遍历结果:";
Display(node);
cout << endl << endl << "后序遍历结果:";
LastDisplay(node);
cout << endl<<endl;
}
}
else continue;
}
return 0;
}
void Insert(BTree *father,BTree *son)
{
if (NULL == father)
{
node = son;
return;
}
if (father->data > son->data)
{
if (father->left != NULL)
{
Insert(father->left, son);
}
else
{
father->left = son;
}
}
else if (father->data < son->data)
{
if (father->right != NULL)
{
Insert(father->right, son);
}
else
{
father->right = son;
}
}
else
{
if (!father->left)
father->left = son;
else if (!father->right)
father->right = son;
else
Insert(father->left, son);
}
}
//中序遍历
void Display(BTree *p)
{
if (p->left)
Display(p->left);
cout << p->data << " ";
if (p->right)
Display(p->right);
else return;
}
//前序遍历
void FirstDisplay(BTree *p)
{
cout << p->data << " ";
if (p->left)
FirstDisplay(p->left);
if (p->right)
FirstDisplay(p->right);
else return;
}
//后序遍历
void LastDisplay(BTree *p)
{
if (p->left)
LastDisplay(p->left);
if (p->right)
LastDisplay(p->right);
cout << p->data << " ";
return;
}
相关文章推荐
- 一个简单的数据结构举例——快排结合…
- 【技术-数据结构与算法分析】一致性哈希算法与Java实现
- CodeForces 140C - New Year Snowmen(数据结构)
- 数据结构面试题总结7——数组:最大连续子段和、最大连续子段积
- COJ 1002 WZJ的数据结构(二)(splay模板)
- 数据结构面试题总结6——数组:求两个数组中满足给定和的两个元素
- 数据结构与算法分析——C语言描述
- 稀疏矩阵的快速转置
- 数据结构面试题总结5——数组:找出数组中唯一一个出现一次的元素
- 数据结构之---C语言实现稀疏矩阵
- 数据结构面试题总结4——数组:求两个有序数组中的共同元素
- 数据结构学习
- 数据结构之 --线性表之--串
- 【整理】数据结构与算法【概念】
- 黑马程序员-数据类型、变量作用域和数据结构的学习笔记
- 算法导论14(数据结构的扩张)
- 数据结构面试题总结3——数组:求数组中两个元素的最小距离
- 星际之门(一)
- C#数据结构回顾之循环队列
- Dancing Links讲解和练习整理