Question

In: Computer Science

(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...

(IN C)

Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:

• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.

• Find a node by integer value: This function takes in two parameters: the root of the tree and the integer value to find. If it finds the integer value, return the node. If it is unable to find the integer value, return NULL.

• Traverse the tree. This function takes in two parameters: the root of the tree, and a flag to indicate the order: preorder, inorder, or postorder. The flags are defined as macros:

#define PREORDER 0

#define INORDER 1

#define POSTORDER 2

Solutions

Expert Solution


/*
 *  C - Program
 *  BST - Insertion and Searching
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PREORDER 0
#define INORDER 1
#define POSTORDER 2
#define S 50

struct Node
{ 
  int ID;
  char str[30];
  struct Node *left;
  struct Node *right; 
}; 
   
struct Node *newNode(int id, char *str) 
{ 
  struct Node *temp =  (struct Node *) malloc(sizeof(struct Node)); 
  
  temp->ID = id;
  strcpy(temp->str, str);
  temp->left = temp->right = NULL;
  
  return temp; 
} 

void preOrder(struct Node *root) 
{ 
  if (root != NULL) 
  { 
    printf("%d\t%s\n", root->ID, root->str);
    preOrder(root->left); 
    preOrder(root->right);
  } 
}

void postOrder(struct Node *root)
{
  if (root != NULL)
  {
    preOrder(root->left);
    preOrder(root->right);
    printf("%d\t%s\n", root->ID, root->str);
  }
}

void inOrder(struct Node *root)
{
  if (root != NULL)
  {
    preOrder(root->left);
    printf("%d\t%s\n", root->ID, root->str);
    preOrder(root->right);
  } 
}

struct Node* insert(struct Node* root, int id, char *str)
{
  if (root == NULL)
    return newNode(id, str); 

  if (id < root->ID) 
    root->left  = insert(root->left, id, str); 
  else if (id > root->ID) 
    root->right = insert(root->right, id, str);    

  return root; 
}

int search(struct Node* root, int id) 
{ 
  if (root->ID == id)
  {
    printf("Found: %d, %s\n", root->ID, root->str);
    return 1; 
  }
  
  if (root->ID < id) 
    return search(root->right, id); 
  
  return search(root->left, id);
}

void traverse(struct Node* root, int flag)
{
  printf("ID\tName\n");
  switch (flag)
  {
    case 0  :   preOrder(root);
                break;
    case 1  :   inOrder(root);
                break;
    case 2  :   postOrder(root);
                break;
  }
}
   
int main() 
{
  int idNum;
  struct Node *root = NULL; 
  
  root = insert(root, 30, "John");
  insert(root, 20, "Bill");
  insert(root, 10, "Jennifer");
  insert(root, 40, "Sophia");
  
  traverse(root, INORDER);
  printf("\n");

  printf("Enter ID number to search: ");
  scanf("%d", &idNum);
  
  if (!search(root, idNum))
    printf("Record not found\n");

  return 0; 
}
/*  Program ends here */


Related Solutions

Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
Write a C++ method that traverses through a binary tree (only 2 children to a node)...
Write a C++ method that traverses through a binary tree (only 2 children to a node) using preorder traversal and finds the proper place to put in a new node. The method will take in an int and traverse through the tree to find the proper place but it must use PREORDER traversal to find a proper position. struct node{ int data; struct node* left, *right; Node(int Data){ this->data=data; left=right=NULL; } }; void preorderAdd(int newNum){ //Method to traverse using preorder...
in C++, given a binary search tree of ints, in which each node contains a size...
in C++, given a binary search tree of ints, in which each node contains a size parameter (also an int), explain, in English, not code, how you would find the median element of the tree in theta(log N) time.
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
A binary tree is a rooted tree in which each node has at most two children....
A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree the number of nodes with two children is exactly one less than the number of leaves.
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
Write this code in full c++ and in separate files in visual studio. Binary Search Tree...
Write this code in full c++ and in separate files in visual studio. Binary Search Tree Using a binary search tree, you are tasked with building a dictionary program that stores a word with its definition. Each node of the tree will contain the word and definition. The word is what will be used as the key to sort our data. The dictionary should allow you to search for a word. If the word exists in the dictionary then the...
(+5) Level of a node in a binary tree is distance from root to that node....
(+5) Level of a node in a binary tree is distance from root to that node. For example, level of root is 0 and levels of left and right children of the root are 1. Level      Max number of nodes 1 2 4 8 16 32 64 ..      … n       ?? The maximum number of nodes on level n of a binary tree is : A          2^(n-1)                         B          2^n C          2^(n+1)                       D            2^[(n+1)//2] In the above answers, the operator...
Draw a (single) binary tree T, such that  Each internal node of T stores a...
Draw a (single) binary tree T, such that  Each internal node of T stores a single character  A preorder traversal of T yields ALGORITHMS  An inorder traversal of T yields GOLATIHRMS
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT