第一讲
暴力定义二叉树,并适应三种遍历方法遍历二叉树,核心思想是递归
#include<stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *left;
struct node *right;
}Node;
//先序遍历,从根部出发,先访问左边,在访问右边,根据递归的原理
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 main()
{
//暴力定义四个结点并将它们连接起来
Node n1;
Node n2;
Node n3;
Node n4;
n1.data = 5;
n2.data = 6;
n3.data = 7;
n4.data = 8;
n1.left = &n2;
n1.right = &n3;
n2.left = &n4;
n2.right = NULL;
n3.right = NULL;
n3.left = NULL;
n4.left = NULL;
n4.right = NULL;
preorder(&n1);
putchar('\n');
inorder(&n1);
putchar('\n');
postorder(&n1);
return 0;
}
搜索二叉树——中序遍历都是从小到大排列
//二叉树规则
//根大于左边,小于右边
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int data;
struct node* left;
struct node* right;
}Node;
//将树打包
typedef struct {
Node* root;
}Tree;
void insert(Tree* tree, int value) {
//将value打包成一个新的结点
Node* node = (Node *)malloc(sizeof(Node));
node->data = value;
node->left = NULL;
node->right = NULL;
if (tree->root == NULL) { //如果树的根为空,则直接将node结点放到树的根上去
tree->root = node;
}
else { //否则往下进行探测比较
Node* temp = tree->root; //建立一个临时结点,使用while循环对node进行比较
while (temp != NULL) {
if (value < temp->data) { //如果value小于temp左边,往下探测比较
if (temp->left == NULL) { //如果左边为空,node放进去
temp->left = node;
return; //返回
}
else {
temp = temp->left; //不为空,继续沿着左边往下
}
}
else { //tvalue大于右边的情况,同理:
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_height(Node* node) {
if (node == NULL) { //递归出口
return 0;
}
else {
int left_h = get_height(node->left);
int right_h = get_height(node->right);
int max = left_h;
if (right_h > max) {
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;
int max = m1;
if (m2 > max) { max = m2; }
if (m3 > max) { max = m3; }
return max;
}
}
int main()
{
int arr[7] = { 6,3,8,2,5,1,7 };
int i;
Tree tree;
tree.root = NULL;
for (i = 1; i < 7; i++) {
insert(&tree, arr[i]);
}
preorder(tree.root); //一般使用先序遍历和中序遍历就可以确定二叉树插的对不对
putchar('\n');
inorder(tree.root); //搜索二叉树的中序遍历都是从小到大的顺序排列
putchar('\n');
int h = get_height(tree.root);
printf("h = %d\n", h);
int m = get_maximum(tree.root);
printf("m = %d\n", m);
}