本节学习了二叉树,二叉树节点的定义,插入,如何建二叉树,四种遍历方式,怎么根据给定的前序/后序+中序序列来建树等等
可以结合PATA1020/1086/1102进行理解
//A1020 A1086 A1102
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node{
int data;
int level;//记录结点层次
node* lchild;
node* rchild;
};
node* newNode(int v);
void modify_x(node *root,int x,int newdata);
node* search(node *root,int x);
void insert(node* &root,int x);
node *create(int data[],int n);
void preorder(node* root);
void inorder(node* root);
void postorder(node* root);
node* create1(int postl,int postr,int inl,int inr);
node* create2(int prel,int prer,int inl,int inr);
int main()
{
return 0;
}
node* newNode(int v)
{
node* Node = new node;
Node->data=v;
Node->lchild=Node->rchild;
return Node;
}
void modify_x(node *root,int x,int newdata)
{
if(root==NULL)
return;
if(root->data==x)
root->data=newdata;
modify_x(root->lchild,x,newdata);
modify_x(root->rchild,x,newdata);
}
node* search(node *root,int x)
{
if(root==NULL)
return NULL;
if(root->data==x)
return root;
search(root->lchild,x);
search(root->rchild,x);
}
void insert(node* &root,int x)//这里必须要用引用,因为修改的是root本身
{
if(root==NULL)
{
root=newNode(x);
return;
}
if(x<root->data)
insert(root->lchild,x);
else
insert(root->rchild,x);
}
node *create(int data[],int n)
{
node* root=NULL;
for(int i=0;i<n;i++)
insert(root,data[i]);
return root;
}
void preorder(node* root)
{
if(root==NULL)
return;
printf("%d\n",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
void inorder(node* root)
{
if(root==NULL)
return;
inorder(root->lchild);
printf("%d\n",root->data);
inorder(root->rchild);
}
void postorder(node* root)
{
if(root==NULL)
return;
postorder(root->lchild);
postorder(root->rchild);
printf("%d\n",root->data);
}
void levelorder(node* root)
{
queue<node*> q;
q.push(root);
root->level=1;
while(!q.empty())
{
node *now=q.front();
q.pop();
printf("%d",now->data);
if(now->lchild!=NULL)
{
now->lchild->level=now->level+1;
q.push(now->lchild);
}
if(now->rchild!=NULL)
{
now->rchild->level=now->level+1;
q.push(now->rchild);
}
}
}
node* create1(int postl,int postr,int inl,int inr)
{
//给出中序和后序序列,建树
//post和in数组由全局变量给出,n是结点数量
//当前二叉树的后序序列区间为[postl,postr],中序为[inl,inr]
if(postl>postr)
return NULL;
node* root=new node;//建立新结点用来存放当前二叉树的根节点
root->data=post[postr];//后序序列的最后一个结点就是当前二叉树的根节点
int k;
for(k=inl;k<=inr;k++)
{
if(in[k]==post[postr])
break;
}//找到根节点在中序序列中的位置,记为k
int numleft=k-inl;//左子树的结点个数
root->lchild=create1(postl,postl+numleft-1,inl,k-1);
//返回右子树的根节点,赋值给root的右指针
root->rchild=create1(postl+numleft,postr-1,k+1,inr);
return root;
}
node* create2(int prel,int prer,int inl,int inr)
{
//给出中序和后序序列,建树
//post和in数组由全局变量给出,n是结点数量
//当前二叉树的先序序列区间为[prel,prer],中序为[inl,inr]
if(prel>prer)
return NULL;
node* root=new node;//建立新结点用来存放当前二叉树的根节点
root->data=pre[prel];//先序序列的第一个结点就是当前二叉树的根节点
int k;
for(k=inl;k<=inr;k++)
{
if(in[k]==pre[prel])
break;
}//找到根节点在中序序列中的位置,记为k
int numleft=k-inl;//左子树的结点个数
root->lchild=create2(prel+1,prel+numleft,inl,k-1);
//返回右子树的根节点,赋值给root的右指针
root->rchild=create2(prel+numleft+1,prer,k+1,inr);
return root;
}