SHARE
TWEET

Untitled

a guest Sep 15th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define ElemType int
  2.  
  3. typedef struct BiTNode {
  4.     ElemType data;
  5.     struct BiTNode *lchild, *rchild;
  6.     int count;
  7. } BiTNode, *BiTree;
  8.  
  9. #include <stdlib.h>
  10.  
  11. BiTree Search(BiTree t, int k) {
  12.     static int count = 0;
  13.     static BiTree p;
  14.     if (t) {
  15.         Search(t->lchild, k);
  16.         count++;
  17.         if (count == k) p = t;
  18.         else Search(t->rchild, k);
  19.     }
  20.     return p;
  21. }
  22.  
  23. BiTree SearchK(BiTree t, int k) {
  24.     static int count = 0;
  25.     static BiTree p;
  26.     if (t) {
  27.         if (t->lchild) {
  28.             if (t->lchild->count < k) {
  29.                 count += t->lchild->count + 1;
  30.                 if (count == k) p = t;
  31.                 else SearchK(t->rchild, k - count);
  32.             } else SearchK(t->lchild, k);
  33.         }
  34.  
  35.         count++;
  36.         if (count == k) p = t;
  37.        
  38.         if (t->rchild) {
  39.             if (t->rchild->count < k) {
  40.                 count += t->lchild->count + 1;
  41.                 if (count == k) p = t;
  42.                 else SearchK(t->lchild, k - count);
  43.             } else {
  44.                 count++;
  45.                 if (count == k) p = t;
  46.                 SearchK(t->rchild, k - count);
  47.             }
  48.         }
  49.     }
  50.     return p;
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top