二叉树添加索引
Posted 不二小杰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树添加索引相关的知识,希望对你有一定的参考价值。
前序遍历创建,中序遍历添加索引。。
typedef struct BiNode { char data; char ltag, rtag; struct BiNode *lchild, *rchild; }BiNode; void createTree(BiNode **root); void printTree(BiNode *root, int lever); void addThread(BiNode *root, BiNode *pre); void desTree(BiNode *root, BiNode *pre); int main(int argc, char *argv[]) { BiNode *root = NULL; BiNode pre; BiNode *prePointer = ⪯ int lever = 1; createTree(&root); printTree(root, lever); pre.ltag = ‘t‘; pre.lchild = root; addThread(root, prePointer); pre.rtag = ‘t‘; pre.rchild = prePointer; prePointer->rtag = ‘t‘; prePointer->rchild = ⪯ return 0; } void createTree(BiNode **root) { char el; scanf("%c", &el); if(el == ‘ ‘) { *root = NULL; }else { *root = (BiNode *)malloc(sizeof(BiNode)); (*root)->data = el; (*root)->ltag = ‘l‘; (*root)->rtag = ‘l‘; createTree(&((*root)->lchild)); createTree(&((*root)->rchild)); } } void printTree(BiNode *root, int lever) { if(root) { printf("data: %c, lever: %d \n", root->data, lever); printTree(root->lchild, lever+1); printTree(root->rchild, lever+1); } } void addThread(BiNode *root, BiNode *pre) { if(root) { if(pre->rchild == NULL) { pre->rtag = ‘t‘; pre->rchild = root; } if(root->lchild) { addThread(root->lchild, pre); }else { root->ltag = ‘t‘; root->lchild = pre; pre = root->lchild; } pre = root; addThread(root->rchild, pre); } } void desTree(BiNode *root, BiNode *pre) { }
以上是关于二叉树添加索引的主要内容,如果未能解决你的问题,请参考以下文章