二叉树 & 多叉树

· · 算法·理论

二叉树模板

struct tree
{
    char val;
    tree *L;
    tree *R;
};

tree *createNode(char v)
{
    tree *t = new tree;
    t->val = v;
    t->L = NULL;
    t->R = NULL;
    return t;
}

tree *createTree(int i)
{
    //createTree
    return NULL;
}

void PrintAns(tree *t)
{
    if(t == NULL) return;
    cout << t->val;
    printAns(t->L);
    printAns(t->R);
}

int main()
{
    tree *root = createTree(0);
    printAns(root);
    return 0;
}

前序:根-左-右

中序:左-根-右

后续:左-右-根

多叉树

一般来讲多叉树不会用像存图那样用链表,所以就有了

多叉树的二叉链表示

超详细的多叉树详解!

struct tree
{
    int v;
    tree *fir, *sib;
};

fir表示该节点的第一个子节点,sib表示该节点的下一个兄弟节点