目录
扫描二维码关注公众号,回复:
13434207 查看本文章
一.树与二叉树
1.建二叉树
struct node{
typename data;
node* lchild;
node* rchild;
};
node* root=null;
2.新建节点
struct node{
int data;
node* lchild;
node* rchild;
};
node* root=null;
node* newnode(int v){
node* nodes=new node;
nodes->data=v;
nodes->lchild=nodes->rchild=null;
return node;
}
3.节点的查找与修改
void search(node *root,int x,int newdata){
if(root==NULL){
return;
}
if(root->data==x){
root->data=newdata;
}
search(root->lchild,x,newdata);
search(root->rchild,x,newdata);
}
4.节点的插入
void insert(node* &root,int x){
if(root==NULL){
root = newnode(x);
return;
}
if(由二叉树的性质,x应该插于左子树){
insert(root->lchild,x);
}else{
insert(root->rchild);
}
}
5.二叉树的创建
node* create(int data[],int n){
node* root = NULL;
for(int i=0;i<n;i++){
insert(root,data[i]);
}
return root;
}
二.二叉树的遍历
1.先序遍历
void preorder(node* root){
if(root==NULL){
return;
}
printf("%d\n",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
2.中序遍历
void inorder(node* root){
if(root==NULL){
return;
}
inorder(root->lchild);
printf("%d\n",root->data);
inorder(root->rchild);
}
3.后序遍历
void postorder(node* root){
if(root==null){
return;
}
postorder(root->lchild);
postorder(root->rchild);
printf("%d\n",root->data);
}
4.层序遍历
void layerorder(node* root){
queue<node*> q;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
printf("%d",now->data);
if(now->lchild!=NULL) q.push(now->lchild);
if(now->rchild!=NULL) q.push(now->rchild);
}
}
5.记录层数
void layerorder(node* root){
queue<node*> q;
root->layer=1;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
printf("%d",now->data);
if(now->lchild!=NULL){
now->lchild->layer=now->layer+1;
q.push(now->lchild);
}
if(now->rchild!=NULL){
now->rchild->layer=now->layer+1;
q.push(now->rchild);
}
}
}
6.前序中序建树
node* create(int prel,int prer,int inl,int r){
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;
}
}
int numleft = k-inl;
root->lchild=create(prel+1,prel+numleft,inl,k-1);
root->rchild=create(prel+numleft+1,prer,k+1,inr);
return root;
}
7.Tree Traversals
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=50;
struct node{
int data;
node* lchild;
node* rchild;
};
int pre[maxn],in[maxn],post[maxn];
int n;
node* create(int postl,int postr,int inl,int 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;
}
}
int numleft=k-inl;
root->lchild=create(postl,postl+numleft-1,inl,k-1);
root->rchild=create(postl+numleft,postr-1,k+1,inr);
return root;
}
int num=0;
void bfs(node* root){
queue<node*> q;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
printf("%d",now->data);
num++;
if(num<n) printf(" ");
if(now->lchild!=NULL) q.push(now->lchild);
if(now->rchild!=NULL) q.push(now->rchild);
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&post[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&in[i]);
}
node* root=create(0,n-1,0,n-1);
bfs(root);
return 0;
return 0;
}
8.二叉树的静态实现
const int maxn=100;
//节点的定义
struct node{
int data;
int lchild;
int rchild;
}nodes[maxn];
//节点的静态指定
int index=0;
int newnode(int v){
nodes[index].data=v;
nodes[index].lchild=-1;
nodes[index].rchild=-1;
return index++;
}
//查找及修改,root为下标
void search(int root,int x,int newdata){
if(root==-1) return;
if(nodes[root].data==x) nodes[root].data=newdata;
search(nodes[root].lchild,x,newdata);
search(nodes[root].rchild,x,newdata);
}
//插入
void insert(int &root,int x){
if(root==-1){
root=newnode(x);
return;
}
if(插左子树) insert(nodes[root].lchild,x);
ekse insert(nodes[root].rchild,x);
}
//二叉树的建立
int create(int data[],int n){
int root=-1;
for(int i=0;i<n;i++){
insert(root,data[i]);
}
return root;
}
//先序遍历
void preorder(int root){
if(root==-1){
return;
}
printf("%d\n",nodes[root].data);
preorder(nodes[root].lchild);
preorder(nodes[root].rchild);
}
//中序遍历
void inorder(int root){
if(root==-1) return;
inorder(nodes[root].lchild);
printf("%d\n",nodes[root].data);
inorder(nodes[root].rchild);
}
//后序遍历
void postorder(int root){
if(root==-1) return;
postorder(nodes[root].lchild);
postorder(nodes[root].rchild);
printf("%d\n",nodes[root].data);
}
//层序遍历
void layerorder(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int now=q.front();
q.pop();
printf("%d ",nodes[now].data);
if(nodes[now].lchild!=-1) q.push(nodes[now].lchild);
if(nodes[now].rchild!=-1) q.push(nodes[now].rchild);
}
}
9.Tree Traversals Again
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=50;
struct node{
int data;
node* lchild;
node* rchild;
};
int pre[maxn],in[maxn],post[maxn];
int n;
node* create(int prel,int prer,int inl,int 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;
}
}
int numleft=k-inl;
root->lchild=create(prel+1,prel+numleft,inl,k-1);
root->rchild=create(prel+numleft+1,prer,k+1,inr);
return root;
}
int num=0;
void postorder(node* root){
if(root==NULL) return;
postorder(root->lchild);
postorder(root->rchild);
printf("%d",root->data);
num++;
if(num<n) printf(" ");
}
int main(){
scanf("%d",&n);
char str[5];
stack<int> st;
int x,preindex=0,inindex=0;
for(int i=0;i<2*n;i++){
scanf("%s",str);
if(strcmp(str,"Push")==0){
scanf("%d",&x);
pre[preindex++] = x;
st.push(x);
}else{
in[inindex++] =st.top();
st.pop();
}
}
node* root=create(0,n-1,0,n-1);
postorder(root);
return 0;
}
10.Invert a Binary Tree
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=110;
struct node{
int lchild,rchild;
}nodes[maxn];
bool notroot[maxn] ={false};
int n,num=0;
void print(int id){
printf("%d",id);
num++;
if(num<n) printf(" ");
else printf("\n");
}
void inorder(int root){
if(root==-1) return;
inorder(nodes[root].lchild);
print(root);
inorder(nodes[root].rchild);
}
void bfs(int root){
queue<int> q;
q.push(root);
while(!q.empty()){
int now=q.front();
q.pop();
print(now);
if(nodes[now].lchild!=-1) q.push(nodes[now].lchild);
if(nodes[now].rchild!=-1) q.push(nodes[now].rchild);
}
}
void postorder(int root){
if(root==-1){
return;
}
postorder(nodes[root].lchild);
postorder(nodes[root].rchild);
swap(nodes[root].lchild,nodes[root].rchild);
}
int strtonum(char c){
if(c=='-') return -1;
else{
notroot[c-'0']=true;
return c-'0';
}
}
int findroot(){
for(int i=0;i<n;i++){
if(notroot[i]==false){
return i;
}
}
}
int main(){
char lchild,rchild;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%*c%c %c",&lchild,&rchild);
nodes[i].lchild=strtonum(lchild);
nodes[i].rchild=strtonum(rchild);
}
int root = findroot();
postorder(root);
bfs(root);
num=0;
inorder(root);
return 0;
}