Advertisement
Guest User

Untitled

a guest
Jan 19th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.77 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define elementtype int
  6. #define labeltype int
  7. #define LAMBDA NULL
  8. typedef struct celltag1
  9. {
  10. elementtype element;
  11. struct celltag1 *next;
  12. } celltype1;
  13.  
  14. typedef struct _cell
  15. {
  16. labeltype label;
  17. struct _cell *leftchild, *rightchild;
  18. } celltype;
  19.  
  20. typedef celltype *node;
  21. typedef celltype *BinaryTree;
  22. typedef celltype1 *List;
  23. typedef celltype1 *position;
  24.  
  25.  
  26. void BiMakeNull (BinaryTree*);
  27. int BiEmpty (BinaryTree);
  28. void BiCreate (labeltype, BinaryTree, BinaryTree, BinaryTree*);
  29. node kopiraj (BinaryTree);
  30. void BiLeftSubtree (BinaryTree, BinaryTree*);
  31. void BiRightSubtree (BinaryTree, BinaryTree*);
  32. node BiInsertLeftChild (labeltype, node, BinaryTree*);
  33. node BiInsertRigthChild (labeltype, node, BinaryTree*);
  34. void BiDelete (node, BinaryTree*);
  35. node BiRoot (BinaryTree);
  36. node BiLeftChild (node, BinaryTree);
  37. node BiRightChild (node, BinaryTree);
  38. void roditelj (node*, node, node);
  39. node BiParent(node, BinaryTree);
  40. labeltype BiLabel(node, BinaryTree);
  41. void BiChangeLabel(labeltype, node, BinaryTree*);
  42.  
  43.  
  44. position LiEnd(List L)
  45. {
  46. position q;
  47. q = L;
  48. while (q->next != NULL)
  49. q = q->next;
  50. return q;
  51. }
  52.  
  53. int jesuma(int n)
  54. {
  55. int i=2; int k=2;
  56. for(i=2;i<n;k=2*k){
  57. i=i+k;
  58. }
  59. if(i==n)return 1;
  60. else return 0;
  61. }
  62.  
  63. position LiMakeNull(List *Lp)
  64. {
  65. *Lp = (celltype1*)malloc(sizeof(celltype1));
  66. (*Lp)->next = NULL;
  67. return (*Lp);
  68. }
  69.  
  70. void LiInsert(elementtype x, position p, List *Lp)
  71. {
  72. position temp;
  73. temp = p->next;
  74. p->next = (celltype1*)malloc(sizeof(celltype1));
  75. p->next->element = x;
  76. p->next->next = temp;
  77. }
  78.  
  79. void LiDelete(position p, List *Lp)
  80. {
  81. position temp;
  82. temp = p->next;
  83. p->next = p->next->next;
  84. free(temp);
  85. }
  86.  
  87. position LiFirst(List L)
  88. {
  89. return L;
  90. }
  91.  
  92. position LiNext(position p, List L)
  93. {
  94. if (p->next == NULL)
  95. exit(102);
  96. return (p->next);
  97. }
  98.  
  99. position LiPrevious(position p, List L)
  100. {
  101. position q;
  102. for (q = L; q->next != p; q = q->next);
  103. return q;
  104. }
  105.  
  106. elementtype LiRetrieve(position p, List L)
  107. {
  108. return p->next->element;
  109. }
  110.  
  111. void LiPrint(List L)
  112. {
  113. printf("\n");
  114. position p = LiFirst(L);
  115.  
  116. while (p != LiEnd(L))
  117. {
  118. printf("%d ", LiRetrieve(p, L));
  119. p = LiNext(p, L);
  120. }
  121. printf("\n");
  122. }
  123.  
  124.  
  125. void BiMakeNull(BinaryTree *Tp)
  126. {
  127. *Tp = NULL;
  128. }
  129.  
  130. int BiEmpty(BinaryTree T)
  131. {
  132. if (T)
  133. return 0;
  134. return 1;
  135. }
  136.  
  137. void BiCreate(labeltype l, BinaryTree TL, BinaryTree TR, BinaryTree *Tp)
  138. {
  139. *Tp = (celltype*)malloc(sizeof(celltype));
  140. (*Tp)->label = l;
  141. (*Tp)->leftchild = TL;
  142. (*Tp)->rightchild = TR;
  143. }
  144.  
  145. BinaryTree copy(BinaryTree T)
  146. {
  147. BinaryTree B, BL, BR;
  148.  
  149. BiMakeNull(&B);
  150. BiMakeNull(&BL);
  151. BiMakeNull(&BR);
  152.  
  153. if (T == NULL)
  154. return B;
  155.  
  156. if (T->leftchild == NULL && T->rightchild == NULL)
  157. {
  158. BiCreate(T->label, BL, BR, &B);
  159. return B;
  160. }
  161.  
  162. BL = copy(T->leftchild);
  163. BR = copy(T->rightchild);
  164. BiCreate(T->label, BL, BR, &B);
  165.  
  166. return B;
  167. }
  168.  
  169. void BiLeftSubtree(BinaryTree T, BinaryTree *TL)
  170. {
  171. *TL = copy(T->leftchild);
  172. }
  173.  
  174. void BiRightSubtree(BinaryTree T, BinaryTree *TR)
  175. {
  176. *TR = copy(T->rightchild);
  177. }
  178.  
  179. node BiInsertLeftChild(labeltype l, node i, BinaryTree *Tp)
  180. {
  181. if (!i)
  182. {
  183. printf("Taj cvor ne postoji.");
  184. exit(1);
  185. }
  186.  
  187. if (i->leftchild != NULL)
  188. {
  189. printf("Taj cvor vec ima lijevo dijete.");
  190. exit(2);
  191. }
  192.  
  193. if (!(i->leftchild = (celltype*)malloc(sizeof(celltype))))
  194. {
  195. printf("Nema dovoljno memorije.");
  196. exit(3);
  197. }
  198.  
  199. i->leftchild->label = l;
  200. i->leftchild->leftchild = i->leftchild->rightchild = NULL;
  201.  
  202. return i->leftchild;
  203. }
  204.  
  205. node BiInsertRightChild(labeltype l, node i, BinaryTree *Tp)
  206. {
  207. if (!i)
  208. {
  209. printf("Taj cvor ne postoji.");
  210. exit(1);
  211. }
  212.  
  213. if (i->rightchild != NULL)
  214. {
  215. printf("Taj cvor vec ima desno dijete.");
  216. exit(2);
  217. }
  218.  
  219. if (!(i->rightchild = (celltype*)malloc(sizeof(celltype))))
  220. {
  221. printf("Nema dovoljno memorije.");
  222. exit(3);
  223. }
  224.  
  225. i->rightchild->label = l;
  226. i->rightchild->leftchild = i->rightchild->rightchild = NULL;
  227.  
  228. return i->rightchild;
  229. }
  230.  
  231. void roditelj(node *parent, node i, node root)
  232. {
  233. if ((root->leftchild == i) || (root->rightchild == i))
  234. *parent = root;
  235.  
  236. if (*parent)
  237. return;
  238.  
  239. if (root->leftchild)
  240. roditelj(parent, i, root->leftchild);
  241.  
  242. if (root->rightchild)
  243. roditelj(parent, i, root->rightchild);
  244. }
  245.  
  246. node BiParent(node i, BinaryTree T)
  247. {
  248. if (!i)
  249. {
  250. printf("Taj cvor ne postoji.");
  251. exit(1);
  252. }
  253.  
  254. if (i == T)
  255. return NULL;
  256.  
  257. node parent = NULL;
  258. roditelj(&parent, i, T);
  259. return parent;
  260. }
  261.  
  262. node BiRoot(BinaryTree T)
  263. {
  264. return T;
  265. }
  266.  
  267. node BiLeftChild(node i, BinaryTree T)
  268. {
  269. if (!i)
  270. {
  271. return LAMBDA;
  272. }
  273.  
  274. if (i->leftchild == NULL)
  275. return LAMBDA;
  276.  
  277. return i->leftchild;
  278. }
  279.  
  280. node BiRightChild(node i, BinaryTree T)
  281. {
  282. if (!i)
  283. {
  284. return LAMBDA;
  285. }
  286.  
  287. if (i->rightchild == NULL)
  288. return LAMBDA;
  289.  
  290. return i->rightchild;
  291. }
  292.  
  293. void BiDelete(node i, BinaryTree *Tp)
  294. {
  295. if (!i)
  296. {
  297. printf("Taj cvor ne postoji.");
  298. exit(1);
  299. }
  300.  
  301. if (i->leftchild || i->rightchild)
  302. {
  303. printf("Taj cvor ima dijete te ga nije moguce obrisati.");
  304. exit(4);
  305. }
  306.  
  307. node p = BiParent(i, *Tp);
  308.  
  309. if (p == LAMBDA)
  310. {
  311. free(i);
  312. return;
  313. }
  314.  
  315. if (BiLeftChild(p, *Tp) == i)
  316. p->leftchild = NULL;
  317. else
  318. p->rightchild = NULL;
  319.  
  320. free(i);
  321. }
  322.  
  323. labeltype BiLabel(node i, BinaryTree T)
  324. {
  325. if (!i)
  326. {
  327. return 'L';
  328. }
  329.  
  330. return i->label;
  331. }
  332.  
  333. void BiChangeLabel(labeltype l, node i, BinaryTree *Tp)
  334. {
  335. if (!i)
  336. {
  337. printf("Taj cvor ne postoji.");
  338. exit(1);
  339. }
  340.  
  341. i->label = l;
  342. }
  343.  
  344.  
  345. void preorder(BinaryTree p)
  346. {
  347. if(p==NULL)
  348. {
  349. printf("Prazna lista\n");
  350. return;
  351. }
  352.  
  353. printf("%d ",p->label);
  354. if (p->leftchild) preorder(p->leftchild);
  355. if (p->rightchild) preorder(p->rightchild);
  356. }
  357.  
  358. int prebroji(BinaryTree p)
  359. {
  360. if(p==NULL)return 0;
  361. if(p-> leftchild == NULL && p->rightchild== NULL )return 1;
  362. if (p->leftchild == NULL ) return prebroji(p->rightchild)+1;
  363. if (p->rightchild == NULL ) return prebroji(p->leftchild)+1;
  364. return ( prebroji(p->rightchild)+prebroji(p->leftchild)+1 );
  365. }
  366.  
  367. node HeapDodaj(BinaryTree *Hrpa, node tren, labeltype k)
  368. {
  369. BinaryTree L,R;
  370. node zadnji;
  371.  
  372. if( BiLeftChild( tren, *Hrpa ) ==NULL )
  373. {
  374. /* printf("\n*uspjesno insertan lijevo*\n");
  375. printf("kao dijete slova: %d",BiLabel(tren,*Hrpa));*/
  376. zadnji=BiInsertLeftChild ( k , tren, Hrpa);
  377. return zadnji;
  378. }
  379.  
  380. if( BiRightChild( tren, *Hrpa )==NULL )
  381. {
  382. /* printf("\n*uspjesno insertan desno*\n");
  383. printf("kao dijete slova: %d",BiLabel(tren,*Hrpa));*/
  384. zadnji=BiInsertRightChild ( k , tren, Hrpa);
  385. return zadnji;
  386. }
  387.  
  388. BiLeftSubtree(tren,&L);
  389. BiRightSubtree(tren,&R);
  390. if ( prebroji(L) == prebroji (R) ) return HeapDodaj(&L,BiLeftChild(tren,*Hrpa),k);
  391. if ( jesuma(prebroji(L)+1) ) return HeapDodaj(&R, BiRightChild(tren,*Hrpa),k);
  392. return HeapDodaj(&L, BiLeftChild(tren,*Hrpa),k);
  393. }
  394.  
  395. void LabelSwap ( node i , node j , BinaryTree *T)
  396. {
  397. labeltype temp;
  398. labeltype a,b;
  399. a=BiLabel ( i, *T);
  400. b=BiLabel ( j, *T);
  401. if ( a > b )
  402. {
  403. temp = BiLabel (i, *T);
  404. BiChangeLabel ( BiLabel (j,*T) , i , T );
  405. BiChangeLabel ( temp, j , T );
  406. if (BiParent (j,*T) == NULL )return;
  407. LabelSwap ( j, BiParent(j, *T), T );
  408. }
  409.  
  410. }
  411.  
  412. void HeapAdd(BinaryTree *Hrpa, labeltype k)
  413. {
  414. node zadnji;
  415. zadnji = HeapDodaj(Hrpa, *Hrpa, k);
  416. /*if(BiParent(zadnji,*Hrpa)==NULL){printf("Bananaaaaaaaa");return;}
  417. printf("\ntreba-");*/
  418. LabelSwap (zadnji, BiParent (zadnji,*Hrpa), Hrpa );
  419. }
  420.  
  421. node nadjizadnji(BinaryTree *Hrpa, node tren)
  422. {
  423. BinaryTree L,R;
  424. node zadnji;
  425.  
  426. if( BiLeftChild( tren, *Hrpa ) ==NULL && BiRightChild(tren, *Hrpa)== NULL )
  427. {
  428. return tren;
  429. }
  430.  
  431.  
  432. BiLeftSubtree(tren,&L);
  433. BiRightSubtree(tren,&R);
  434.  
  435. if( BiRightChild( tren, *Hrpa )==NULL )return nadjizadnji(&L,BiLeftChild(tren,*Hrpa));
  436. if ( prebroji(L) == prebroji (R) ) return nadjizadnji(&R,BiRightChild(tren,*Hrpa));
  437. /*if ( jesuma(prebroji(R)+1 ) && jesuma(prebroji(L)+1) ) return nadjizadnji(&L, BiLeftChild(tren,*Hrpa));*/
  438. if( jesuma(prebroji (R)+1 ) ) return nadjizadnji (&L, BiLeftChild(tren,*Hrpa));
  439. return nadjizadnji(&R, BiRightChild(tren,*Hrpa));
  440. }
  441.  
  442. void sortiraj ( BinaryTree *T)
  443. {
  444. labeltype temp;
  445. labeltype a,b;
  446. node L;
  447. if( BiLeftChild(*T,*T)==NULL && BiRightChild(*T,*T)==NULL )return;
  448.  
  449.  
  450. if (BiLeftChild(*T,*T )!= NULL && BiRightChild (*T,*T)!= NULL)
  451. {
  452. a=BiLabel ( BiLeftChild(*T,*T), *T);
  453. b=BiLabel ( BiRightChild(*T,*T), *T);
  454. if (a>=b)
  455. {
  456. if ( a > BiLabel( *T,*T) )
  457. {
  458. temp = BiLabel (*T, *T);
  459. BiChangeLabel ( a , *T, T );
  460. BiChangeLabel ( temp, BiLeftChild(*T,*T) , T );
  461. L=BiLeftChild( *T, *T);
  462. sortiraj (&L);
  463. /*BiLeftSubtree(*T,&K);
  464. sortiraj (&K);*/
  465. }
  466. }
  467. else
  468. {
  469. if ( b > BiLabel( *T,*T) )
  470. {
  471. temp = BiLabel (*T, *T);
  472. BiChangeLabel ( b , *T, T );
  473. BiChangeLabel ( temp, BiRightChild(*T,*T) , T );
  474. L=BiRightChild( *T, *T);
  475. sortiraj (&L);
  476. /*BiRightSubtree(*T,&K);
  477. sortiraj ( &K);*/
  478. }
  479. }
  480. return;
  481. }
  482.  
  483. if(BiLeftChild(*T,*T) != NULL )
  484. {
  485. a=BiLabel ( BiLeftChild(*T,*T), *T);
  486. if ( a > BiLabel( *T,*T) )
  487. {
  488. temp = BiLabel (*T, *T);
  489. BiChangeLabel ( a , *T, T );
  490. BiChangeLabel ( temp, BiLeftChild(*T,*T) , T );
  491. L=BiLeftChild( *T, *T);
  492. sortiraj (&L);
  493. /*BiLeftSubtree(*T,&K);
  494. sortiraj (&K);*/
  495. }
  496. }
  497.  
  498. if(BiRightChild(*T,*T) != NULL )
  499. {
  500. b=BiLabel ( BiRightChild(*T,*T), *T);
  501. if ( b > BiLabel( *T,*T) )
  502. {
  503. temp = BiLabel (*T, *T);
  504. BiChangeLabel ( b , *T, T );
  505. BiChangeLabel ( temp, BiRightChild(*T,*T) , T );
  506. L=BiLeftChild( *T, *T);
  507. sortiraj (&L);
  508. /*BiRightSubtree(*T,&K);
  509. sortiraj ( &K);*/
  510. }
  511. }
  512.  
  513. }
  514.  
  515.  
  516.  
  517.  
  518. labeltype HeapDelMax(BinaryTree *Hrpa)
  519. {
  520. node zadnji;
  521. labeltype vracaj;
  522. zadnji = nadjizadnji ( Hrpa, *Hrpa );
  523. vracaj=BiLabel(*Hrpa,*Hrpa);
  524. BiChangeLabel ( BiLabel(zadnji, *Hrpa) , *Hrpa, Hrpa );
  525. if(BiRoot(*Hrpa) != zadnji)
  526. {
  527. BiDelete(zadnji,Hrpa);
  528. /*printf ( " -> preorder prije sorta: ");
  529. preorder(*Hrpa);*/
  530. sortiraj(Hrpa);
  531. }
  532. else BiMakeNull(Hrpa);
  533. return vracaj;
  534. }
  535.  
  536.  
  537. void HeapSort(List *L, int n)
  538. {
  539. position p =LiFirst(*L);
  540. elementtype k;
  541. BinaryTree Hrpa;
  542. BiMakeNull (&Hrpa);
  543. p=*L;
  544. printf("\n");
  545. printf("Ubacujem %d", LiRetrieve( p,*L) );
  546. k=LiRetrieve (p, *L);
  547. BiCreate( k, NULL, NULL, &Hrpa);
  548. p = LiNext( p, *L );
  549. printf ( " -> preorder: ");
  550. preorder(Hrpa);
  551. printf("\n");
  552. while( p != LiEnd( *L ) )
  553. {
  554. printf("\nUbacujem %d", LiRetrieve( p,*L) );
  555. k=LiRetrieve ( p , *L );
  556. HeapAdd(&Hrpa, k);
  557. printf ( " -> preorder: ");
  558. preorder(Hrpa);
  559. printf("\n");
  560. p = LiNext( p, *L );
  561. }
  562.  
  563. p=*L;
  564. while(p !=LiEnd(*L) )
  565. {
  566. printf("\nBrisem najveceg to je %d", BiLabel(BiRoot(Hrpa), Hrpa));
  567. k=HeapDelMax(&Hrpa);
  568. LiDelete(p,L);
  569. LiInsert(k,p,L);
  570. printf ( " -> preorder: ");
  571. preorder(Hrpa);
  572. printf("\n");
  573. p = LiNext( p, *L );
  574. }
  575. }
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582. int main(void)
  583. {
  584. int n=0;
  585. elementtype temp;
  586. List L1,L2;
  587. position p;
  588.  
  589. /* printf("Upisite broj clanova liste:\n");
  590. scanf("%d",&broj);
  591.  
  592. printf("Upisite clanove liste:\n");
  593. */
  594.  
  595. L1=LiMakeNull(&L1);
  596. p=L1;
  597.  
  598. while(1)
  599. {
  600. scanf(" %d",&temp);
  601. if(temp==0)break;
  602. n++;
  603. LiInsert(temp,p,&L1);
  604. p=LiNext(p,L1);
  605. }
  606.  
  607. HeapSort(&L1, n);
  608.  
  609. printf("\n\nSortirana lista:\n ****");
  610. LiPrint(L1);
  611. printf(" **** \n ");
  612.  
  613.  
  614. return 0;
  615.  
  616.  
  617. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement