Btree.h
先定义树的节点:
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};
struct Btree
{
TreeNode *root;
public:
void buildTree(vector<int>&a);
void Layerorder(TreeNode *root);//层序遍历
void PreorderFor(TreeNode *root);//前序遍历非递归
void InorderFor(TreeNode *root);//中序遍历非递归
void PostorderFor(TreeNode *root);//后序遍历非递归
void Preorder(TreeNode *root);//前序遍历(递归)
void Inorder(TreeNode *root);//中序遍历(递归)
void Postorder(TreeNode *root);//后序遍历(递归)
int heightofTree(TreeNode *root);//二叉树的高度
TreeNode *getRoot();//增强代码封装性
void destroyTree(TreeNode *root);//销毁二叉树
};
Btree.cpp
void Btree::buildTree(vector<int>&a)//构建二叉搜索树
{
if (a.size()== 0)
{
return;
}
root = new TreeNode(a[0]);
for (int i = 1; i < a.size(); i++)
{
TreeNode *newNode = new TreeNode(a[i]);
TreeNode *node = root;
while (node)
{
if (a[i] < node->val)
{
if (node->left==NULL)
{
node->left=newNode;
break;
}
node = node->left;
}
else
{
if (node->right==NULL)
{
node->right = newNode;
break;
}
node = node->right;
}
}
}
}
void Btree::Layerorder(TreeNode *root)//层次遍历(换行)
{
if (root == NULL)
{
return;
}
TreeNode *last = root;
TreeNode *nlast = NULL;
queue<TreeNode *>que;
que.push(root);
cout << endl;
while (!que.empty())
{
TreeNode *node = que.front();
cout << node->val<<" ";
que.pop();
if (node->left)
{
que.push(node->left);
nlast = node->left;
}
if (node->right)
{
que.push(node->right);
nlast = node->right;
}
if (node == last)
{
cout << endl;
last = nlast;
}
}
}
void Btree::Preorder(TreeNode *root)//前序遍历递归实现(根左右)
{
if (root == NULL)
{
return;
}
cout << root->val << " ";
Preorder(root->left);
Preorder(root->right);
}
void Btree::Inorder(TreeNode *root)//中序遍历,递归实现(左根右)
{
if (root == NULL)
{
return;
}
Inorder(root->left);
cout << root->val << " ";
Inorder(root->right);
}
void Btree::Postorder(TreeNode *root)//后序遍历递归实现(左右根)
{
if (root==NULL)
{
return;
}
Postorder(root->left);
Postorder(root->right);
cout << root->val << " ";
}
void Btree::PreorderFor(TreeNode *root)//前序遍历,非递归实现
{
if (root == NULL)
{
return;
}
stack<TreeNode *>stk;
stk.push(root);
cout << "前序遍历:";
while (!stk.empty())
{
TreeNode *node = stk.top();
stk.pop();
cout << node->val << " ";
if (node->right)
{
stk.push(node->right);
}
if (node->left)
{
stk.push(node->left);
}
}
cout<< endl;
}
void Btree::InorderFor(TreeNode *root)//中序遍历,非递归实现
{
if (root == NULL)
{
return;
}
cout << "中序遍历:";
stack<TreeNode *>stk;
TreeNode *cur = root;
while (!stk.empty() || cur)
{
while(cur)
{
stk.push(cur);
cur = cur->left;
}
TreeNode *node = stk.top();
stk.pop();
cout <<node->val<< " ";
cur = node->right;
}
}
void Btree::PostorderFor(TreeNode *root)//后序遍历,非递归实现
{
if (root == NULL)
{
return;
}
cout << endl;
cout << "后序遍历:";
stack<TreeNode *>s1;
stack<TreeNode *>s2;
s1.push(root);
while (!s1.empty())
{
TreeNode *cur = s1.top();
s1.pop();
s2.push(cur);
if(cur->left)
s1.push(cur->left);
if(cur->right)
s1.push(cur->right);
}
while (!s2.empty())
{
TreeNode *node = s2.top();
cout << node->val << " ";
s2.pop();
}
}
int Btree::heightofTree(TreeNode *root)//二叉树的高度(后序遍历思想)
{
if (root == NULL)
{
return 0;
}
int lheight = heightofTree(root->left);
int rheight = heightofTree(root->right);
int max = lheight > rheight ? lheight : rheight;
return max + 1;
}
TreeNode *Btree::getRoot()
{
return root;
}
void Btree::destroyTree(TreeNode *root)//销毁二叉树(后序遍历思想)
{
if (root == NULL)
{
return;
}
destroyTree(root->left);
destroyTree(root->right);
delete root;
root = NULL;
}
main.cpp
int main()
{
vector<int>a ={3,1,6,2,5,8,7,9,4};
Btree b;
b.buildTree(a);//建树
b.PreorderFor(b.getRoot());//非递归前序
//b.Preorder(b.getRoot());//递归前序
b.InorderFor(b.getRoot());//非递归中序
//b.Inorder(b.getRoot());//递归中序
b.PostorderFor(b.getRoot());//非递归后序
//b.Postorder(b.getRoot());//递归后序
b.Layerorder(b.getRoot());//层次遍历
cout << b.heightofTree(b.getRoot());//树的高度
b.destroyTree(b.getRoot());//销毁树
return 0;
}