数据结构(5) -- 二叉树
//BinaryTree.h #ifndef BINARYTREE_H_ #define BINARYTREE_H_ #define ElemType int #define MAXSIZE 100 typedef struct _TreeNode { ElemType data; //存储的数据 _TreeNode* left; _TreeNode* right; }TreeNode; class BinaryTree { private: TreeNode *root; public: BinaryTree(ElemType d); //创建一个值为d的根节点 ~BinaryTree(); TreeNode* GetRoot() { return root; } //添加二叉树节点 void InsertNodeAsL(TreeNode *p, ElemType e); //作为当前节点的左孩子插入新节点 void InsertNodeAsR(TreeNode *p, ElemType e); //作为当前节点的右孩子插入新节点 //清空二叉树所有节点 void ClearTreeNode(TreeNode *ps); //遍历二叉树 void PreOrder(TreeNode *p); //先序遍历 void CenOrder(TreeNode *p); //中序遍历 void LasOrder(TreeNode *p); //后序遍历 void Trave(TreeNode *p); //层次遍历 //二叉树深度 int Deepth(TreeNode *p); bool Empty(); //是否为空树 }; #endif //BinaryTree.cpp #include "BinaryTree.h" #include <queue> #include <iostream> using namespace std; BinaryTree::BinaryTree(ElemType d) { root = new TreeNode(); root->data = d; root->left = NULL; root->right = NULL; } BinaryTree::~BinaryTree() { ClearTreeNode(root); } //添加二叉树节点 void BinaryTree::InsertNodeAsL(TreeNode *p, ElemType e) { TreeNode *s = new TreeNode(); s->data = e; s->left = NULL; s->right = NULL; p->left = s; } //遍历二叉树 void BinaryTree::InsertNodeAsR(TreeNode *p, ElemType e) { TreeNode *s = new TreeNode(); s->data = e; s->left = NULL; s->right = NULL; p->right = s; } void BinaryTree::ClearTreeNode(TreeNode *p) { while (p) { ClearTreeNode(p->left); ClearTreeNode(p->right); delete p; //释放当前节点 } } //遍历二叉树 void BinaryTree::PreOrder(TreeNode *p) { if (p == NULL) return; std::cout << p->data << " "; PreOrder(p->left); PreOrder(p->right); } void BinaryTree::CenOrder(TreeNode *p) { if (p == NULL) return; CenOrder(p->left); std::cout << p->data << " "; CenOrder(p->right); } void BinaryTree::LasOrder(TreeNode *p) { if (p == NULL) return; LasOrder(p->left); LasOrder(p->right); std::cout << p->data << " "; } //层次遍历二叉树 void BinaryTree::Trave(TreeNode *p) { std::queue<TreeNode*> q; q.push(p); while (!q.empty()) { TreeNode *s = q.front(); std::cout << s->data << " "; if (s->left != NULL) q.push(s->left); if (s->right != NULL) q.push(s->right); q.pop(); } } //二叉树深度 int BinaryTree::Deepth(TreeNode *p) { int hl; int hr; int maxh; if (p) { hl = Deepth(p->left); hr = Deepth(p->right); maxh = (hl > hr) ? hl : hr; return (maxh + 1); } else return 0; } bool BinaryTree::Empty() { if (root == NULL) return true; else return false; } //测试代码 #include <iostream> #include "BinaryTree.h" #include <string> #include <queue> using namespace std; int main() { BinaryTree *b = new BinaryTree(1); b->InsertNodeAsL(b->GetRoot(), 2); b->InsertNodeAsR(b->GetRoot(), 3); b->InsertNodeAsL(b->GetRoot()->left, 4); b->InsertNodeAsR(b->GetRoot()->left, 5); b->InsertNodeAsL(b->GetRoot()->right, 6); b->InsertNodeAsR(b->GetRoot()->right, 7); b->InsertNodeAsL(b->GetRoot()->left->left, 8); cout << "先序遍历: "; b->PreOrder(b->GetRoot()); cout << endl; cout << "中序遍历: "; b->CenOrder(b->GetRoot()); cout << endl; cout << "后序遍历: "; b->LasOrder(b->GetRoot()); cout << endl; cout << "层次遍历: "; b->Trave(b->GetRoot()); cout << endl; cout << "树的深度:"; cout << b->Deepth(b->GetRoot()); cout << endl; system("pause"); return 0; }