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
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.
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.
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);
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
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels...
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a perfect binary tree is 2^(height+1) - 1.) /** Returns true if the tree is a perfect binary tree */ public boolean isPerfectBST() Class Name: Exercise25_03
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT