能看到二叉树 应该了解二叉树是个啥了 那就直接上代码了
#include<iostream>
#include<malloc.h>//包含malloc()、realloc()函数 没用它 用了new
using namespace std;
typedef char elementype;
typedef int Status;
typedef struct BTree
{
elementype data;
struct BTree *lef_child, *rig_child;
}*T_NODE,Bnode;
创建二叉树应用到递归的规律
//创建二叉树应用到递归的规律
Status CreatTree(T_NODE &tree)//满二叉树 从上至下 先左后右
{
//tree = (BTree*)malloc(sizeof(BTree));
tree = new BTree;
cout << "Please enter node element" << endl;
cin >> tree->data;
if (tree->data == '#')
{
cout << "The node branch is empty" << endl;
tree = NULL;
}
else
{
cout <<"Please enter the " << tree->data << " left child" << endl;
CreatTree(tree->lef_child);
cout << "Please enter the " << tree->data << " right child" << endl;
CreatTree(tree->rig_child);
}
return 0;
}
//销毁二叉树这里也是可以用遍历的方法 每找到一个对应得结点 释放之后 就是删除了
Status DesTree(T_NODE &tree)
{
if (tree != NULL)
{
free(tree);
tree = NULL;
}
return 0;
}
Status DesTree_del(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
if (tree->lef_child != NULL)
{
DesTree_del(tree->lef_child);
//tree->lef_child = NULL;
}
if (tree->rig_child != NULL)
{
DesTree_del(tree->rig_child);
//tree->rig_child = NULL;
}
if (tree != NULL)
{
free(tree);
tree = NULL;
}
return 0;
}//检验没有问题
应该也有前序删除 后续删除 中序删除
//二叉树的复制
Status CopyTree(T_NODE &tree, T_NODE &treecopy)
{
if (tree == NULL)
{
treecopy = NULL;
return 0;
}
else
{
treecopy = new BTree;
treecopy->data = tree->data;
CopyTree(tree->lef_child, treecopy->lef_child);
CopyTree(tree->rig_child, treecopy->rig_child);
}
return 0;
}
返回二叉树的深度
//返回二叉树的深度 找到最后一个结点 其左右孩子皆为空==0 比较之后其返回值为1 ,最后一个结点往上走再+1 依次从下到根反求深度//根结点的深度为1
Status DepthTree(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
else
{
int m = DepthTree(tree->lef_child);
int n = DepthTree(tree->rig_child);
if (m > n)
{
return (m + 1);
//cout << m + 1 << endl;
}
else
{
return (n + 1);
//cout << m + 1 << endl;
}
}
return 0;
}
//寻找某一结点的深度
Status Depthtree_x(T_NODE &tree,const elementype &x)
{
//int d;
if(tree)
{
if (tree->data == x)
{
//d = DepthTree(tree);
cout<< "以元素值为" << x << "的结点为根的子树的深度为: " << DepthTree(tree) << endl;
}
else
{
if (tree->lef_child)
{
Depthtree_x(tree->lef_child,x);
}
if (tree->rig_child)
{
Depthtree_x(tree->rig_child,x);
}
}
}
return 0;
}
//将二叉树的某个结点的位置元素给换成其他的
Status ChangeTree(T_NODE &tree,const elementype &x,const elementype &y)
{
if (tree == NULL)
{
return 0;
}
else
{
if (tree->data == x)
{
tree->data = y;
}
else if (tree->lef_child)
{
ChangeTree(tree->lef_child, x,y);
}
else if(tree->rig_child)
{
ChangeTree(tree->rig_child, x,y);
}
}
return 0;
}
//返回某一结点的双亲 判断是否为根元素 如果为空时
//返回某一结点的双亲 判断是否为根元素 如果为空时
Status ParentTree(T_NODE &tree, const elementype &x)
{
if (tree)
{
if (tree->lef_child&&tree->lef_child->data == x)
{
cout << "存在" << x << "的双亲结点为:" << tree->data; return 0;
}
else if (tree->rig_child&&tree->rig_child->data == x)
{
cout << "存在" << x << "的双亲结点为:" << tree->data; return 0;
}
else
{
ParentTree(tree->lef_child, x);
ParentTree(tree->rig_child, x);
}
}
else
cout <<"不存在" << endl;;
return 0;
}
前序遍历 中序遍历 后续遍历
//前序遍历
Status OutputTree_before(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
if (tree != NULL)
{
cout << tree->data << endl;
}
if (tree->lef_child != NULL)
{
OutputTree_before(tree->lef_child);
//cout << tree->lef_child->data << endl;
}
if (tree->rig_child != NULL)
{
OutputTree_before(tree->rig_child);
//cout<<tree->rig_child->data << endl;
}
return 0;
}
//中序返回 先左后根最后右
Status OutputTree_mid(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
else
{
if (tree->lef_child != NULL)
{
OutputTree_mid(tree->lef_child);
//cout << tree->lef_child->data << endl;
}
cout << tree->data << endl;
if (tree->rig_child != NULL)
{
OutputTree_mid(tree->rig_child);
//cout<<tree->rig_child->data << endl;
}
}
}
Status OutputTree_after(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
else
{
if (tree->lef_child != NULL)
{
OutputTree_after(tree->lef_child);
//cout << tree->lef_child->data << endl;
}
if (tree->rig_child != NULL)
{
OutputTree_after(tree->rig_child);
//cout << tree->rig_child->data << endl;
}
cout << tree->data << endl;
}
return 0;
}
——————————————————
#include<iostream>
#include<malloc.h>//包含malloc()、realloc()函数
using namespace std;
typedef char elementype;
typedef int Status;
typedef struct BTree
{
elementype data;
struct BTree *lef_child, *rig_child;
}*T_NODE,Bnode;
//创建二叉树应用到递归的规律
Status CreatTree(T_NODE &tree)//满二叉树 从上至下 先左后右
{
//tree = (BTree*)malloc(sizeof(BTree));
tree = new BTree;
cout << "Please enter node element" << endl;
cin >> tree->data;
if (tree->data == '#')
{
cout << "The node branch is empty" << endl;
tree = NULL;
}
else
{
cout <<"Please enter the " << tree->data << " left child" << endl;
CreatTree(tree->lef_child);
cout << "Please enter the " << tree->data << " right child" << endl;
CreatTree(tree->rig_child);
}
return 0;
}
//销毁二叉树 这个好像是直接销毁吗?
Status DesTree(T_NODE &tree)
{
if (tree != NULL)
{
free(tree);
tree = NULL;
}
return 0;
}//检验没有问题
//后续销毁 逐个递归从最后面开始删除
Status DesTree_del(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
if (tree->lef_child != NULL)
{
DesTree_del(tree->lef_child);
tree->lef_child = NULL;
}
if (tree->rig_child != NULL)
{
DesTree_del(tree->rig_child);
tree->rig_child = NULL;
}
if (tree != NULL)
{
free(tree);
tree = NULL;
}
return 0;
}//检验没有问题
//二叉树的复制
Status CopyTree(T_NODE &tree, T_NODE &treecopy)
{
if (tree == NULL)
{
treecopy = NULL;
return 0;
}
else
{
treecopy = new BTree;
treecopy->data = tree->data;
CopyTree(tree->lef_child, treecopy->lef_child);
CopyTree(tree->rig_child, treecopy->rig_child);
}
return 0;
}
//返回二叉树的深度 找到最后一个结点 其左右孩子皆为空==0 比较之后其返回值为1 ,最后一个结点往上走再+1 依次从下到根反求深度//根结点的深度为1
Status DepthTree(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
else
{
int m = DepthTree(tree->lef_child);
int n = DepthTree(tree->rig_child);
if (m > n)
{
return (m + 1);
//cout << m + 1 << endl;
}
else
{
return (n + 1);
//cout << m + 1 << endl;
}
}
return 0;
}
//寻找某一结点的深度
Status Depthtree_x(T_NODE &tree,const elementype &x)
{
//int d;
if(tree)
{
if (tree->data == x)
{
//d = DepthTree(tree);
cout << "The subtree depth of the root of element " <<x<<"is: "<< DepthTree(tree) << endl;
}
else
{
if (tree->lef_child)
{
Depthtree_x(tree->lef_child,x);
}
if (tree->rig_child)
{
Depthtree_x(tree->rig_child,x);
}
}
}
return 0;
}
//返回二叉树的高度 和求深度的区别呢???
Status HeightTree(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
else
{
int x, y;
if (tree->lef_child != NULL)
{
x= HeightTree(tree->lef_child);
//cout << tree->lef_child->data << endl;
}
if (tree->rig_child != NULL)
{
y= HeightTree(tree->rig_child);
//cout << tree->rig_child->data << endl;
}
return x>y?x+1:y+1;
}
return 0;
}
//将二叉树的某个结点的位置元素给换成其他的
Status ChangeTree(T_NODE &tree,const elementype &x,const elementype &y)
{
if (tree == NULL)
{
return 0;
}
else
{
if (tree->data == x)
{
tree->data = y;
}
else if (tree->lef_child)
{
ChangeTree(tree->lef_child, x,y);
}
else if(tree->rig_child)
{
ChangeTree(tree->rig_child, x,y);
}
}
return 0;
}
//返回某一结点的双亲 判断是否为根元素 如果为空时
Status ParentTree(T_NODE &tree, const elementype &x)
{
if (tree)
{
if (tree->lef_child&&tree->lef_child->data == x)
{
cout <<"The parent node of" <<x<<"is: "<< tree->data; return 0;
}
else if (tree->rig_child&&tree->rig_child->data == x)
{
cout << "The parent node of" << x << "is: " << tree->data; return 0;
}
else
{
ParentTree(tree->lef_child, x);
ParentTree(tree->rig_child, x);
}
}
else
cout <<"non-existent" << endl;;
return 0;
}
//前序遍历
Status OutputTree_before(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
if (tree != NULL)
{
cout << tree->data << endl;
}
if (tree->lef_child != NULL)
{
OutputTree_before(tree->lef_child);
//cout << tree->lef_child->data << endl;
}
if (tree->rig_child != NULL)
{
OutputTree_before(tree->rig_child);
//cout<<tree->rig_child->data << endl;
}
return 0;
}
//中序返回 先左后根最后右
Status OutputTree_mid(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
else
{
if (tree->lef_child != NULL)
{
OutputTree_mid(tree->lef_child);
//cout << tree->lef_child->data << endl;
}
cout << tree->data << endl;
if (tree->rig_child != NULL)
{
OutputTree_mid(tree->rig_child);
//cout<<tree->rig_child->data << endl;
}
}
}
//后续遍历
Status OutputTree_after(T_NODE &tree)
{
if (tree == NULL)
{
return 0;
}
else
{
if (tree->lef_child != NULL)
{
OutputTree_after(tree->lef_child);
//cout << tree->lef_child->data << endl;
}
if (tree->rig_child != NULL)
{
OutputTree_after(tree->rig_child);
//cout << tree->rig_child->data << endl;
}
cout << tree->data << endl;
}
return 0;
}
int main()
{
T_NODE tree,tree_;
cout <<"——————Create a binary tree——————" << endl;
CreatTree(tree);
cout << "——————Preamble output——————" << endl;
OutputTree_before(tree);
cout << "——————Middle order output——————" << endl;
OutputTree_mid(tree);
cout << "——————Sequential output——————" << endl;
OutputTree_after(tree);
cout << "——————Copy a binary tree——————" << endl;
CopyTree(tree,tree_);
cout << "——————Binary tree with preamble output replication——————" << endl;
OutputTree_before(tree_);
cout << "——————Return depth of tree——————" << endl;
int depth;
depth=DepthTree(tree);
cout << "Depth: " << depth<<endl;
cout << "——————Please enter an element——————" << endl;
elementype x;
cin >> x;
cout << "——————Returns the depth of the element——————" << endl;
Depthtree_x(tree, x);
//cout << "Depth: " << depth << endl;
cout << "——————Please select element——————" << endl;
elementype rep_x;
cin >> rep_x;
cout << "——————Please input the element to be replaced——————" << endl;
elementype rep_y;
cin >> rep_y;
cout << "——————replaced——————" << endl;
ChangeTree(tree,rep_x,rep_y);
cout << "——————Output trees after permutation——————" << endl;
OutputTree_before(tree);
cout << "——————Return to parents——————" << endl;
ParentTree(tree,x);
cout << "——————Destroy binary tree——————" << endl;
DesTree(tree);
DesTree_del(tree_);
cout << "out——test" << endl;
OutputTree_before(tree_);
OutputTree_before(tree);
return 0;
}
——————————————————
结果
:
———————————————————————————————————————
二叉树的顺序存储结构很简单。定义一个数组tree[n]
tree[i]是双亲 其左孩子为tree[i2],其有孩子为tree[2i+1]
顺序存储结构比较浪费空间,所以这种适合完全二叉树。