树 Tree
概念:
1、根节点,该节点上,没有父节点
2、子节点,存在与根节点下,数量若干,但只能有一个父节点
3、由【节点】和【边】组成
4、深度,从根节点到最低层节点的层数称为深度
5、叶子节点,该节点无子节点
6、非终端节点,上有老,下有小
7、度,子节点个数
分类:
1、一般树,子节点的个数无限制
2、二叉树,子节点个数最多2个,且子节点位置不可更改
3、森林,N个互不相交的树的集合
二叉树:
1、一般二叉树
2、满二叉树
3、完全二叉树
#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct BalanceTreeNode { int data; struct BalanceTreeNode * pLeftNode; struct BalanceTreeNode * pRightNode; } BtNode, * PBtNode; PBtNode createBalanceTreeNode(int data); void traverseTree(PBtNode root); int main() { PBtNode root = createBalanceTreeNode(0); PBtNode b = createBalanceTreeNode(12); PBtNode c = createBalanceTreeNode(33); PBtNode d = createBalanceTreeNode(55); PBtNode e = createBalanceTreeNode(23); root -> pLeftNode = b; root -> pRightNode = c; c -> pLeftNode = d; d -> pLeftNode = e; traverseTree(root); return 0; } /** * 创建树节点 * @param data * @return */ PBtNode createBalanceTreeNode(int data) { PBtNode pNewBtNode = (PBtNode)malloc(sizeof(BtNode)); if (NULL == pNewBtNode) { printf("内存分配失败,程序中止"); exit(-1); } pNewBtNode -> data = data; pNewBtNode -> pLeftNode = NULL; pNewBtNode -> pRightNode = NULL; return pNewBtNode; } /** * 遍历树节点 * @param iterator */ void traverseTree(PBtNode iterator) { if (NULL == iterator) return; printf("%d ", iterator -> data); if (NULL != iterator -> pLeftNode) traverseTree(iterator -> pLeftNode); if (NULL != iterator -> pRightNode) traverseTree(iterator -> pRightNode); }