数据结构:(代码篇 000)二叉树的存储结构和基本操作

目录

一.静态存储结构

1.节点的生成:

2.查找&修改

3. 插入

4.创建二叉树

5.四种遍历

5.1 先序遍历

5.2 中序遍历

5.3 后序遍历

5.4 层次遍历

6.完整的代码

二.动态存储结构

1.新生成一个节点

2.查找&修改

3.插入

4.生成树

5.四种遍历

6.完整的代码:


一.静态存储结构

用数组来存二叉树,指向左右孩子的节点不用指针,而是用int,表示左右子树的根节点在数组中的下标。

struct node{
	int data;
	int lchild;
	int rchild;
}Node[maxn];  // maxn表示节点范围 

1.节点的生成:

int index=0;
int newNode(int v){    
	Node[index].data=v;   //分配一个数组单元元素给新的节点,index为其下标 
	Node[index].lchild=-1;  // 以-1表示空,数组范围为0~maxn-1 
	Node[index].rchild=-1;
	return index++;
} 

2.查找&修改

void search(int root,int x,int newdata){
	if(root==-1){    // 死胡同 
		return ;
	}
	if(Node[root].data==x){    // 找到了 
		Node[root].data=newdata;
		return ;
	}
	search(Node[root].lchild,x,newdata);
	search(Node[root].rchild,x,newdata);
} 

3. 插入

void insert(int &root,int x){
	if(root==-1){
		root=newNode(x);
		return ;
	}
	if(Node[root].data>x){    // 这里我按 二叉搜索树(binary search tree)的性质,插入节点 
		insert(Node[root].lchild,x);
	}
	else{
		insert(Node[root].rchild,x);
	}
}

4.创建二叉树

int create(int data[],int len){  // 生成二叉树 
	int root=-1;
	for(int i=0;i<len;i++){
		insert(root,data[i]);
	}
	return root;
}

5.四种遍历

先序、中序、后序遍历一般用DFS实现,而层次遍历用BFS实现

5.1 先序遍历

void preorder(int root){      // 先序遍历 
	if(root==-1){
		return ;
	}
	printf("%d ",Node[root].data);
	preorder(Node[root].lchild);
	preorder(Node[root].rchild);
} 

5.2 中序遍历

void inorder(int root){       // 中序遍历 
	if(root==-1){
		return ;
	}
	
	inorder(Node[root].lchild);
	printf("%d ",Node[root].data);
	inorder(Node[root].rchild);
}

5.3 后序遍历

void postorder(int root){     // 后序遍历 
	if(root==-1){
		return ;
	}
	postorder(Node[root].lchild);
	postorder(Node[root].rchild);
	printf("%d ",Node[root].data);
} 

5.4 层次遍历

void layerorder(int root){    // 层次遍历 
	queue<int> s;
	
	s.push(root);
	int temp=-1;
	while(!s.empty()){
		temp=s.front();
		s.pop();
		printf("%d ",Node[temp].data);
		if(Node[temp].lchild!=-1) 
		    s.push(Node[temp].lchild);
		if(Node[temp].rchild!=-1)
		    s.push(Node[temp].rchild);
	}
}

6.完整的代码

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 1000
struct node{
	int data;
	int lchild;
	int rchild;
}Node[maxn];  // maxn表示节点范围 
int index=0;
int newNode(int v){    
	Node[index].data=v;   //分配一个数组单元元素给新的节点,index为其下标 
	Node[index].lchild=-1;  // 以-1表示空,数组范围为0~maxn-1 
	Node[index].rchild=-1;
	return index++;
} 
void search(int root,int x,int newdata){
	if(root==-1){    // 死胡同 
		return ;
	}
	if(Node[root].data==x){    // 找到了 
		Node[root].data=newdata;
		return ;
	}
	search(Node[root].lchild,x,newdata);
	search(Node[root].rchild,x,newdata);
} 
void insert(int &root,int x){
	if(root==-1){
		root=newNode(x);
		return ;
	}
	if(Node[root].data>x){    // 这里我按 二叉搜索树(binary search tree)的性质,插入节点 
		insert(Node[root].lchild,x);
	}
	else{
		insert(Node[root].rchild,x);
	}
}
int create(int data[],int len){  // 生成二叉树 
	int root=-1;
	for(int i=0;i<len;i++){
		insert(root,data[i]);
	}
	return root;
} 
void preorder(int root){      // 先序遍历 
	if(root==-1){
		return ;
	}
	printf("%d ",Node[root].data);
	preorder(Node[root].lchild);
	preorder(Node[root].rchild);
} 
void inorder(int root){       // 中序遍历 
	if(root==-1){
		return ;
	}
	
	inorder(Node[root].lchild);
	printf("%d ",Node[root].data);
	inorder(Node[root].rchild);
} 
void postorder(int root){     // 后序遍历 
	if(root==-1){
		return ;
	}
	postorder(Node[root].lchild);
	postorder(Node[root].rchild);
	printf("%d ",Node[root].data);
} 
void layerorder(int root){    // 层次遍历 
	queue<int> s;
	
	s.push(root);
	int temp=-1;
	while(!s.empty()){
		temp=s.front();
		s.pop();
		printf("%d ",Node[temp].data);
		if(Node[temp].lchild!=-1) 
		    s.push(Node[temp].lchild);
		if(Node[temp].rchild!=-1)
		    s.push(Node[temp].rchild);
	}
}
//测试数据: 10 9 8 7 4 5 6 3 2 1 
int main(){
	int data[10];
	int i,j;
	int root=-1;
	for(i=0;i<10;i++)
	scanf("%d",&data[i]); 
	root=create(data,10);   // 记录根节点的下标 
	cout<<root<<endl;
	printf("中序遍历:");
	inorder(root); 
	cout<<endl<<"请输入要修改哪个节点及相应的节点值:"<<endl;
	cin>>i>>j;
	search(root,i,j);
	cout<<endl;
	printf("中序遍历:");
	inorder(root); 
	cout<<endl;
	printf("先序遍历:");
	preorder(root); 
	cout<<endl;
	printf("后序遍历:");
	postorder(root); 
	cout<<endl;
	printf("层序遍历:");
	layerorder(root); 
	return 0;
}

二.动态存储结构

用指针分别指向左右子树

struct node{
	int data;
	node * lchild;
	node * rchild;
};

1.新生成一个节点

node* newNode(int v){    
	node * Node=new node;
	Node->data=v;
	Node->lchild=Node->rchild=NULL;
	return Node;
} 

2.查找&修改

void search(node* root,int x,int newdata){
	if(root==NULL){    // 死胡同 
		return ;
	}
	if(root->data==x){    // 找到了 
		root->data=newdata;
		return ;
	}
	search(root->lchild,x,newdata);
	search(root->rchild,x,newdata);
} 

3.插入

void insert(node* &root,int x){
	if(root==NULL){
		root=newNode(x);
		return ;
	}
	if(root->data>x){    // 这里我按 二叉搜索树(binary search tree)的性质,插入节点 
		insert(root->lchild,x);
	}
	else{
		insert(root->rchild,x);
	}
}

4.生成树

node* create(int data[],int len){  // 生成二叉树 
	node * root=NULL; 
	for(int i=0;i<len;i++){
		insert(root,data[i]);
	}
	return root;
}

5.四种遍历

void preorder(node* root){      // 先序遍历 
	if(root==NULL){
		return ;
	}
	printf("%d ",root->data);
	preorder(root->lchild);
	preorder(root->rchild);
} 
void inorder(node* root){       // 中序遍历 
	if(root==NULL){
		return ;
	}
	inorder(root->lchild);
	printf("%d ",root->data);
	inorder(root->rchild);
} 
void postorder(node* root){     // 后序遍历 
	if(root==NULL){
		return ;
	}
	postorder(root->lchild);
	postorder(root->rchild);
	printf("%d ",root->data);
} 
void layerorder(node* root){    // 层次遍历 
	queue<node*> s;
	s.push(root);
	node* temp=NULL;
	while(!s.empty()){
		temp=s.front();
		s.pop();
		printf("%d ",temp->data);
		if(temp->lchild!=NULL) 
		    s.push(temp->lchild);
		if(temp->rchild!=NULL)
		    s.push(temp->rchild);
	}
}

6.完整的代码:

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define NULL 0
struct node{
	int data;
	node * lchild;
	node * rchild;
};
node * Root=NULL;
node* newNode(int v){    
	node * Node=new node;
	Node->data=v;
	Node->lchild=Node->rchild=NULL;
	return Node;
} 
void search(node* root,int x,int newdata){
	if(root==NULL){    // 死胡同 
		return ;
	}
	if(root->data==x){    // 找到了 
		root->data=newdata;
		return ;
	}
	search(root->lchild,x,newdata);
	search(root->rchild,x,newdata);
} 
void insert(node* &root,int x){
	if(root==NULL){
		root=newNode(x);
		return ;
	}
	if(root->data>x){    // 这里我按 二叉搜索树(binary search tree)的性质,插入节点 
		insert(root->lchild,x);
	}
	else{
		insert(root->rchild,x);
	}
}
node* create(int data[],int len){  // 生成二叉树 
	node * root=NULL; 
	for(int i=0;i<len;i++){
		insert(root,data[i]);
	}
	return root;
} 
void preorder(node* root){      // 先序遍历 
	if(root==NULL){
		return ;
	}
	printf("%d ",root->data);
	preorder(root->lchild);
	preorder(root->rchild);
} 
void inorder(node* root){       // 中序遍历 
	if(root==NULL){
		return ;
	}
	inorder(root->lchild);
	printf("%d ",root->data);
	inorder(root->rchild);
} 
void postorder(node* root){     // 后序遍历 
	if(root==NULL){
		return ;
	}
	postorder(root->lchild);
	postorder(root->rchild);
	printf("%d ",root->data);
} 
void layerorder(node* root){    // 层次遍历 
	queue<node*> s;
	s.push(root);
	node* temp=NULL;
	while(!s.empty()){
		temp=s.front();
		s.pop();
		printf("%d ",temp->data);
		if(temp->lchild!=NULL) 
		    s.push(temp->lchild);
		if(temp->rchild!=NULL)
		    s.push(temp->rchild);
	}
}
//测试数据: 10 9 8 7 4 5 6 3 2 1 
int main(){
	int data[10];
	int i,j;
	
	for(i=0;i<10;i++)
	scanf("%d",&data[i]); 
	Root=create(data,10);  
	
	printf("中序遍历:");
	inorder(Root); 
	cout<<endl<<"请输入要修改哪个节点及相应的节点值:"<<endl;
	cin>>i>>j;
	search(Root,i,j);
	cout<<endl;
	printf("中序遍历:");
	inorder(Root); 
	cout<<endl;
	printf("先序遍历:");
	preorder(Root); 
	cout<<endl;
	printf("后序遍历:");
	postorder(Root); 
	cout<<endl;
	printf("层序遍历:");
	layerorder(Root); 
	return 0;
}

如果有错,请评论。

发布了374 篇原创文章 · 获赞 101 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_41325698/article/details/104059668