"孩子兄弟表示法(二叉链表)的存储结构如下:\n\nplaintext\ntypedef struct CSNode{\n ElemType data;\n struct CSNode *firstchild, *nextsibling;\n}CSNode, *CSTree;\n\n\n其中,ElemType为结点数据的类型。\n\n根据给定的边集合,可以将该树转化成如下的二叉树:\n\n A\n / | \n B C D\n / / \n E F G\n \n转化成二叉树的步骤如下:\n1. 将树的根结点A作为二叉树的根结点。\n2. 将树的每个结点的第一个孩子作为对应二叉树结点的左孩子,将树的每个结点的下一个兄弟作为对应二叉树结点的右孩子。\n\n根据上面的步骤,可以得到如下的二叉树:\n\n A\n / \n B C\n / / \n E F G\n \n对应的孩子兄弟表示法(二叉链表)的存储结构如下:\n\nplaintext\ntypedef struct CSNode{\n ElemType data;\n struct CSNode *firstchild, *nextsibling;\n}CSNode, *CSTree;\n\n\n其中,ElemType为结点数据的类型。\n\n根据上面的二叉树,可以得到以下的存储结构:\n\nplaintext\nCSTree T;\n\nT = (CSTree)malloc(sizeof(CSNode));\nT->data = 'A';\nT->firstchild = (CSTree)malloc(sizeof(CSNode));\nT->firstchild->data = 'B';\nT->firstchild->firstchild = (CSTree)malloc(sizeof(CSNode));\nT->firstchild->firstchild->data = 'E';\nT->firstchild->firstchild->firstchild = NULL;\nT->firstchild->firstchild->nextsibling = NULL;\nT->firstchild->nextsibling = NULL;\nT->nextsibling = (CSTree)malloc(sizeof(CSNode));\nT->nextsibling->data = 'C';\nT->nextsibling->firstchild = (CSTree)malloc(sizeof(CSNode));\nT->nextsibling->firstchild->data = 'F';\nT->nextsibling->firstchild->firstchild = NULL;\nT->nextsibling->firstchild->nextsibling = (CSTree)malloc(sizeof(CSNode));\nT->nextsibling->firstchild->nextsibling->data = 'G';\nT->nextsibling->firstchild->nextsibling->firstchild = NULL;\nT->nextsibling->firstchild->nextsibling->nextsibling = NULL;\nT->nextsibling->nextsibling = (CSTree)malloc(sizeof(CSNode));\nT->nextsibling->nextsibling->data = 'D';\nT->nextsibling->nextsibling->firstchild = NULL;\nT->nextsibling->nextsibling->nextsibling = NULL;\n\n


原文地址: https://www.cveoy.top/t/topic/pCBE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录