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

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.
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
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...
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
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
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal...
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal algorithms (preorder, inorder, and postorder) for binary trees. Assuming that your algorithm is recursive, find the number of recursive calls made.
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder,...
C++ code (just fuction) Binary Search Tree (BTS) 1) Algorithm for all traversals (inorder, preorder, postorder, level order), done nonrecursively 2)Ability to provide the result of traversing a tree in all traversals (inorder, preorder, postorder, level order)
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT