📜  须藤放置[1.4] | BST 遍历

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

须藤放置[1.4] | BST 遍历

给定要插入二叉搜索树的 N 个元素。任务是构造一个只有插入操作的二叉搜索树,最后打印后序遍历中的元素。 BST是根据元素的到达顺序构造的。

例子:

Input: N elements =  {8, 5, 10, 3, 4, 9, 7}
Output: 4 3 7 5 9 10 8

For the above input, the BST is: 
               8
             /    \
          5         10
        /   \      /
       3     7    9
       \
        4
The post-order traversal of the above BST is 4 3 7 5 9 10 8
 

方法:解决这个问题的方法是在BST中使用插入方法构造BST。插入所有节点后,打印树的后序遍历。

下面是上述方法的实现:

C++
// C++ program to insert nodes
// and print the postorder traversal
#include 
using namespace std;
 
// structure to store the BST
struct Node {
    int data;
    Node* left = NULL;
    Node* right = NULL;
};
 
// locates the memory space
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->data = key;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
 
// inserts node in the BST
Node* insertNode(Node* head, int key)
{
    // if first node
    if (head == NULL)
        head = newNode(key);
    else {
        // move to left
        if (key < head->data)
            head->left = insertNode(head->left, key);
        // move to right
        else
            head->right = insertNode(head->right, key);
    }
    return head;
}
 
// print the postorder traversal
void posOrder(Node* head)
{
    // leaf node is null
    if (head == NULL)
        return;
 
    // left
    posOrder(head->left);
 
    // right
    posOrder(head->right);
 
    // data
    cout << head->data << " ";
}
 
// Driver Code
int main()
{
 
    Node* root = NULL;
    root = insertNode(root, 8);
    root = insertNode(root, 5);
    root = insertNode(root, 10);
    root = insertNode(root, 3);
    root = insertNode(root, 4);
    root = insertNode(root, 9);
    root = insertNode(root, 7);
 
    // prints the postorder traversal of
    // the tree
    posOrder(root);
    cout << endl;
}


Java
// Java program to insert nodes
// and print the postorder traversal
class GFG {
 
    // structure to store the BST
    static class Node {
        int data;
        Node left = null;
        Node right = null;
    };
 
    // locates the memory space
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.data = key;
        temp.left = null;
        temp.right = null;
        return temp;
    }
 
    // inserts node in the BST
    static Node insertNode(Node head, int key)
    {
        // if first node
        if (head == null)
            head = newNode(key);
        else {
            // move to left
            if (key < head.data)
                head.left = insertNode(head.left, key);
            // move to right
            else
                head.right = insertNode(head.right, key);
        }
        return head;
    }
 
    // print the postorder traversal
    static void posOrder(Node head)
    {
        // leaf node is null
        if (head == null)
            return;
 
        // left
        posOrder(head.left);
 
        // right
        posOrder(head.right);
 
        // data
        System.out.print(head.data + " ");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        Node root = new Node();
        root = insertNode(root, 8);
        root = insertNode(root, 5);
        root = insertNode(root, 10);
        root = insertNode(root, 3);
        root = insertNode(root, 4);
        root = insertNode(root, 9);
        root = insertNode(root, 7);
 
        // prints the postorder traversal of
        // the tree
        posOrder(root);
        System.out.println("");
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python program to insert nodes
# and print the postorder traversal
import math
 
# structure to store the BST
class Node:
    def __init__(self,data):
        self.data = data
        self.left = None
        self.right=None
 
# locates the memory space
def newNode(key):
    temp = Node(key)
    temp.data = key
    temp.left = None
    temp.right = None
    return temp
 
# inserts node in the BST
def insertNode(head, key):
     
    # if first node
    if (head == None):
        head = newNode(key)
    else :
         
        # move to left
        if (key < head.data):
            head.left = insertNode(head.left, key)
         
        # move to right
        else:
            head.right = insertNode(head.right, key)
     
    return head
 
# print the postorder traversal
def posOrder(head):
     
    # leaf node is None
    if (head == None):
        return
 
    # left
    posOrder(head.left)
 
    # right
    posOrder(head.right)
 
    # data
    print(head.data,end = " ")
 
# Driver Code
if __name__=='__main__':
    root = None
    root = insertNode(root, 8)
    root = insertNode(root, 5)
    root = insertNode(root, 10)
    root = insertNode(root, 3)
    root = insertNode(root, 4)
    root = insertNode(root, 9)
    root = insertNode(root, 7)
 
    # prints the postorder traversal of
    # the tree
    posOrder(root)
 
# This code is contributed by Srathore


C#
// C# program to insert nodes
// and print the postorder traversal
using System;
 
class GFG {
 
    // structure to store the BST
    public class Node {
        public int data;
        public Node left = null;
        public Node right = null;
    };
 
    // locates the memory space
    static Node newNode(int key)
    {
        Node temp = new Node();
        temp.data = key;
        temp.left = null;
        temp.right = null;
        return temp;
    }
 
    // inserts node in the BST
    static Node insertNode(Node head, int key)
    {
        // if first node
        if (head == null)
            head = newNode(key);
        else {
            // move to left
            if (key < head.data)
                head.left = insertNode(head.left, key);
            // move to right
            else
                head.right = insertNode(head.right, key);
        }
        return head;
    }
 
    // print the postorder traversal
    static void posOrder(Node head)
    {
        // leaf node is null
        if (head == null)
            return;
 
        // left
        posOrder(head.left);
 
        // right
        posOrder(head.right);
 
        // data
        Console.Write(head.data + " ");
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        Node root = new Node();
        root = insertNode(root, 8);
        root = insertNode(root, 5);
        root = insertNode(root, 10);
        root = insertNode(root, 3);
        root = insertNode(root, 4);
        root = insertNode(root, 9);
        root = insertNode(root, 7);
 
        // prints the postorder traversal of
        // the tree
        posOrder(root);
        Console.WriteLine("");
    }
}
 
// This code contributed by Rajput-Ji


Javascript


输出:

4 3 7 5 9 10 8