码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树

时间:2019-03-07 17:50:36      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:clu   creat   bit   abd   pause   方式   cti   添加   ons   

二叉树的基本操作(C++)

1. 首先创建二叉树结构,以及二叉树的类,定义在BinaryTree.h中

 1 #ifndef _BINARYTREE_H
 2 #define _BINARYTREE_H
 3 #include "pch.h"
 4 #include <iostream>
 5 
 6 using namespace std;
 7 
 8 typedef struct TreeNode
 9 {
10     char val;            //节点的数据类型
11     TreeNode * left;     //左子树
12     TreeNode * right;    //右子树
13     TreeNode(char x):val(x), left(nullptr), right(nullptr) {}
14 
15 }TreeNode, *BiTree;
16 
17 class BinaryTree
18 {
19 public:
20     BinaryTree() {}
21     ~BinaryTree() {}
22     void TreeCreate();            //递归的创建二叉树的节点
23     int getSize();                //递归得到树的节点数目
24     int getHeight();              //递归得到树的高度
25     void preOrder();              //前序遍历
26     void inOrder();               //中序遍历
27     void postOrder();             //后序遍历
28     void distroy();               //删除二叉树
29 
30 private:
31     BiTree create();                        //递归的创建二叉树的节点
32     void preOrder(BiTree root);             //前序遍历
33     void inOrder(BiTree root);              //中序遍历
34     void postOrder(BiTree root);            //后序遍历
35     void distroy(BiTree root);              //摧毁树
36     int getHeight(BiTree root);             //递归得到树的高度
37     void AddNode(const char key, int direction, BiTree root);         //添加节点
38     BiTree m_root;                          //根节点
39     int size;                               //节点总数
40 };
41 
42 #endif

2. 具体实现    BinaryTree.cpp

# include "BinaryTree.h"

#pragma
region 私有成员函数 //添加节点 //key为要插入的值,direction是从左子树插入还是右子树插入,root为从哪个节点插入 void BinaryTree::AddNode(const char key, int direction, BiTree root) { if (direction == 0) { //从左子树插入 if (root->left == NULL) root->left = new TreeNode(key); else AddNode(key, direction, root->left); } else if (direction == 1) { //从右子树插入 if (root->right == NULL) root->right = new TreeNode(key); else AddNode(key, direction, root->right); } } //二叉树的建立,按前序遍历(root->left->right)的方式建立二叉树 BiTree BinaryTree::create() { BiTree current = NULL; char val; cin >> val;//输入键值 if (val == #)//标识当前子树为空,转向下一节点 { return NULL; } else { //递归的创建左右子树 size++;//记录节点数 current = new TreeNode(val); current->left = create(); current->right = create(); return current; } } //删除二叉树 void BinaryTree::distroy(BiTree root) { if (root) { distroy(root->left); distroy(root->right); delete root; root = NULL; size = 0; } } //递归得到树的高度 int BinaryTree::getHeight(BiTree root) { if (root == NULL) return 0; int left_height = getHeight(root->left); int right_height = getHeight(root->right); return (left_height > right_height) ? (left_height + 1) : (right_height + 1); } //前序遍历 root->left->right void BinaryTree::preOrder(BiTree root) { if (root == NULL) return; else { cout << root->val << " --> "; //首先打印根节点 preOrder(root->left); //接着遍历左子树 preOrder(root->right); //接着遍历右子树 } } //中序遍历 left->root->right void BinaryTree::inOrder(BiTree root) { if (root == NULL) return; else { inOrder(root->left); //首先遍历左子树 cout << root->val << " --> "; //接着打印根节点 inOrder(root->right); //接着遍历右子树 } } //后序遍历 left->right->root void BinaryTree::postOrder(BiTree root) { if (root == NULL) return; else { postOrder(root->left); //首先遍历左子树 postOrder(root->right); //接着遍历右子树 cout << root->val << " --> "; //接着打印根节点 } } #pragma endregion #pragma region 公有成员函数 //二叉树的建立 void BinaryTree::TreeCreate() { size = 0; m_root =create(); } //删除二叉树 void BinaryTree::distroy() { distroy(m_root); } //递归得到树的高度 int BinaryTree::getHeight() { return getHeight(m_root); } //前序遍历 void BinaryTree::preOrder() { cout << "前序遍历的结果如下:" << endl; preOrder(m_root); cout << endl; } //中序遍历 void BinaryTree::inOrder() { cout << "中序遍历的结果如下:" << endl; inOrder(m_root); cout << endl; } //后序遍历 void BinaryTree::postOrder() { cout << "后序遍历的结果如下:" << endl; postOrder(m_root); cout << endl; } //获取大小 int BinaryTree::getSize() { //这里是创建时候直接进行了计数 //也可以利用遍历的方式获取,当节点有值,就加1 return size; } #pragma endregion

3.主函数调用

int main()
{
    BinaryTree tree;
    cout << "按前序遍历方式创建树" << endl;
    //"ABDG##H###CE#I##F##";
    tree.TreeCreate();
    cout << "树的高度为:" << tree.getHeight() << endl;
    cout << "树的节点为:" << tree.getSize() << endl;
    tree.preOrder();            //前序遍历
    tree.inOrder();             //中序遍历
    tree.postOrder();           //后序遍历
    tree.distroy();             //摧毁树
    system("pause");
}

 

二叉树

标签:clu   creat   bit   abd   pause   方式   cti   添加   ons   

原文地址:https://www.cnblogs.com/guoshuai-ouc/p/BinaryTree.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!