// 5_a1.cpp -- 二叉树结点定义.
/*
* -> 题目要求:
* 1. 完成对二叉树的二叉链表结构的定义。
* 2. 并编写算法生成一棵二叉树,以及编写二叉树的先序遍历、中序遍历、后序遍历算法,并且验证各算法功能已实现。
* 3. 此题的源程序保存为5_a1.cpp。
**/
/*
* -> 题目分析:
* 1. 定义二叉树结点,有7个函数需要写。
* 2. 第一种[2个],元素的读取,写入
* 3. 第二种[2个],左结点地址的读取,写入
* 4. 第三种[2个],右结点的地址的读取,写入
* 6. 第四种[1个],判断函数是否为空
* 7. 定义二叉树,现暂写两种函数添加,遍历。
**/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
// --------------------------------------------- 二叉树类结点定义(无特殊结构) ------------------------------------------
template <typename elem> class binarytree;
template <typename elem>
class treenode
{
friend class binarytree<elem>;
private:
elem element; // data in this node
treenode<elem> * lc; // left child
treenode<elem> * rc; // right child
public:
// 构造函数:包含两种
// 第一种:只令指针为零指针,无形参,不赋data
treenode():lc(NULL), rc(NULL){}
// 第二种:可对左右指针进行修改,有data作为形参
treenode(elem x, treenode<elem> * leftc = NULL, treenode<elem> * rightc = NULL):
element(x), lc(leftc), rc(rightc){}
// 元素
elem getelem()const {return element;} // 读
void setelem(elem x){element = x; return;} // 写
// left child
treenode<elem> * getlc()const {return lc;} // 读
void setlc(treenode<elem> * x){ lc = x;} // 写
// right child
treenode<elem> * getrc()const {return rc;} // 读
void setrc(treenode<elem> * x){ rc = x;} // 写
// empty or not
bool isleft() {return (lc == NULL) && (rc == NULL);} // 空则返回真
};
// --------------------------------------------- 二叉树类声明 ------------------------------------------
template <typename elem>
class binarytree
{
public:
treenode<elem> * root;
public:
// 构造函数
binarytree(elem x){root = new treenode<elem>(x);}
// 判空
bool isempty() {return root == NULL ? 1 : 0;}
// 添
void append(const elem & x, treenode<elem> * & temp);
// 遍历
void frontprint(treenode<elem> * & t); // 前序
void middleprint(treenode<elem> * & t); // 中序
void lastprint(treenode<elem> * & t); // 后序
};
template <typename elem>
void binarytree<elem>::append(const elem & x, treenode<elem> * & t)
{
if ( t == NULL) // 基准条件,找到位置后插入
t = new treenode<elem>( x, NULL, NULL);
else if (x < t-> element) // 比较大小,找到合适的位置
append(x, t -> lc);
else if (t -> element < x)
append(x, t -> rc);
else
;
return;
}
template <typename elem>
void binarytree<elem>::middleprint(treenode<elem> * & t)
{
if ( t != NULL)
{
middleprint( t->lc );
cout << t -> element << endl;
middleprint(t -> rc);
}
}
template <typename elem>
void binarytree<elem>::lastprint(treenode<elem> * & t)
{
if ( t != NULL)
{
lastprint( t -> lc);
lastprint ( t -> rc);
cout << t -> element << endl;
}
}
template <typename elem>
void binarytree<elem>::frontprint(treenode<elem> * & t)
{
if ( t != NULL)
{
cout << t -> element << endl;
frontprint ( t -> lc);
frontprint ( t -> rc);
}
return;
}
// -------------------------------------------------- 主函数开始 ---------------------------------------------------
int main()
{
binarytree<int> test(20);
test.append(1, test.root);
test.append(2, test.root);
test.append(30, test.root);
test.append(40, test.root);
system("pause");
cout << "前序遍历:\n";
test.frontprint(test.root);
cout << "中序遍历:\n";
test.middleprint (test.root);
cout << "后序遍历:\n";
test.lastprint (test.root);
cout << endl;
system("pause");
return 0;
}