Lab 11
Lab 11
Lab 11
2023
ALERT!
Output Format: Print N space separated integers denoting the elements of the array in the order in which
they are required.
Sample Input:
52
12345
Sample Output:
13524
Binary trees are a type of data structure used for searching, storing, and retrieving elements in an
efficient manner. It consists of nodes connected in a tree-like fashion where each node has a
maximum of two children nodes. The node at the top of the tree is called the root node, while the
nodes that have no children are called leaf nodes.
The array representation of binary tree can be done using a technique called level order traversal. In level-
order traversal, the elements of the binary tree are stored in the array in the order in which they are visited
in a breadth-first search.
The array representation of binary tree allows for efficient access to the elements of the tree. For example,
if a binary tree has n nodes, the array representation of the tree can be stored in an array of size n, allowing
for constant-time access to each node in the tree.
The left child of node i can be found at position 2i + 1
The right child of node i can be found at position 2i + 2
It is also possible to determine the parent of a node at position i using the formula:
The parent of node i can be found at position (i – 1) / 2
For the array representation of binary tree, we will form an array of size 2^h-1 size where h
represents the maximum possible height of tree. Now we will move step by step for the array
representation of binary tree.
1. First, suppose we have a binary tree with seven nodes and height 3.
2. Now, for the array representation of binary tree, we have to give numbering to the
corresponding nodes. The standard form of numbering the nodes is to start from the root
node and move from left to right at every level .After numbering the tree and nodes will look
like this:
3. Now as we have numbered from zero you can simply place the corresponding number in the
matching index of an array then the array will look like this:
4. That is the array representation of binary tree but this is the easy case as every node has
two child so what about other cases? We will discuss other cases below.
5. In these cases, we will discuss the scenarios for the array representation of binary tree
where there the lead node is not always on the last level.
8. Now just number the nodes as done above for the array representation of binary tree after
that the tree will look like this:
9. Now we have the number on each node we can easily use the tree for array representation
of binary tree and the array will look like this:
class ArrayBT
{
int maxHeight;
int arraySize;
T * data;
bool* nodeStatus;
// You can make a private helper function if you need, remember to justify it
public:
ArrayBT(int h); //Initializes the node status array to zero.
setRoot(T r); // stores r at data[0] as root of tree and also sets the
nodeStatus[0] = 1
T getRoot(); // return root of the tree if exists
setLeftChild(T parent, T child);
setRightChild(T parent, T child);
T getParent(T node);
remove(T node); // removes the given node and all its descendants from the tree
displayAncestors(T node); // display ancestors of the given node
displayDescendants(T node); // display descendants of the given node
int heightOfTree(); // returns height (actual height) of the tree
levelOrder(); // do the level order traversal of the tree
displayLevel(int levelNo); // display the nodes on a particular level number
int findLevelOfNode(T node); // returns the level/depth of the given node
//each index of a level starts from 2^levelNo-1 and end at 2^levelNo+2^(levelNo-1)
displayParenthesizedView();
displayExplorerForm();
};
Visit Root (1): Process the value of the root node, which is 1.
Traverse Left Subtree (2): Move to the left subtree and repeat the process.
Traverse Left Subtree (4): Move to the left subtree and repeat.
Traverse Right Subtree (5): Move to the right subtree and repeat.
Traverse Right Subtree (3): Move to the right subtree and repeat.