Javatpoint Logo
Javatpoint Logo

Print Ancestors of a given node in Binary Tree

Introduction:

Binary trees are fundamental data structures in computer science that organize data in a hierarchical manner. They consist of nodes, each having at most two children - a left child and a right child. Understanding and manipulating binary trees is crucial in various applications, and one common task is finding and printing the ancestors of a given node.

Understanding Binary Trees:

A binary tree is a hierarchical data structure where each node has at most two children, referred to as the left child and the right child. The topmost node is called the root, and nodes with no children are known as leaves. The relationship between nodes forms a tree-like structure, making binary trees efficient for searching, insertion, and deletion operations.

Importance of Ancestors in Binary Trees:

An ancestor of a node in a binary tree is any node that lies on the path from the root to that specific node. Understanding the ancestors of a node is crucial in various scenarios, such as:

  • Tree Traversal: Ancestors play a vital role in tree traversal algorithms like pre-order, in-order, and post-order, helping to process nodes in a specific order.
  • Path Finding: Identifying ancestors is essential when determining the path from the root to a given node, providing insights into the structure and organization of the tree.

Understanding the Problem:

Before we delve into the implementation, let's understand the problem. Given a binary tree and a specific node in that tree, the goal is to print all the ancestors of the given node. An ancestor of a node is any node that lies on the path from the root to that node.

Approach:

Recursive Approach:

To solve this problem, we can use a recursive approach. We start from the root of the tree and traverse down to the target node. While traversing, we keep track of the nodes visited in a vector or a stack. Once we reach the target node, we print the nodes in the vector or stack, which represents the ancestors of the given node.

Implementation:

Explanation:

  • The program defines a basic structure for a binary tree node named Node. Each node has an integer data value (int data), and two pointers (Node* left and Node* right) pointing to its left and right children.
  • The constructor initializes the node with a given value, setting the left and right pointers to nullptr.
  • The printAncestors Function takes three parameters - the root of the binary tree (Node* root), the value of the target node (int target), and a vector of integers (vector& ancestors) to store the ancestors.
  • The function returns a boolean indicating whether the target node is found in the tree. If the target is found, the ancestors are stored in the vector.
  • The printVector Function, A utility function is provided to print the elements of a vector.
  • The main function serves as the program's entry point. It creates a sample binary tree with integer values and sets up a target node value (targetNodeValue).
  • It then calls the printAncestors function to find the ancestors of the target node. If the target node is found, it prints the ancestors; otherwise, it informs that the node is not present in the tree.

Program Output:

Print Ancestors of a given node in Binary Tree

Iterative Approach:

We will use an iterative approach to solve this problem. The idea is to start from the root of the tree and traverse down to the target node while keeping track of the visited nodes. As we move down the tree, we will push the nodes onto a stack. Once we reach the target node or a leaf node, we will pop the nodes from the stack and print them, as they are the ancestors of the target node.

Implementation:

Explanation:

  • The program starts by defining a structure named Node, which represents a node in a binary tree. Each node contains an integer value (data), and pointers to its left and right
  • The constructor initializes the node with a given value and sets the left and right pointers to
  • The program provides a function called printAncestors that takes the root of a binary tree and a target node value as parameters. It uses a stack to traverse the tree and print the ancestors of the target node.
  • The function employs an iterative approach to traverse the binary tree using a stack. It starts from the root and moves to the leftmost leaf, pushing each node onto the stack.
  • When it reaches a leaf node or a node with no left child, it checks if the right child's data matches the target value.
  • If the right child's data matches the target, it means the target is found, and the ancestors are printed by popping elements from the stack. The function then returns.
  • The main function provides an example usage of the printAncestors It creates a binary tree with a root node and several child nodes, sets a target node value, and calls the printAncestors function to print the ancestors of the target node.

Program Output:

Print Ancestors of a given node in Binary Tree

Conclusion:

In conclusion, the problem of printing ancestors of a given node in a binary tree involves navigating through the ancestral path from the given node to the root of the tree. This task is commonly approached using recursive algorithms, which prove effective in traversing the tree structure and identifying the ancestors of the specified node.

One key observation is that the ancestors are essentially the nodes encountered on the path from the root to the target node. By recursively moving up the tree and recording the nodes along the way, we can systematically print the ancestors in the desired order. This process relies on the inherent hierarchical structure of binary trees, where each node has at most two children.

Efficiency considerations are important in implementing solutions for this problem. The recursive approach offers a clear and concise solution, leveraging the natural recursive structure of the binary tree. However, it is crucial to manage the complexity of the algorithm, especially in large trees, by optimizing for both time and space efficiency.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA