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

二叉树

两个值得注意的性质:

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;
}
相关推荐
Fanxt_Ja3 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1233 天前
【数据结构】二叉树的概念
数据结构·二叉树
散1124 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19434 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww4 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
苏小瀚4 天前
[数据结构] 排序
数据结构
睡不醒的kun4 天前
leetcode算法刷题的第三十四天
数据结构·c++·算法·leetcode·职场和发展·贪心算法·动态规划
吃着火锅x唱着歌4 天前
LeetCode 978.最长湍流子数组
数据结构·算法·leetcode
Whisper_long4 天前
【数据结构】深入理解堆:概念、应用与实现
数据结构
IAtlantiscsdn4 天前
Redis7底层数据结构解析
前端·数据结构·bootstrap