Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. //wyswietlanie preorder
  2. //dodawanie rekurencyjne
  3. //usuwanie drzewa
  4. //wyswietlanie liczby wewnetrznych wezlow
  5.  
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. struct treeNode {
  10. int data, klucz;
  11. struct treeNode *lewy, *prawy;
  12. } *root;
  13.  
  14. void addNode(struct treeNode **root, int data, int klucz) {
  15. if(*root) {
  16. if((*root)->klucz >= klucz)
  17. addNode(&(*root)->lewy, data, klucz);
  18. else
  19. addNode(&(*root)->prawy, data, klucz);
  20. } else {
  21. *root = (struct treeNode *)malloc(sizeof(struct treeNode));
  22. (*root)->data = data;
  23. (*root)->klucz = klucz;
  24. (*root)->lewy = (*root)->prawy = NULL;
  25. }
  26. }
  27. void printTreePreOrder(struct treeNode *root) {
  28. if(root) {
  29. printf("K:%d|W:%d ", root->klucz, root->data);
  30. printTreePreOrder(root->lewy);
  31. printTreePreOrder(root->prawy);
  32. }
  33. }
  34. void deleteTree(struct treeNode **root) {
  35. if(*root) {
  36. deleteTree(&(*root)->lewy);
  37. deleteTree(&(*root)->prawy);
  38. free(*root);
  39. *root = NULL;
  40. }
  41. }
  42. int ileWewnetrznych(struct treeNode *root) {
  43. if(root) {
  44. if(root->lewy && root->prawy)
  45. return 1 + ileWewnetrznych(root->lewy) + ileWewnetrznych(root->prawy);
  46. else return 0;
  47. }
  48. }
  49. int main(void) {
  50. addNode(&root, 4, 4);
  51. addNode(&root, 2, 2);
  52. addNode(&root, 6, 6);
  53. addNode(&root, 1, 1);
  54. addNode(&root, 3, 3);
  55. addNode(&root, 5, 5);
  56. addNode(&root, 7, 7);
  57.  
  58. printf("Wypisanie drzewa PreOrder:\n");
  59. printTreePreOrder(root);
  60.  
  61. printf("\nIlosc wezlow wewnetrznych: %d", ileWewnetrznych(root));
  62.  
  63. printf("\n\nUsuwanie drzewa");
  64. deleteTree(&root);
  65. printf("\nIlosc wezlow wewnetrznych: %d", ileWewnetrznych(root));
  66. printf("\nWypisania drzewa PreOrder:\n");
  67. printTreePreOrder(root);
  68. printf("\n\n");
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement