Question

In: Computer Science

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.

Solutions

Expert Solution

//Pseudo code for each methods

Lets assume Tree 'T' is global declared as an array 'A'.
and 'N' is size of Array.

//1.
    {
        int root()
        {
            return A[0];
        }
      
    }

//2.
    {
        int parent(int val)
        {
            int i;
            for(i=0;i<(N/2);i++)
            {
                if((((2*i)+1)<N)&&A[(2*i)+1]==val)
                    return A[i];
                else if((((2*i)+2)<N)&&A[(2*val)+2]==val)
                    return A[i];
            }
            cout<<"This value is not present in Tree"<<endl;
            return -1;
        }
    }
  
//3.
    {
        int left(int val)
        {
            int i;
            for(i=0;i<N;i++)
            {
                if(((2*i)+1)<N)
                {
                    if(A[i]==val)
                        return A[((2*i)+1)];
                }
            }
            cout<<"This value is not present in Tree"<<endl;
            return -1;
        }
    }

//4.
    {
        int right(int val)
        {
            int i;
            for(i=0;i<N;i++)
            {
                if(((2*i)+2)<N)
                {
                    if(A[i]==val)
                        return A[((2*i)+2)];
                }
            }
            cout<<"This value is not present in Tree"<<endl;
            return -1;
        }
    }
  
//5.
    {
        bool IsExternal(int val)
        {
            int i;
            for(i=0;i<N;i++)
            {
                if(A[i]==val)
                return true;
            }
            return false;
        }
    }
  
//6.
    {
        int IsRoot(int val)
        {
            if(A[0]==val)
            return true;
            return false;
        }
    }


Related Solutions

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...
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
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
Given a binary search tree T with n elements. For any two keys k1 and k2...
Given a binary search tree T with n elements. For any two keys k1 and k2 such that k1 < k2, there is an algorithm to print all elements x in T such that k1 ≤x ≤k2 in O(K + log n) time on average, where K is the number of the elements printed out.
Given a binary search tree T with n elements. For any two keys k1 and k2...
Given a binary search tree T with n elements. For any two keys k1 and k2 such that k1 < k2, there is an algorithm to print all elements x in T such that k1 ≤x ≤k2 in O(K + log n) time on average, where K is the number of the elements printed out.
​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.
Draw a (single) binary tree T, such that  Each internal node of T stores a...
Draw a (single) binary tree T, such that  Each internal node of T stores a single character  A preorder traversal of T yields ALGORITHMS  An inorder traversal of T yields GOLATIHRMS
On a circular array with n positions, we wish to place the integers 1, 2, ......
On a circular array with n positions, we wish to place the integers 1, 2, ... r in order, clockwise, such that consecutive integers, including the pair (r,1) are not in adjacent positions on the array. Arrangements obtained by rotation are considered the same. In how many ways can this be done? Give a combinatorial proof.
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
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT