📌  相关文章
📜  打印给定二叉树节点的祖先而不递归

📅  最后修改于: 2022-05-13 01:57:09.214000             🧑  作者: Mango

打印给定二叉树节点的祖先而不递归

给定一个二叉树和一个键,编写一个函数来打印给定二叉树中键的所有祖先。
例如,考虑以下二叉树

1
        /       \
       2         3
     /   \     /   \
    4     5    6    7 
   /       \       /
  8         9     10  

以下是上述树中的不同输入键及其祖先

Input Key    List of Ancestors 
-------------------------
 1            
 2            1
 3            1
 4            2 1
 5            2 1
 6            3 1
 7            3 1
 8            4 2 1
 9            5 2 1
10            7 3 1

此处讨论此问题的递归解决方案。
很明显,我们需要使用基于堆栈的二叉树迭代遍历。这个想法是当我们到达具有给定键的节点时,将所有祖先都放在堆栈中。一旦我们到达键,我们所要做的就是打印堆栈的内容。
当我们到达给定节点时,如何获取堆栈中的所有祖先?我们可以通过后序方式遍历所有节点。如果我们仔细观察递归后序遍历,我们可以很容易地观察到,当为一个节点调用递归函数时,递归调用堆栈包含该节点的祖先。所以想法是进行迭代后序遍历并在我们到达所需节点时停止遍历。
以下是上述方法的实现。

C
// C program to print all ancestors of a given key
#include 
#include 
 
// Maximum stack size
#define MAX_SIZE 100
 
// Structure for a tree node
struct Node
{
    int data;
    struct Node *left, *right;
};
 
// Structure for Stack
struct Stack
{
    int size;
    int top;
    struct Node* *array;
};
 
// A utility function to create a new tree node
struct Node* newNode(int data)
{
    struct Node* node = (struct Node*) malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}
 
// A utility function to create a stack of given size
struct Stack* createStack(int size)
{
    struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
    stack->size = size;
    stack->top = -1;
    stack->array = (struct Node**) malloc(stack->size * sizeof(struct Node*));
    return stack;
}
 
// BASIC OPERATIONS OF STACK
int isFull(struct Stack* stack)
{
    return ((stack->top + 1) == stack->size);
}
int isEmpty(struct Stack* stack)
{
    return stack->top == -1;
}
void push(struct Stack* stack, struct Node* node)
{
    if (isFull(stack))
        return;
    stack->array[++stack->top] = node;
}
struct Node* pop(struct Stack* stack)
{
    if (isEmpty(stack))
        return NULL;
    return stack->array[stack->top--];
}
struct Node* peek(struct Stack* stack)
{
    if (isEmpty(stack))
        return NULL;
    return stack->array[stack->top];
}
 
// Iterative Function to print all ancestors of a given key
void printAncestors(struct Node *root, int key)
{
    if (root == NULL) return;
 
    // Create a stack to hold ancestors
    struct Stack* stack = createStack(MAX_SIZE);
 
    // Traverse the complete tree in postorder way till we find the key
    while (1)
    {
        // Traverse the left side. While traversing, push the nodes into
        // the stack so that their right subtrees can be traversed later
        while (root && root->data != key)
        {
            push(stack, root);   // push current node
            root = root->left;  // move to next node
        }
 
        // If the node whose ancestors are to be printed is found,
        // then break the while loop.
        if (root && root->data == key)
            break;
 
        // Check if right sub-tree exists for the node at top
        // If not then pop that node because we don't need this
        // node any more.
        if (peek(stack)->right == NULL)
        {
            root = pop(stack);
 
            // If the popped node is right child of top, then remove the top
            // as well. Left child of the top must have processed before.
            // Consider the following tree for example and key = 3.  If we
            // remove the following loop, the program will go in an
            // infinite loop after reaching 5.
            //          1
            //        /   \
            //       2     3
            //         \
            //           4
            //             \
            //              5
            while (!isEmpty(stack) && peek(stack)->right == root)
               root = pop(stack);
        }
 
        // if stack is not empty then simply set the root as right child
        // of top and start traversing right sub-tree.
        root = isEmpty(stack)? NULL: peek(stack)->right;
    }
 
    // If stack is not empty, print contents of stack
    // Here assumption is that the key is there in tree
    while (!isEmpty(stack))
        printf("%d ", pop(stack)->data);
}
 
// Driver program to test above functions
int main()
{
    // Let us construct a binary tree
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->left->left->left = newNode(8);
    root->left->right->right = newNode(9);
    root->right->right->left = newNode(10);
 
    printf("Following are all keys and their ancestors\n");
    for (int key = 1; key <= 10; key++)
    {
        printf("%d: ", key);
        printAncestors(root, key);
        printf("\n");
    }
 
    getchar();
    return 0;
}


C++
// C++ program to print all ancestors of a given key
#include 
using namespace std;
 
 
// Structure for a tree node
struct Node
{
    int data;
    struct Node *left, *right;
};
 
// A utility function to create a new tree node
struct Node* newNode(int data)
{
    struct Node* node = (struct Node*) malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return node;
}
 
// Iterative Function to print all ancestors of a given key
void printAncestors(struct Node *root, int key)
{
    if (root == NULL) return;
 
    // Create a stack to hold ancestors
    stack st;
 
    // Traverse the complete tree in postorder way till we find the key
    while (1)
    {
        // Traverse the left side. While traversing, push the nodes into
        // the stack so that their right subtrees can be traversed later
        while (root && root->data != key)
        {
            st.push(root);   // push current node
            root = root->left;  // move to next node
        }
 
        // If the node whose ancestors are to be printed is found,
        // then break the while loop.
        if (root && root->data == key)
            break;
 
        // Check if right sub-tree exists for the node at top
        // If not then pop that node because we don't need this
        // node any more.
        if (st.top()->right == NULL)
        {
            root = st.top();
            st.pop();
 
            // If the popped node is right child of top, then remove the top
            // as well. Left child of the top must have processed before.
 
            while (!st.empty() && st.top()->right == root)
               {root = st.top();
               st.pop();
               }
        }
 
        // if stack is not empty then simply set the root as right child
        // of top and start traversing right sub-tree.
        root = st.empty()? NULL: st.top()->right;
    }
 
    // If stack is not empty, print contents of stack
    // Here assumption is that the key is there in tree
    while (!st.empty())
    {
        cout<data<<" ";
        st.pop();
    }
}
 
// Driver program to test above functions
int main()
{
    // Let us construct a binary tree
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);
    root->right->left = newNode(6);
    root->right->right = newNode(7);
    root->left->left->left = newNode(8);
    root->left->right->right = newNode(9);
    root->right->right->left = newNode(10);
 
    cout<<"Following are all keys and their ancestors"<


Java
// Java program to print all ancestors of a given key
import java.util.Stack;
 
public class GFG
{
    // Class for a tree node
    static class Node
    {
        int data;
        Node left,right;
         
        // constructor to create Node
        // left and right are by default null
        Node(int data)
        {
            this.data = data;
        }
    }
     
    // Iterative Function to print all ancestors of a given key
    static void printAncestors(Node root,int key)
    {
        if(root == null)
            return;
         
         // Create a stack to hold ancestors
        Stack st = new Stack<>();
         
        // Traverse the complete tree in postorder way till we find the key
        while(true)
        {
             
            // Traverse the left side. While traversing, push the nodes into
            // the stack so that their right subtrees can be traversed later
            while(root != null && root.data != key)
            {
                st.push(root);   // push current node
                root = root.left;   // move to next node
            }
             
            // If the node whose ancestors are to be printed is found,
            // then break the while loop.
            if(root != null && root.data == key)
                break;
             
            // Check if right sub-tree exists for the node at top
            // If not then pop that node because we don't need this
            // node any more.
            if(st.peek().right == null)
            {
                root =st.peek();
                st.pop();
                 
                // If the popped node is right child of top, then remove the top
                // as well. Left child of the top must have processed before.
                while( st.empty() == false && st.peek().right == root)
                {
                    root = st.peek();
                    st.pop();
                }
            }
             
            // if stack is not empty then simply set the root as right child
            // of top and start traversing right sub-tree.
            root = st.empty() ? null : st.peek().right;
        }
         
        // If stack is not empty, print contents of stack
        // Here assumption is that the key is there in tree
        while( !st.empty() )
        {
            System.out.print(st.peek().data+" ");
            st.pop();
        }
    }
     
    // Driver program to test above functions
    public static void main(String[] args)
    {
         // Let us construct a binary tree
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right.left = new Node(6);
        root.right.right = new Node(7);
        root.left.left.left = new Node(8);
        root.left.right.right = new Node(9);
        root.right.right.left = new Node(10);
         
        System.out.println("Following are all keys and their ancestors");
        for(int key = 1;key <= 10;key++)
        {
            System.out.print(key+": ");
            printAncestors(root, key);
            System.out.println();
        }
    }
 
}
//This code is Contributed by Sumit Ghosh


Python3
# Python3 program to print all ancestors of a given key
 
# Class for a tree node
class Node:
 
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Iterative Function to print all ancestors of a given key
def printAncestors(root, key):
    if(root == None):
        return;
     
     # Create a stack to hold ancestors
    st = []
     
    # Traverse the complete tree in postorder way till we find the key
    while(True):
         
        # Traverse the left side. While traversing, append the nodes into
        # the stack so that their right subtrees can be traversed later
        while(root != None and root.data != key):
         
            st.append(root);   # append current node
            root = root.left;   # move to next node
         
        # If the node whose ancestors are to be printed is found,
        # then break the while loop.
        if(root != None and root.data == key):
            break;
         
        # Check if right sub-tree exists for the node at top
        # If not then pop that node because we don't need this
        # node any more.
        if(st[-1].right == None):
            root = st[-1];
            st.pop();
             
            # If the popped node is right child of top, then remove the top
            # as well. Left child of the top must have processed before.
            while(len(st) != 0 and st[-1].right == root):
                root = st[-1];
                st.pop();
         
        # if stack is not empty then simply set the root as right child
        # of top and start traversing right sub-tree.
        root = None if len(st) == 0 else st[-1].right;
     
    # If stack is not empty, print contents of stack
    # Here assumption is that the key is there in tree
    while(len(st) != 0):
        print(st[-1].data, end = " ")
        st.pop();
     
# Driver program to test above functions
if __name__=='__main__':
 
     # Let us construct a binary tree
    root = Node(1);
    root.left = Node(2);
    root.right = Node(3);
    root.left.left = Node(4);
    root.left.right = Node(5);
    root.right.left = Node(6);
    root.right.right = Node(7);
    root.left.left.left = Node(8);
    root.left.right.right = Node(9);
    root.right.right.left = Node(10);
     
    print("Following are all keys and their ancestors");
    for key in range(1, 11):
 
        print(key, end = ": ");
        printAncestors(root, key);
        print();
     
# This code is contributed by rutvik_56.


C#
// C# program to print all ancestors
// of a given key
using System;
using System.Collections.Generic;
 
class GFG
{
// Class for a tree node
public class Node
{
    public int data;
    public Node left, right;
 
    // constructor to create Node
    // left and right are by default null
    public Node(int data)
    {
        this.data = data;
    }
}
 
// Iterative Function to print
// all ancestors of a given key
public static void printAncestors(Node root,
                                  int key)
{
    if (root == null)
    {
        return;
    }
 
    // Create a stack to hold ancestors
    Stack st = new Stack();
 
    // Traverse the complete tree in
    // postorder way till we find the key
    while (true)
    {
 
        // Traverse the left side. While
        // traversing, push the nodes into
        // the stack so that their right
        // subtrees can be traversed later
        while (root != null && root.data != key)
        {
            st.Push(root); // push current node
            root = root.left; // move to next node
        }
 
        // If the node whose ancestors
        // are to be printed is found,
        // then break the while loop.
        if (root != null && root.data == key)
        {
            break;
        }
 
        // Check if right sub-tree exists for
        // the node at top. If not then pop
        // that node because we don't need
        // this node any more.
        if (st.Peek().right == null)
        {
            root = st.Peek();
            st.Pop();
 
            // If the popped node is right child
            // of top, then remove the top as well.
            // Left child of the top must have
            // processed before.
            while (st.Count > 0 &&
                   st.Peek().right == root)
            {
                root = st.Peek();
                st.Pop();
            }
        }
 
        // if stack is not empty then simply
        // set the root as right child of top
        // and start traversing right sub-tree.
        root = st.Count == 0 ?
                        null : st.Peek().right;
    }
 
    // If stack is not empty, print contents
    // of stack. Here assumption is that the
    // key is there in tree
    while (st.Count > 0)
    {
        Console.Write(st.Peek().data + " ");
        st.Pop();
    }
}
 
// Driver Code
public static void Main(string[] args)
{
    // Let us construct a binary tree
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.left.right = new Node(5);
    root.right.left = new Node(6);
    root.right.right = new Node(7);
    root.left.left.left = new Node(8);
    root.left.right.right = new Node(9);
    root.right.right.left = new Node(10);
 
    Console.WriteLine("Following are all keys " +
                          "and their ancestors");
    for (int key = 1; key <= 10; key++)
    {
        Console.Write(key + ": ");
        printAncestors(root, key);
        Console.WriteLine();
    }
}
}
 
// This code is contributed by Shrikant13


Javascript


输出:

Following are all keys and their ancestors
1:
2: 1
3: 1
4: 2 1
5: 2 1
6: 3 1
7: 3 1
8: 4 2 1
9: 5 2 1
10: 7 3 1