验证二叉树的链接存储结构及其上的基本操作。
[实验要求]:
- 从文件创建一棵二叉树,并对其初始化;
- 先根、中根、后根遍历二叉树;
- 在二叉树中搜索给定结点的父结点;
- 搜索二叉树中符合数据域条件的结点;
- 从二叉树中删除给定结点及其左右子树。
[截图]:
- 文件截图
2. 操作截图
[实现代码]:
分成三个文件tree.h、tree.cpp和main.cpp
tree.h
#ifndef _TREE_H
#define _TREE_H
#include<iostream>
#include<fstream>
using namespace std;
class Node{
private:
Node *right;
Node *left;
public:
char data;
Node(const char&item,Node*lptr=NULL,Node*rptr=NULL):data(item),left(lptr),right(rptr){
}
Node*getleft(void)const{
return left;}
void setleft(Node*L){
left=L;}
Node*getright(void)const{
return right;}
void setright(Node*R){
right=R;}
char&getdata(){
return data;}
void setdata(const char&item){
data=item;}
};
class tree{
private:
Node *root;
char stop;
public:
tree(Node*t=NULL):root(t){
}
~tree(){
delete root;}
void createtree();
Node*create(char*a);
Node*getroot(){
return root;}
void setstop(char stop) {
this->stop=stop;}
Node*father(Node*root,Node*son);
Node*find(Node*root,const char