Question

In: Computer Science

Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal...

  1. Create a Binary Search Tree for the following data and do In-order, Preorder and Post-order traversal of the tree.

50, 60, 25, 40, 30, 70, 35, 10, 55, 65, 5

  1. Write an algorithm to delete a node in Singly Linked List                            [12
  2. Write an algorithm of Binary Search                                                              [10]
  3. Write a program in ‘C’ to generate Fibonacci series using recursion            [8]

Solutions

Expert Solution

*** FUNCTION TO DELETE A NODE FROM SINGLE LINK LIST:-

/* Given a reference (pointer to pointer) to the head of a list

   and a key, deletes the first occurrence of key in linked list */

void deleteNode(struct Node **head_ref, int key)

{

    // Store head node

    struct Node* temp = *head_ref, *prev;

  

    // If head node itself holds the key to be deleted

    if (temp != NULL && temp->data == key)

    {

        *head_ref = temp->next;   // Changed head

   free(temp);               // free old head

        return;

    }

  

    // Search for the key to be deleted, keep track of the

    // previous node as we need to change 'prev->next'

    while (temp != NULL && temp->data != key)

    {

        prev = temp;

        temp = temp->next;

    }

  

    // If key was not present in linked list

    if (temp == NULL) return;

  

    // Unlink the node from linked list

    prev->next = temp->next;

  

    free(temp); // Free memory

}

B. ALGORITHM FOR BINARY SEARCH:-

function binary_search(A, n, T):
    L := 0
    R := n − 1
    while L <= R:
        m := floor((L + R) / 2)
        if A[m] < T:
            L := m + 1
        else if A[m] > T:
            R := m - 1
        else:
            return m
    return unsuccessful

C.Fibonacci Series using recursion in C:-

  1. #include<stdio.h>    
  2. void printFibonacci(int n){    
  3.     static int n1=0,n2=1,n3;    
  4.     if(n>0){    
  5.          n3 = n1 + n2;    
  6.          n1 = n2;    
  7.          n2 = n3;    
  8.          printf("%d ",n3);    
  9.          printFibonacci(n-1);    
  10.     }    
  11. }    
  12. int main(){    
  13.     int n;    
  14.     printf("Enter the number of elements: ");    
  15.     scanf("%d",&n);    
  16.     printf("Fibonacci Series: ");    
  17.     printf("%d %d ",0,1);    
  18.     printFibonacci(n-2); //n-2 because 2 numbers are already printed    
  19.   return 0;  
  20. }    

OUTPUT:-

Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

Related Solutions

Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to...
Design a nonrecursive post order traversal algorithm. 1.Use the linked representation for the binary tree to be traversed. 2.Except left, right, and data fields, there are no other fields in a node. 3.After the traversal the tree should remain intact. 4.Use only one stack. 5.You can’t push anything other than nodes into the stack.
Create a new project to build a Binary search tree, and do the following: Create a...
Create a new project to build a Binary search tree, and do the following: Create a TreeNode class, Add the methods "Insert" to insert new nodes to the tree. Add the method "inorder". Make the method to return a list of the node elements in inorder. Implement the equals method in the BST. Two BST are equal if they contain the same elements. In the main insert the elements of the tree, call the max method and print the max...
M = [4,3,7,6,5,2,4,1,0,7] Construct a binary search tree for M. Then traverse it (post order)
M = [4,3,7,6,5,2,4,1,0,7] Construct a binary search tree for M. Then traverse it (post order)
JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below:...
JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 Print the Pre-order of this tree Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three columns respectively. Use...
Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85,...
Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 Print the Pre-order of this tree Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three columns respectively. Use the following...
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output...
Tree Reconstruction - Pre+In => Post (C++) Give you pre-order traversal and in-order traversal, please output post-order traversal. Input Two lines. Pre-order traversal and in-order traversal. Output Post-order traversal. Sample Input GDAFEMHZ ADEFGHMZ Sample Output AEFDHZMG
the following lists the nodes in a binary tree in two different orders: Preorder : A...
the following lists the nodes in a binary tree in two different orders: Preorder : A B C D E F G H I J K L M Inorder : C E D F B A H J I K G M L Draw the binary tree
1. Create the binary tree that has postorder traversal of { b d a h g...
1. Create the binary tree that has postorder traversal of { b d a h g c f,} and has the inorder traversal of {b i a d f g h c} 2. Create the binary tree that has the preorder traversal of {10, 20, 40, 50, 80, 60, 70, 90} and the inorder traversal of { 40, 50, 20, 10, 80, 70, 90, 60}
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT