Question

In: Computer Science

Create an array-based implementation of a binary tree. DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM

Create an array-based implementation of a binary tree.

DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM

Solutions

Expert Solution

A binary tree is a special type of tree in which each node of the tree can have at most two child nodes. These child nodes are known as right child and left child.

A simple binary tree is −

For representing trees, there are two ways,

  • dynamic node representation which uses linked list
  • Sequential representation which uses array.

Here, we will discuss about array representation of binary tree. For this we need to number the nodes of the BT. This numbering can start from 0 to (n-1) or from 1 to n.

Lets derive the positions of nodes and their parent and child nodes in the array.

When we use 0 index based sequencing,

Suppose parent node is an index p.

Then, the left_child node is at index (2*p)+ 1.

The right_child node is at index (2*p) + 2.

Root node is at index 0.

left_child is at index 1.

Right_child is at index 2.

When we use 1 index based sequencing,

Suppose parent node is at index p,

Right_node is at index (2*p).

Left_node is at index (2*p)+1.

Root node is at index 1.

left_child is at index 2.

Right_child is at index 3.

Example

Implementation

#include<bits/stdc++.h>
using namespace std;
char tree[10];
int rootnode(char key){
   if(tree[0] != '\0')
      cout<<"Tree already had root";
   else
      tree[0] = key;
   return 0;
}
int leftchild(char key, int parent){
   if(tree[parent] == '\0')
      cout <<"\nCan't set child at"<<(parent * 2) + 1<<" , no parent found";
   else
      tree[(parent * 2) + 1] = key;
   return 0;
}
int rightchild(char key, int parent){
   if(tree[parent] == '\0')
      cout<<"\nCan't set child at"<<(parent * 2) + 2<<" , no parent found";
   else
      tree[(parent * 2) + 2] = key;
   return 0;
}
int traversetree(){
   cout << "\n";
   for(int i = 0; i < 10; i++){
      if(tree[i] != '\0')
         cout<<tree[i];
      else
         cout<<"-";
   }
   return 0;
}
int main(){
   rootnode('A');
   rightchild('C', 2);
   leftchild('D', 0);
   rightchild('E', 1);
   rightchild('F', 2);
   traversetree();
   return 0;
}

Output

Can't set child at6 , no parent found
Can't set child at6 , no parent found
AD--E-----

UML diagram


Related Solutions

Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
Please make an Array-based implementation of a Binary Tree in Python based on the given file...
Please make an Array-based implementation of a Binary Tree in Python based on the given file below. Make sure to keep the Abstract Data File of the starter code below when building the Array-based implementation. Python Starter Code Given Below: class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element."""...
Given an array of foods, create a binary search tree. Then, make a copy of that...
Given an array of foods, create a binary search tree. Then, make a copy of that BST and balance it. Language is C++. Vectors are not allowed. The balance function definition: void balance(BST treeObj); and then when writing the function: void BST::balance(BST treeObj) where BST is a class. The function will be called in main like: originalTree.balance(balancedTree);
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of...
Make an Array implementation of a binary tree given the below class ArrayBinaryTree(BinaryTree): """Linked representation of a binary tree structure.""" # -------------------------- nested _Node class -------------------------- class _Node: def __init__(self, element, parent=None, left=None, right=None): # -------------------------- nested Position class -------------------------- class Position(BinaryTree.Position): """An abstraction representing the location of a single element.""" def __init__(self, container, node): def element(self): def __eq__(self, other): # ------------------------------- utility methods ------------------------------- def _validate(self, p): """Return associated node, if position is valid.""" def _make_position(self, node): """Return Position...
a) Based on the binary tree implementation from the Python program below  write a recursive method that...
a) Based on the binary tree implementation from the Python program below  write a recursive method that calculates the number of leaf nodes in the tree. class Binaertre: def __init__(self, datatobjekt): self.data = datatobjekt self.forelder = None self.venstre_barn = None self.hoyre_barn = None @property def venstre_barn(self): return self.__venstre_barn @venstre_barn.setter def venstre_barn(self, node): self.__venstre_barn = node if node is not None: node.forelder = self @property def hoyre_barn(self): return self.__hoyre_barn @hoyre_barn.setter def hoyre_barn(self, node): self.__hoyre_barn = node if node is not None: node.forelder...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram which shows: Classes (Only the ones listed in bold below) Attributes in classes (remember to indicate privacy level, and type) No need to write methods Relationships between classes (has is, is a, ...) Use a program like Lucid Cart and then upload your diagram. Each movie has: name, description, length, list of actors, list of prequals and sequals Each TV Show has: name, description,...
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
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
What would the pseudocode look like for this UML class diagram? Class - oranges: String -...
What would the pseudocode look like for this UML class diagram? Class - oranges: String - bananas: String - grapes: String - apples: String + Class(String bananas, String grapes, String apples) + oranges( ): void + isValidGrapes( ): boolean + isValidApples( ): boolean + getOranges( ): String + getBananas( ): String + setBananas(String bananas): void + getGrapes( ): String + setGrapes(String grapes): void + getApples( ): String + setApples(String apples): void
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT