Question

In: Computer Science

C++ How can I insert normal array as degenerate tree. Plz use sequential implementation. Thanks.

C++

How can I insert normal array as degenerate tree. Plz use sequential implementation. Thanks.

Solutions

Expert Solution

answer-

C++ program to insert normal array as degenerate tree.-

#include<bit/std++.h>

using namespace std;

// tree Node

struct Node

{ int key;

struct Node *left, *right; };

Node * newNode(int key)

{

Node *temp =new Node;

temp->key =key;

temp->left =temp->right=NULL;

return (temp);

}

void createNode(int parent[], int i, Node *created[], Node **root)

{

if (created[i] != NULL)  

return;

// create a new Node & set created[i]

created[i] = newNode(i);

if (parent[i] == -1)

{

*root = created[i];

return;

}     

if (created[parent[i]] == NULL)

createNode(parent, parent[i], created, root);

Node *p = created[parent[i]];

// find parent pointer

if (p->left == NULL)

    p->left = created[i];

  else

// second child

p->right = created[i];

}

  Node *createTree(int parent[], int n)

{

    Node *created[n];

for (int i=0; i<n; i++)

created[i] = NULL;

Node *root = NULL;

for (int i=0; i<n; i++)

createNode(parent, i, created, &root);

return root;

}

//For adding new line in a program

inline void newLine()

{

cout << "\n";

}  

void inorder(Node *root)

{

if (root != NULL)

{

inorder(root->left);

cout << root->key << " ";

inorder(root->right);

}

}  

// Driver method

int main()

{

   int parent[] = {-1, 0, 0, 1, 1, 3, 5};

int n = sizeof parent / sizeof parent[0];

Node *root = createTree(parent, n);

cout << "Inorder Traversal of constructed tree\n";

  inorder(root);

newLine();

}

Output:

Inorder Traversal of constructed tree

6 5 3 1 4 0 2


Related Solutions

i have an array of strings, how do i sort them into a binary tree? C...
i have an array of strings, how do i sort them into a binary tree? C Program
How can i bubble sort a sentence in a char array in c++ This is the...
How can i bubble sort a sentence in a char array in c++ This is the prototype of the function: char* sort(char string[], int numOfWords, int lengthOfWord); This is the testing code in the main file: char words[] = "CAT FAT BAT HAT RAT"; printf("Before sort: t%s\n";words); char result = sort(words; 5; 3); printf("After sort : t%s\n"; result); Expected output: Before sort: CAT FAT BAT HAT RAT After sort: BAT CAT FAT HAT RAT
HOW CAN I USE a string instead of array tries and res on this assignment, with...
HOW CAN I USE a string instead of array tries and res on this assignment, with out impacting the program or modifying too much on conditions  check code bellow import java.util.Scanner; import java.util.Random;//starter code provided public class inputLap { public static char roller; public static String playerName; public static int printed=0; public static int rounds=8,lives=0,randy; public static int tries[]=new int[4];//use arrays to store number of tries in each life public static int res[]=new int[4]; public static String getName(String aString){ Scanner sc=...
C++ How can I print out the subtrees in my self-balancing tree? What I would like...
C++ How can I print out the subtrees in my self-balancing tree? What I would like to do is have it display something like " ROOT = X, LEFT PARENT = X, X,X, RIGHT PARENT = X,X,X. Here is my code: #include <iostream> using namespace std; class TreeNode { public:    int data;    TreeNode* left;    TreeNode* right; }; TreeNode* newNode(int data); /* A function that constructs Balanced Binary Search Tree from a sorted array */ TreeNode* convertToBTS(int arr[],...
Code in c++, do not use loops, make it as simple as it can be. Thanks!...
Code in c++, do not use loops, make it as simple as it can be. Thanks! Background Well it has finally happened; AMC’s “The Walking Dead” has become a reality. The zombie apocalypse took place. It has been a couple of years since we have started to “rebuild” our society, and now is the time for you to be able to shine. We have come across technology that will allow for us to get back to life as it once...
How can i modify my c code so that each number stored in the array is...
How can i modify my c code so that each number stored in the array is not the array index but the value of the array index converted to radians. I have already made a function for this converion above main(). Below is the code: #include <stdio.h> #include <stdlib.h> #include <math.h> float Deg2Rad (float degrees) { // Calculate & return value float Result; Result = ((M_PI*degrees)/180.0); return (Result); } int main(void) { // Declare variables int Array[90]; int i; //...
How can I create a hexadecimal number in c code from a specific array. For example...
How can I create a hexadecimal number in c code from a specific array. For example I have a[4]={2,5,7,4}; and I want to create this number : 0x004725 by adding 1 number at a time using << operation. Thank you!
In language C Have the program present a menu where I can either: Insert a new...
In language C Have the program present a menu where I can either: Insert a new integer onto the stack. Process an integer from the stack. Quit the program. Every time that you present the menu, please print out the contents of the stack before I pick a menu option. If the stack is empty, please let the user know. The point of this assignment is to use dynamic memory allocation. So you must use malloc at the start of...
I need the code for following in C++ working for Visual studio please. Thanks Use a...
I need the code for following in C++ working for Visual studio please. Thanks Use a Struct to create a structure for a Player. The Player will have the following data that it needs maintain: Struct Player int health int level string playerName double gameComplete bool isGodMode Create the 2 functions that will do the following: 1) initialize(string aPlayerName) which takes in a playername string and creates a Player struct health= 100 level= 1 playerName = aPlayerName gameComplete = 0...
How do I write a C# and a C++ code for creating a character array containing...
How do I write a C# and a C++ code for creating a character array containing the characters 'p', 'i', 'n','e','P','I','N','E' only and then using these lower and capital case letter character generate all possible combinations like PInE or PinE or PIne or PINE or piNE etc. and only in this order so if this order is created eg. NeIP or EnPi or NeIP or IPnE and on. You can generate all the combinations randomly by creating the word pine...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT