数据结构——简单二叉树的性质和遍历

二叉树

两个值得注意的性质:

1.二叉树是有序树,这个我想了很久不知道为什么,可是为什么二叉树只有三种遍历方式,而不是六种?说明每个形态不同的树都有不同的含义。那完全二叉树和平衡二叉树呢?也是一样的

2.n0=n2+1 推法: n-1=2n2+n1 n=n0+n1+n2

遍历

前 中 后序遍历

cpp 复制代码
#include <iostream>
#include <stdlib.h>

using namespace std;

typedef struct Node
{
    struct Node *left;
    struct Node *right;
    int data;
} N, Tr;
N *createNode(int data)
{
    N *a = (N *)malloc(sizeof(N));
    a->left = a->right = NULL;
    a->data = data;
    return a;
}
void pre(Tr *root)
{
    if (root == NULL)
        return;
    cout
        << root->data << " ";
    pre(root->left);
    pre(root->right);
}
void In(Tr *root)
{
    if (root == NULL)
        return;
    In(root->left);
    cout << root->data << " ";
    In(root->right);
}
void aft(Tr *root)
{
    if (root == NULL)
        return;
    aft(root->left);
    aft(root->right);
    cout << root->data << " ";
}
int main()
{
    Tr *root = createNode(3);
    root->right = createNode(4);
    root->left = createNode(5);
    root->left->left = createNode(6);
    root->left->right = createNode(7);
    root->right->left = createNode(8);
    root->right->right = createNode(9);
    pre(root);
    cout << endl;
    In(root);
    cout << endl;
    aft(root);
    cout << endl;
}
相关推荐
m0_547486665 天前
《数据结构教程》全套 PPT课件2026
数据结构
tryxr5 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_35 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
All for pursuit.5 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.5 天前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
渡我白衣5 天前
HttpRequest与HttpResponse的实现
服务器·数据结构·c++·人工智能·tcp/ip·机器学习·caffe
晴天的雨.9925 天前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
淡海水5 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic1015 天前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
2401_839080545 天前
C++常见八股
数据结构·c++·算法