Question

In: Computer Science

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 instance for given node (or None if no node)."""

    # -------------------------- binary tree constructor --------------------------
    def __init__(self):
        """Create an initially empty binary tree."""

    # -------------------------- public accessors --------------------------
    def __len__(self):
        """Return the total number of elements in the tree."""

    def root(self):
        """Return the root Position of the tree (or None if tree is empty)."""

    def parent(self, p):
        """Return the Position of p's parent (or None if p is root)."""

    def left(self, p):
        """Return the Position of p's left child (or None if no left child)."""

    def right(self, p):
        """Return the Position of p's right child (or None if no right child)."""

    def num_children(self, p):
        """Return the number of children of Position p."""

    # -------------------------- nonpublic mutators --------------------------
    def _add_root(self, e):
        """Place element e at the root of an empty tree and return new Position."""

    def _add_left(self, p, e):
        """Create anew left child for Position p, storing element e."""

    def _add_right(self, p, e):
        """Create a new right child for Position p, storing element e. Return the Position of new node. Raise ValueError if Position p is invalid or p already has a right child.
        """

    def _replace(self, p, e):
        """Replace the element at position p with e, and return old element."""

    def _delete(self, p):
        """Delete the node at Position p, and replace it with its child, if any.  Return the element that had been stored at Position p.Raise ValueError if Position p is invalid or p has two children.
        """

    def _attach(self, p, t1, t2):
        """Attach trees t1 and t2, respectively, as the left and right subtrees of the external Position p. As a side effect, set t1 and t2 to empty.
        Raise TypeError if trees t1 and t2 do not match type of this tree. Raise ValueError if Position p is invalid or not external.
        """

Solutions

Expert Solution

#include<iostream.h>

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

int binarysearch(int arr[],int k,int r,int x)

{

while(k<=r)

{

int m=k+(r-k)/2;

if(arr[m]==x)

{

return m;

}

if(arr[m]<x)

{

k=m+1;

}

else

{

r=m-1;

}

}

return -1;

}

int main(void)

{

clrscr();

int arr[20],n,x,i;

cout<<"enter the number of elements in the array: ";

cin>>n;

cout<<"enter the elements one by one:\n" ;

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

{

cin>>arr[i];

}

cout<<"enter the value you want to check : ";

cin>>x;

int result=binarysearch(arr,0,n-1,x);

if(result==-1)

{

cout<<"element is not present in the array";

}

else

{

result++;

cout<<"Element is present at "<<result<<" position";

}

getch();

return 0;

}

#include<conio.h>

#include<stdio.h>

int binarysearch(int arr[],int k,int r,int x)

{

while(k<=r)

{

int m=k+(r-k)/2;

if(arr[m]==x)

{

return m;

}

if(arr[m]<x)

{

k=m+1;

}

else

{

r=m-1;

}

}

return -1;

}

int main(void)

{

clrscr();

int arr[20],n,x,i;

cout<<"enter the number of elements in the array: ";

cin>>n;

cout<<"enter the elements one by one:\n" ;

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

{

cin>>arr[i];

}

cout<<"enter the value you want to check : ";

cin>>x;

int result=binarysearch(arr,0,n-1,x);

if(result==-1)

{

cout<<"element is not present in the array";

}

else

{

result++;

cout<<"Element is present at "<<result<<" position";

}

getch();

return 0;

}


Related Solutions

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);
Let T be a binary tree with n positions that is realized with an array representation...
Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
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.
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
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
Write an implementation of the set class, with associated iterators using a binary search tree. Add...
Write an implementation of the set class, with associated iterators using a binary search tree. Add to each node a link to the parent node. (C++)
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT