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...
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.
(+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
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
Create a binary search tree of stacks where each node contains a key and a stack.
IN C++   Create a binary search tree of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key, and a empty...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order methods. Make sure to validate that your output is correct. Review the terminology for tree structures and list structures. What is the root of the tree, what’s a parent, what’s a child, what’s an ancestor, what is an external node, what is an internal node, what is the head of the list, what is the tail of the list, etc. Traverse the binary tree...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT