二叉树学习

本文介绍了二叉树的基本概念,包括树高度、节点度数等,并提供了先序、中序、后序遍历的代码实现。同时讲解了二叉搜索树(BST)的特点和构建方法,以及如何求解树的高度和找到树中的最大数值。通过示例展示了以6382517构建的BST的前序遍历过程。
摘要由CSDN通过智能技术生成

#二叉树概念
二叉树的相关概念,如,树高度,节点层数,节点度数,路径,叶节点,分支节点,根节点,父节点,左节点,右节点,兄弟节点,祖先节点,子孙节点,左子树,右子树等基本概念。

列举几个二叉树相关概念

先序遍历

在这里插入图片描述

先序遍历就是在二叉树的每个根结点处先遍历根节点,再遍历左分支在遍历右分支从而形成排序;
口诀:先根 再左 后右 — 根左右

代码实现:

void preorder(Node* node){
	if(node != NULL)
	{
		printf("%d\n",node -> data);
		preorder(node -> left);
		preorder(node -> right);
	}

}

整个遍历的实现主要是通过递归的方法实现整个先序遍历

中序遍历

在这里插入图片描述

口诀:先左 再根 后右 — 左根右

代码实现:

void inorder(Node* node){
	if(node != NULL)
	{
		inorder(node -> left);
		printf("%d\n",node -> data);
		inorder(node -> right);
	}
}

整个遍历的实现主要是通过递归的方法实现整个先序遍历

后序遍历

在这里插入图片描述

口诀:先左 再右 后根 — 左右根

代码实现:

void postorder(Node* node){
	if(node != NULL){
		postorder(node -> left);
		postorder(node -> right);
		printf("%d\n",node -> data);
	}
}

整个遍历的实现主要是通过递归的方法实现整个先序遍历

二叉搜索树(BST)

二叉搜索树是可以降低搜索复杂度
满足特点:
任何子树中的根节点都大于左分支 : 根 > 左
任何子树中的根节点都小于右分支 : 根 < 右

构建树的代码:

//创建结点,具有结点数值和左分支和右分支
typedef struct node{
	int data;
	struct node* left;
	struct node* right;
} Node;

//构建树,调用每个根结点
typedef struct {
	Node* root;
} Tree;

//输入主函数中创建的树,和根节点数据值
void insert(Tree* tree,int value){
//malloc用于分配合理的空间使用
	Node* node = (Node*)malloc(sizeof(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(value < temp -> data)
			{
				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;
				}
			}
		}
	}
}

求树高度

int get_hight(Node* node){
	if(node == NULL){
		return 0;
	}
	else{
		int left_h = get_hight(node -> left);
		int right_h =get_hight(node -> right);
		int max = left_h;
		if(max < right_h) max = right_h;
		return max+1;
	}
}

求树中最大数结点

int get_maximum(Node* node){
	if(node == NULL){
		return -1;
	}
	else{
		int m1 = get_maximum(node->left);
		int m2 = get_maximum(node->right);
		int m3 = node->data;
		if(m1>m3) {m3 = m1;}
		if(m2>m3) {m3 = m2;}
		return m3;
	}
}

全部代码合并展示

以 6 3 8 2 5 1 7 为二叉搜索树进行前序遍历

#include<bits/stdc++.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 = (Node*)malloc(sizeof(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(value < temp -> data)
			{
				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)
	{
		printf("%d\n",node->data);
		preorder(node->left);
		preorder(node->right);
	}

}
void inorder(Node* node){
	if(node != NULL)
	{
		inorder(node->left);
		printf("%d\n",node->data);
		inorder(node->right);
	}
}
void postorder(Node* node){
	if(node != NULL){
		postorder(node->left);
		postorder(node->right);
		printf("%d\n",node->data);
	}
}

int get_hight(Node* node){
	if(node == NULL){
		return 0;
	}
	else{
		int left_h = get_hight(node -> left);
		int right_h =get_hight(node -> right);
		int max = left_h;
		if(max < right_h) max = right_h;
		return max+1;
	}
}
int get_maximum(Node* node){
	if(node == NULL){
		return -1;
	}
	else{
		int m1 = get_maximum(node->left);
		int m2 = get_maximum(node->right);
		int m3 = node->data;
		if(m1>m3) {m3 = m1;}
		if(m2>m3) {m3 = m2;}
		return m3;
	}
}
int main(){
	int arr[7]={6,3,8,2,5,1,7};
	Tree tree;
	tree.root=NULL;
	for(int i=0;i<7;i++){
		insert(&tree,arr[i]);
	}
	preorder(tree.root);
	int h = get_hight(tree.root);
	printf("h = %d\n",h);
	int mk = get_maximum(tree.root);
	printf("max = %d\n",mk);
}

参考文章:

https://blog.csdn.net/chinesekobe/article/details/110874773?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165770236016781683985362%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=165770236016781683985362&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-110874773-null-null.142^v32^new_blog_pos_by_title,185^v2^control&utm_term=%E4%BA%8C%E5%8F%89%E6%A0%91&spm=1018.2226.3001.4187

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值