二叉树(链式存储和数组存储) 代码分别来自B站up主 正月点灯笼和挑战程序设计

链式存储

插入的时候其实是按照二叉搜索树的规则进行插入的

Tree 结构记录的是这个数的根节点

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
{
	int data;
	struct node* left;
	struct node* right; 
}Node;
typedef struct 
{
	Node* root;
}Tree;
void insert(Tree* tree,int value)
{
	Node* node=new Node;
	node->data=value;
	node->left=NULL;
	node->right=NULL;
	if(tree->root==NULL)
	tree->root=node;
	else
	{
		Node* temp=tree->root;
		while(temp!=NULL)
		{
			if(temp->data>value)
			{
				if(temp->left==NULL)
				{
					temp->left=node;
					return; 
				}
				else
				temp=temp->left;		
			}
			else
			{
				if(temp->right==NULL)
				{
					temp->right=node;
					return;
				}
				else
				temp=temp->right;
			}
		}
	}
}
void preorder(Node *node)
{
	if(node!=NULL)
	{
		cout<<node->data<<" ";
		preorder(node->left);
		preorder(node->right);
	}
}
void inorder(Node *node)
{
	if(node!=NULL)
	{
		inorder(node->left);
		cout<<node->data<<" ";	
		inorder(node->right);
	}
}
void postorder(Node *node)
{
	if(node!=NULL)
	{
		postorder(node->left);
		postorder(node->right);
		cout<<node->data<<" ";
	}
}
int get_height(Node* node)
{
	 if(node==NULL)
	 return 0;
	 else
	 {
	 	int left_h=get_height(node->left);
	 	int right_h=get_height(node->right);
	 	int maxm=left_h;
	 	if(right_h>maxm)
			maxm=right_h; 	
			return maxm+1;
	 }
} 
int get_maxnum(Node* node)
{
	if(node==NULL)
	return -1;
	else
	{
		int m1=get_maxnum(node->left);
		int m2=get_maxnum(node->right);
		int m3=node->data;
		int maxm=m1;
		if(m2>maxm)
		maxm=m2;
		if(m3>maxm)
		maxm=m3;
		return maxm;
	} 
}
int main()
{
	int a[]={6,3,8,2,5,1,7};
	Tree tree;
	tree.root=NULL;
	for(int i=0;i<7;i++)
	insert(&tree,a[i]); 
	cout<<"先序遍历 : ";
	preorder(tree.root);
	cout<<endl;
	cout<<"中序遍历 : "; 
	inorder(tree.root);
	cout<<endl ;
	cout<<"后序遍历 : ";
	postorder(tree.root);
	cout<<endl;
	int h=get_height(tree.root);
	cout<<h<<endl;
	int m=get_maxnum(tree.root);
	cout<<m<<endl;
	return 0;	
}

结构体数组存储

注释已经很全了。。

9
1 2 3
2 4 5
3 6 7
4 8 9
5 -1 -1
6 -1 -1
7 -1 -1
8 -1 -1
9 -1 -1

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=10000+10;
struct Node
{
	int P,L,R;	
}node[maxn];
void init(int n)
{
	for(int i=1;i<=n;i++)
		node[i].P=-1;
}
void preorder(int u)
{
	if(u==-1) 
		return ;
	printf("%d ",u);
	preorder(node[u].L);
	preorder(node[u].R);	
}
void inorder(int u)
{
	if(u==-1)    //当前节点不存在 
		return ;
	inorder(node[u].L);
	printf("%d ",u);
	inorder(node[u].R);
}
void postorder(int u)
{
	if(u==-1)
		return ;
	postorder(node[u].L);
	postorder(node[u].R);
	printf("%d ",u);
}
int main()
{
	freopen("in.txt","r",stdin);
	int n,v,l,r,root;
	scanf("%d",&n);
	init(n);             // 将 n 个结点的父节点都初始化为 -1  
	for(int i=0;i<n;i++)
	{
		scanf("%d %d %d",&v,&l,&r);
		node[v].L=l;    // 若 l为 -1,则表示v的左节点不存在 
		node[v].R=r;	//  r 同理  
		if(node[l].P==-1) // 若这个结点还没有父节点 
		node[l].P=v;     // 那么这个结点的父节点就是 v 
		if(node[r].P==-1)
		node[r].P=v;
	}
	for(int i=1;i<=n;i++)
	if(node[i].P==-1)    // 寻找根节点 
	{					 // 只有根本结点没有父节点
		root=i;          // 就是说他的 p 是 -1 
		break;
	}
	preorder(root);
	printf("\n");
	inorder(root);
	printf("\n");
	postorder(root);
	printf("\n"); 
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值