#二叉树概念
二叉树的相关概念,如,树高度,节点层数,节点度数,路径,叶节点,分支节点,根节点,父节点,左节点,右节点,兄弟节点,祖先节点,子孙节点,左子树,右子树等基本概念。
列举几个二叉树相关概念
先序遍历
先序遍历就是在二叉树的每个根结点处先遍历根节点,再遍历左分支在遍历右分支从而形成排序;
口诀:先根 再左 后右 — 根左右
代码实现:
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