二叉树生成和遍历

//树结点

typedef struct node *pointer;
struct node{
    int data;
    pointer lchild , rchild;
};
typedef pointer bitree;

二序生成二叉树

前序、中序 生成二叉树

//前序、中序 生成二叉树
bitree prein_creat(datatype Pre[ ],int ps, int pe, datatype In[ ],int is, int ie)
{    //先序和中序建立二叉树,ps、pe、is、ie分别为区间的起止节点
	bitree t;
	if(ps>pe) 
        		return NULL;
	t=new node;
	t->data=Pre[ps];
	int i=is;
	while(In[i]!=Pre[ps]) i++;
	t->lchild = prein_creat(Pre , ps+1 , ps+i-is , In , is , i-1);
	t->rchild = prein_creat(Pre , ps+i-is+1 , pe , In , i+1 , ie);
	return t;
}

 后序、中序 生成二叉树

//后序、中序 生成二叉树
bitree postin_creat(datatype Post[ ],int ps, int pe, datatype In[ ],int is, int ie)
{    //先序和中序建立二叉树,ps、pe、is、ie分别为区间的起止节点
	bitree t;
	if(ps>pe) 
		return NULL;
	t=new node;
	t->data=Post[pe];
	int i=is;
	while(In[i]!=Post[pe]) i++;
	t->lchild = postin_creat(Post , ps , ps+i-is-1 , In , is , i-1 );
	t->rchild = postin_creat(Post, ps+i-is , pe-1 , In , i+1 , ie );
	return t;
}

递归遍历二叉树

前序遍历

//前序遍历
void preorder(bitree t) {	//先根遍历
   if(t==NULL) return;
   cout<data<lchild);	//先根遍历左子树
   preorder(t−>rchild);	//先根遍历右子树
}

 中序遍历

//中序遍历
void inorder(bitree t) {	//中根遍历
   if(t==NULL) return;
   inorder(t−>lchild);	//中根遍历左子树
   cout<data<rchild);	//中根遍历右子树
}

后序遍历

//后序遍历
void postorder(bitree t) { //后根遍历
   if(t==NULL) return;
   postorder(t−>lchild);	  //后根遍历左子树
   postorder(t−>rchild);	  //后根遍历右子树
   cout<data<

非递归遍历二叉树

前序遍历

//前序遍历
void preOrder(bitree root)
{
    stack s;
    while (!s.empty() || root)
    {
        while (root)
        {
            cout << root->data << " ";
            s.push(root);
            root = root->left;
        }
        root = s.top(); s.pop();
        root = root->right;
    }
    cout << endl;
}

中序遍历

//中序遍历
void inOrder(bitree root)
{
    stack s;
    while (!s.empty() || root)
    {
        while (root)
        {
            s.push(root);
            root = root->left;
        }
        root = s.top(); s.pop();
        cout << root->data << " ";
        root = root->right;
    }
    cout << endl;
}

后序遍历

//后序遍历
//法一:
void postOrder(bitree root)
{
    stack s;
    vector vec;
    while (!s.empty() || root)
    {
        while (root)
        {
            vec.push_back(root->data);
            s.push(root);
            root = root->right;
        }
        root = s.top(); s.pop();
        root = root->left;
    }
    for (int i = vec.size() - 1; i >= 0; i--)
        cout << vec[i] << " ";
}
//法二:
void postOrder(bitree root)
{
    stack s;
    bitree p = NULL;
    while(!s.empty() || root) {
        while (root){
            s.push(root);
            root = root->left;
        }
        root = s.top();
        if (!root->right || root->right == p) {
            cout << root->data << " ";
            s.pop();
            p = root;
            root = NULL;
        }
        else
            root = root->right;
    }
    cout << endl;
}

力扣练习

前序遍历:https://leetcode-cn.com/problems/binary-tree-preorder-traversal

中序遍历:https://leetcode-cn.com/problems/binary-tree-inorder-traversal

后序遍历:https://leetcode-cn.com/problems/binary-tree-postorder-traversal

层序遍历二叉树

//层序遍历
void sequence(bitree t)
{
    queue que;
    if(t==NULL) return ;
    que.push(t);
    while(!que.empty())
    {
        pointer p = que.front();
        que.pop();
        if(p->lchild) que.push(p->lchild);
        if(p->rchild) que.push(p->rchild);
        cout << p->data << " ";
      }
}

你可能感兴趣的:(算法与数据结构,c++,c语言)