⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 清华大学计算机系数据结构课程教材《数据结构 用面向对象方法和C++描述》(殷人昆主编)的类库(书中程序的源代码)
💻 CPP
字号:
#include<iostream>
using namespace std;


#include "Tree.h"
template<class T>
void visit (TreeNode<T> *t) {
	cout << t ->data << ' ';
}

void Printinfo(bool flag) {
	if(flag) cout << "成功" << endl;
	else cout << "失败" << endl;
}

int main(){
	TreeNode<char> n0, n1, n2, n3, n4;
    n0.data = 'D'; n1.data = 'E'; n2.data ='F'; n3.data ='G'; n4.data ='H';
	n0.firstChild = &n1; n0.nextSibling = NULL;
	n1.firstChild = &n2; n1.nextSibling = &n3;
	n2.firstChild = &n4; n2.nextSibling = NULL;
	n3.firstChild = n3.nextSibling = NULL;
	n4.firstChild = n4.nextSibling = NULL;

	Tree<char> tmp(&n0, &n0);
	cout << "树为:" << endl;
	cout << "                      D                             " << endl;
    cout << "                E         F                      "  << endl;
	cout << "                G                                  "   <<endl;
	cout << "                H                                  "   << endl;
	cout  << endl;
	cout <<"是否为空:"<<endl;
	Printinfo(!tmp.IsEmpty());
    cout << endl;
	cout << "先根次序遍历:" << endl;
	tmp.PreOrder(visit);
	cout << endl;
	cout <<  endl;
	cout << "后根次序遍历:" << endl;
	tmp.PostOrder(visit);
	cout << endl;

	cout << "搜索元素:" << endl;
	char sc;
	cin >> sc;
	Printinfo(tmp.Find(sc));
	tmp.show(visit);
    cout<< endl;
	cout << "置根为当前结点后:" << endl;
	tmp.Root();
	tmp.show(visit);
	cout <<   endl;
	cout << "置当前结点的首子女为当前结点后:" << endl;
	tmp.FirstChild();
	tmp.show(visit);
	cout <<endl;
	cout << "置当前结点的下一个兄弟为当前结点后:" << endl;
	tmp.NextSibling();
	tmp.show(visit);
	cout << endl;
	cout << "置当前结点的父亲结点为当前结点后:" << endl;
	tmp.Parent();
	tmp.show(visit);
	cout<<"测试完毕"<<endl;

	while(1)//为了在类库说明文档中便于观察,加入这一句
		cout<<"";
	return 0;
}

/*
测试示例:

树为:
                      D
                E         F
                G
                H

是否为空:
成功

先根次序遍历:
D E F H G

后根次序遍历:
H F E G D
搜索元素:
E
成功
树:
根为:D
当前结点:E

置根为当前结点后:
树:
根为:D
当前结点:D

置当前结点的首子女为当前结点后:
树:
根为:D
当前结点:E

置当前结点的下一个兄弟为当前结点后:
树:
根为:D
当前结点:G

置当前结点的父亲结点为当前结点后:
树:
根为:D
当前结点:D
测试完毕

*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -