Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ElemType int
- typedef struct BiTNode {
- ElemType data;
- struct BiTNode *lchild, *rchild;
- int count;
- } BiTNode, *BiTree;
- #include <stdlib.h>
- BiTree Search(BiTree t, int k) {
- static int count = 0;
- static BiTree p;
- if (t) {
- Search(t->lchild, k);
- count++;
- if (count == k) p = t;
- else Search(t->rchild, k);
- }
- return p;
- }
- BiTree SearchK(BiTree t, int k) {
- static int count = 0;
- static BiTree p;
- if (t) {
- if (t->lchild) {
- if (t->lchild->count < k) {
- count += t->lchild->count + 1;
- if (count == k) p = t;
- else SearchK(t->rchild, k - count);
- } else SearchK(t->lchild, k);
- }
- count++;
- if (count == k) p = t;
- if (t->rchild) {
- if (t->rchild->count < k) {
- count += t->lchild->count + 1;
- if (count == k) p = t;
- else SearchK(t->lchild, k - count);
- } else {
- count++;
- if (count == k) p = t;
- SearchK(t->rchild, k - count);
- }
- }
- }
- return p;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement