C++数据结构--二叉树的前序遍历,中序遍历,后序遍历

1.二叉树的抽象模型和相应的node对象表示
 



2.遍历二叉树
  Example:对下图给出的二叉树进行前,中,后序遍历结果
   




  
  


  
  本示例所用到的二叉树
 
  实现代码:






template<typename T>
class node
{
   public:
      T val;
//节点值
 node<T>* left; //左节点 
 node<T>* right; //右节点
 
 node():val(T()),left(nullptr),right(nullptr){}
 node(T v, node<T>* l=nullptr, node<T>* r=nullptr):val(v),left(l),right(r){}
};






node<char>*   createBTree()   //构建一棵示例二叉树 
   {
    node<char>* d=new node<char>('D');
    node<char>* e=new node<char>('E');
    node<char>* b=new node<char>('B',nullptr,d);
    node<char>* c=new node<char>('C',e,nullptr);
    node<char>* a=new node<char>('A',b,c);
   
   }
   
   void preOrderTravel(node<char>* root) //前序遍历 
{
if(!root) //根节点为空时结束递归 
{
  return;
}
else
{
cout<<root->val<<ends;
preOrderTravel(root->left);  //递归遍历左子树
        preOrderTravel(root->right); //递归遍历右子树
   




void inOrderTravel(node<char>* root)
{
if(!root) //根节点为空时结束递归 
{
  return;
}
else
{
inOrderTravel(root->left);
cout<<root->val<<ends;
inOrderTravel(root->right);
  





   void postOrderTravel(node<char>* root)
{
if(!root) //根节点为空时结束递归 
{
  return;
}
else
{
postOrderTravel(root->left);
postOrderTravel(root->right);
   cout<<root->val<<ends;







int main()
{
    node<char>* root=createBTree();
cout<<"preOrderTravel :" ;
    preOrderTravel(root);
    cout<<endl;
    
    cout<<"inOrderTravel :" ;
    inOrderTravel(root);
    cout<<endl;
    
    cout<<"postOrderTravel :" ;
    postOrderTravel(root);
    cout<<endl;
return 0;
}






  运行结果:
   




   解析:以后序遍历为例,前序和中序遍历类似
 
要实现输入一棵二叉树前序遍历中序遍历,然后输出后序遍历,可以按照以下步骤进行: 1. 首先,我们需要定义一个二叉树的结构,包括节点的值和左右子节点的指针。 2. 接下来,根据输入的前序遍历中序遍历构建二叉树。具体步骤如下: - 前序遍历的第一个元素是根节点的值,我们可以将其作为根节点,并创建一个新的节点。 - 在中序遍历中找到根节点的位置,将中序遍历分为左子树和右子树。 - 根据左子树和右子树的长度,将前序遍历分为左子树和右子树。 - 递归地构建左子树和右子树。 3. 最后,通过后序遍历输出二叉树的节点值。具体步骤如下: - 后序遍历先输出左子树的节点值,再输出右子树的节点值,最后输出根节点的值。 - 递归地进行后序遍历。 下面是C++代码示例: ```cpp #include <iostream> #include <vector> using namespace std; struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) { if (preorder.empty() || inorder.empty()) { return NULL; } int rootVal = preorder[0]; TreeNode* root = new TreeNode(rootVal); int rootIndex = 0; while (inorder[rootIndex] != rootVal) { rootIndex++; } vector<int> leftPreorder(preorder.begin() + 1, preorder.begin() + rootIndex + 1); vector<int> leftInorder(inorder.begin(), inorder.begin() + rootIndex); vector<int> rightPreorder(preorder.begin() + rootIndex + 1, preorder.end()); vector<int> rightInorder(inorder.begin() + rootIndex + 1, inorder.end()); root->left = buildTree(leftPreorder, leftInorder); root->right = buildTree(rightPreorder, rightInorder); return root; } void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); postorderTraversal(root->right); cout << root->val << " "; } int main() { vector<int> preorder = {1, 2, 4, 5, 3, 6}; vector<int> inorder = {4, 2, 5, 1, 6, 3}; TreeNode* root = buildTree(preorder, inorder); cout << "后序遍历结果:"; postorderTraversal(root); cout << endl; return 0; } ``` 运行以上代码,输出结果为后序遍历的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值