热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

打印二叉查找树所有偶数节点

打印二叉查找树所有偶数节点原文:https://www . geesforgeks . org/print-all-even-n

打印二叉查找树所有偶数节点

原文:https://www . geesforgeks . org/print-all-even-nodes-of-binary-search-tree/

给一个二叉查找树。任务是打印二叉查找树的所有偶数节点。
例:

Input :
5
/ \
3 7
/ \ / \
2 4 6 8
Output : 2 4 6 8
Input :
14
/ \
12 17
/ \ / \
8 13 16 19
Output : 8 12 14 16

方法:遍历二叉查找树,检查当前节点的值是否为偶数。如果是,则打印,否则跳过该节点。
以下是上述办法的实施情况:

C++

// C++ program to print all even node of BST
#include
using namespace std;
// create Tree
struct Node {
    int key;
    struct Node *left, *right;
};
// A utility function to create a new BST node
Node* newNode(int item)
{
    Node* temp = new Node;
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}
// A utility function to do inorder traversal of BST
void inorder(Node* root)
{
    if (root != NULL) {
        inorder(root->left);
        printf(root->key,end=" ");
        inorder(root->right);
    }
}
/* A utility function to insert a new node
   with given key in BST */
Node* insert(Node* node, int key)
{
    /* If the tree is empty, return a new node */
    if (node == NULL)
        return newNode(key);
    /* Otherwise, recur down the tree */
    if (key key)
        node->left = insert(node->left, key);
    else
        node->right = insert(node->right, key);
    /* return the (unchanged) node pointer */
    return node;
}
// Function to print all even nodes
void evenNode(Node* root)
{
    if (root != NULL) {
        evenNode(root->left);
        // if node is even then print it
        if (root->key % 2 == 0)
            printf("%d ", root->key);
        evenNode(root->right);
    }
}
// Driver Code
int main()
{
    /* Let us create following BST
       5
      / \
     3     7
    / \ / \
    2 4 6 8 */
    Node* root = NULL;
    root = insert(root, 5);
    root = insert(root, 3);
    root = insert(root, 2);
    root = insert(root, 4);
    root = insert(root, 7);
    root = insert(root, 6);
    root = insert(root, 8);
    evenNode(root);
    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to print all even node of BST
class GfG {
// create Tree
static class Node {
    int key;
    Node left, right;
}
// A utility function to create a new BST node
static Node newNode(int item)
{
    Node temp = new Node();
    temp.key = item;
    temp.left = null;
    temp.right = null;
    return temp;
}
// A utility function to do inorder traversal of BST
static void inorder(Node root)
{
    if (root != null) {
        inorder(root.left);
        System.out.print(root.key + " ");
        inorder(root.right);
    }
}
/* A utility function to insert a new node
with given key in BST */
static Node insert(Node node, int key)
{
    /* If the tree is empty, return a new node */
    if (node == null)
        return newNode(key);
    /* Otherwise, recur down the tree */
    if (key         node.left = insert(node.left, key);
    else
        node.right = insert(node.right, key);
    /* return the (unchanged) node pointer */
    return node;
}
// Function to print all even nodes
static void evenNode(Node root)
{
    if (root != null) {
        evenNode(root.left);
        // if node is even then print it
        if (root.key % 2 == 0)
            System.out.print(root.key + " ");
        evenNode(root.right);
    }
}
// Driver Code
public static void main(String[] args)
{
    /* Let us create following BST
    5
    / \
    3     7
    / \ / \
    2 4 6 8 */
    Node root = null;
    root = insert(root, 5);
    root = insert(root, 3);
    root = insert(root, 2);
    root = insert(root, 4);
    root = insert(root, 7);
    root = insert(root, 6);
    root = insert(root, 8);
    evenNode(root);
}
}

Python 3

# Python3 program to print all even node of BST
# create Tree
# to create a new BST node
class newNode:
    # Construct to create a new node
    def __init__(self, key):
        self.key = key
        self.left = None
        self.right = None
# A utility function to do inorder
# traversal of BST
def inorder(root) :
    if (root != None):
        inorder(root.left)
        printf("%d ", root.key)
        inorder(root.right)
""" A utility function to insert a new
node with given key in BST """
def insert(node, key):
    """ If the tree is empty,
    return a new node """
    if (node == None):
        return newNode(key)
    """ Otherwise, recur down the tree """
    if (key         node.left = insert(node.left, key)
    else:
        node.right = insert(node.right, key)
    """ return the (unchanged)
        node pointer """
    return node
# Function to print all even nodes
def evenNode(root) :
    if (root != None):
        evenNode(root.left)
        # if node is even then print it
        if (root.key % 2 == 0):
            print(root.key, end = " ")
        evenNode(root.right)
# Driver Code
if __name__ == '__main__':
    """ Let us create following BST
    5
    / \
    3 7
    / \ / \
    2 4 6 8 """
    root = None
    root = insert(root, 5)
    root = insert(root, 3)
    root = insert(root, 2)
    root = insert(root, 4)
    root = insert(root, 7)
    root = insert(root, 6)
    root = insert(root, 8)
    evenNode(root)
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

C

// C# program to print all even node of BST
using System;
class GfG
{
    // create Tree
    class Node
    {
        public int key;
        public Node left, right;
    }
    // A utility function to
    // create a new BST node
    static Node newNode(int item)
    {
        Node temp = new Node();
        temp.key = item;
        temp.left = null;
        temp.right = null;
        return temp;
    }
    // A utility function to do
    // inorder traversal of BST
    static void inorder(Node root)
    {
        if (root != null)
        {
            inorder(root.left);
            Console.Write(root.key + " ");
            inorder(root.right);
        }
    }
    /* A utility function to insert a new node
    with given key in BST */
    static Node insert(Node node, int key)
    {
        /* If the tree is empty, return a new node */
        if (node == null)
            return newNode(key);
        /* Otherwise, recur down the tree */
        if (key             node.left = insert(node.left, key);
        else
            node.right = insert(node.right, key);
        /* return the (unchanged) node pointer */
        return node;
    }
    // Function to print all even nodes
    static void evenNode(Node root)
    {
        if (root != null)
        {
            evenNode(root.left);
            // if node is even then print it
            if (root.key % 2 == 0)
                Console.Write(root.key + " ");
            evenNode(root.right);
        }
    }
    // Driver Code
    public static void Main(String[] args)
    {
        /* Let us create following BST
        5
        / \
        3 7
        / \ / \
        2 4 6 8 */
        Node root = null;
        root = insert(root, 5);
        root = insert(root, 3);
        root = insert(root, 2);
        root = insert(root, 4);
        root = insert(root, 7);
        root = insert(root, 6);
        root = insert(root, 8);
        evenNode(root);
    }
}
// This code has been contributed
// by PrinciRaj1992

java 描述语言


Output: 

2 4 6 8

推荐阅读
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 李逍遥寻找仙药的迷阵之旅
    本文讲述了少年李逍遥为了救治婶婶的病情,前往仙灵岛寻找仙药的故事。他需要穿越一个由M×N个方格组成的迷阵,有些方格内有怪物,有些方格是安全的。李逍遥需要避开有怪物的方格,并经过最少的方格,找到仙药。在寻找的过程中,他还会遇到神秘人物。本文提供了一个迷阵样例及李逍遥找到仙药的路线。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 本文介绍了Codeforces Round #321 (Div. 2)比赛中的问题Kefa and Dishes,通过状压和spfa算法解决了这个问题。给定一个有向图,求在不超过m步的情况下,能获得的最大权值和。点不能重复走。文章详细介绍了问题的题意、解题思路和代码实现。 ... [详细]
author-avatar
白闹
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有