考研数据结构手写代码
时间: 2023-10-13 07:07:57 浏览: 193
数据结构的考研手写代码包括二叉树线索化和树的深度计算。以下是相关的代码示例:
1. 二叉树线索化:
```c++
void InThread(ThreadTree p, ThreadTree &pre) {
if (p != NULL) {
InThread(p->lchild, pre);
if (p->lchild == NULL) {
p->lchild = pre;
p->ltag = 1;
}
if (pre != NULL && pre->rchild == NULL) {
pre->rchild = p;
pre->rtag = 1;
}
pre = p;
InThread(p->rchild, pre);
}
}
void CreateInThread(ThreadTree T) {
ThreadTree pre = NULL;
if (T != NULL) {
InThread(T, pre);
pre->rchild = NULL;
pre->rtag = 1;
}
}
```
2. 树的深度计算:
```c++
int treeDepth(BiTree T) {
if(!T) return 0;
else {
int l = treeDepth(T->lchild);
int r = treeDepth(T->rchild);
return (l > r ? l : r) + 1;
}
}
```
阅读全文