Guest User

BST

a guest
May 17th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct tree_node
  5. {
  6.     int value;
  7.     struct tree_node *left, *right;
  8. } node;
  9.  
  10. node* binaryTree(int values[], size_t sizeOfTree);
  11. node* searchTree(node* treeNode, int value);
  12. node* findParent(node* treeNode, node* childNode);
  13. void insertNode(node** root, node* treeNode);
  14. void deleteNode(node* treeNode);
  15. void printTree(node* treeNode);
  16.  
  17. int main()
  18. {
  19.     node *rootNode, *foundNode, *foundParent;
  20.     int values[10] = {10, 8, 9, 4, 3, 11, 45, 89, 43, 100};
  21.    
  22.     rootNode = binaryTree(values, (sizeof(values)/sizeof(int)));
  23.    
  24.     if (rootNode)
  25.     {
  26.         printf("\r\nRoot node address: %p \t\t Root node value: %d \r\n\r\n", rootNode, rootNode->value);
  27.         printTree(rootNode);
  28.     }
  29.     else
  30.     {
  31.         printf("Failed to build tree");
  32.         return 1;
  33.     }
  34.        
  35.     return 0;
  36. }
  37.  
  38. node* binaryTree(int values[], size_t sizeOfTree)
  39. {
  40.     node *current, *root;
  41.     int i;
  42.    
  43.     root = NULL;
  44.    
  45.     for (i = 0; i < sizeOfTree; i++)
  46.     {
  47.         if ((current = (node*)malloc(sizeof(node))) != NULL) // doubt it
  48.         {
  49.             current->left = current->right = NULL;
  50.             current->value = values[i];
  51.             insertNode(&root, current);
  52.         }
  53.         else
  54.         {
  55.             printf("Memory exception.");
  56.             return NULL;
  57.         }
  58.     }
  59.    
  60.     return root;
  61. }
  62.  
  63. void insertNode(node** root, node* treeNode)
  64. {
  65.     if (!(*root))
  66.     {
  67.         *root = treeNode;
  68.         return;
  69.     }
  70.    
  71.     if (treeNode->value < (*root)->value)
  72.         insertNode(&(*root)->left, treeNode);
  73.     else if (treeNode->value > (*root)->value)
  74.         insertNode(&(*root)->right, treeNode);
  75. }
  76.  
  77. node* searchTree(node* treeNode, int value)
  78. {
  79.     node* current;
  80.    
  81.     current = NULL;
  82.    
  83.     if ((value < treeNode->value) && (treeNode->left != NULL)) // make sure the tree actually has a node to search...
  84.         current = searchTree(treeNode->left, value);
  85.     else if (value == treeNode->value)
  86.         current = treeNode;
  87.     else if ((value > treeNode->value) && (treeNode->right != NULL))
  88.         current = searchTree(treeNode->right, value);
  89.        
  90.     return current;
  91. }
  92.  
  93. node* findParent(node* treeNode, node* childNode)
  94. {
  95.     node* parent;
  96.     parent = NULL;
  97.    
  98.     if ((treeNode->left == childNode) || (treeNode->right == childNode))
  99.         parent = treeNode;
  100.     else
  101.     {
  102.         if ((treeNode->left != NULL) && (parent == NULL)) // only continue searching if a result has not been found
  103.             parent = findParent(treeNode->left, childNode);
  104.         if ((treeNode->right != NULL) && (parent == NULL))
  105.             parent = findParent(treeNode->right, childNode);
  106.     }
  107.    
  108.     return parent;
  109. }
  110.  
  111. void deleteNode(node* treeNode)
  112. {
  113.     // still to do
  114. }
  115.  
  116.  
  117. void printTree(node* treeNode)
  118. {
  119.     if (treeNode->left)
  120.         printTree(treeNode->left);
  121.    
  122.     printf("Node address: %p \t\t Left node address: %p \t\t Right node address: %p \t\t Node value: %d \r\n", treeNode, treeNode->left, treeNode->right, treeNode->value);
  123.    
  124.     if (treeNode->right)
  125.         printTree(treeNode->right);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment