📜  数据结构示例-在二叉树中找到最小的元素

📅  最后修改于: 2020-10-15 06:40:14             🧑  作者: Mango

问:程序以查找二叉树中的最小元素。

说明

在此程序中,我们将找出给定二叉树中的最小节点。我们首先定义变量min,它将保存根的数据。然后,我们遍历左子树以找到左子树中的最小节点。将其与最小值进行比较,并在变量min中存储最小值2。然后,我们遍历右子树以找到最小的节点,并将其与min进行比较。最后,min将具有最小的节点。

上图表示一棵二叉树。最初,min将保持4。通过左子树递归。

min = 4, leftMin = 2    =>  (2 < 4) then min = 2
min = 2, leftMin = 1    =>  (1 < 2) then min = 1

通过右子树递归。

min = 1, rightMin = 3  =>  (1 < 3) then min = 1
Recurse in left subtree of 5
min = 1, leftMin = 5    =>  (1 < 5) then min = 1

在3的右子树中递归

min = 1, rightMin = 6  =>  (1 < 6) then min = 1

因此,以上二叉树中的最小节点为1。

算法

  • 定义Node类,它具有三个属性,即:data,left和right。在此,左代表节点的左子节点,右代表节点的右子节点。
  • 创建节点时,数据将传递到该节点的data属性,并且左右都将设置为null。
  • 定义另一个具有属性根的类。
    1. 表示树的根节点,并将其初始化为null。
  • minimumElement()将找出二叉树中的最小节点:
    1. 它检查root是否为null ,这意味着tree为空。
    2. 如果tree不为空,则定义一个变量min,该变量将存储临时数据。
    3. 通过递归调用minimumElement()找出左子树中的最小节点。将该值存储在leftMin中。将min的值与leftMin进行比较,并将最小值2存储到min。
    4. 通过递归调用smallestElement()找出右侧子树中的最小节点。将该值存储在rightMin中。将min的值与rightMin进行比较,并将最大值2存储到min。
    5. 最后,min将保留二叉树中的最小节点。

示例:

Python

#Represent a node of binary tree
class Node:
    def __init__(self,data):
        #Assign data to the new node, set left and right children to None
        self.data = data;
        self.left = None;
        self.right = None;
 
class SmallestNode:
    def __init__(self):
        #Represent the root of binary tree
        self.root = None;
    
    #smallestElement() will find out the smallest node in the binary tree
    def smallestElement(self, temp):
        #Check whether tree is empty
        if(self.root == None):
            print("Tree is empty");
            return 0;
        
        else:
            #Variable minimum will store temp's data
            minimum = temp.data;
            
            #It will find smallest element in left subtree
            if(temp.left != None):
                leftMin = self.smallestElement(temp.left);
                #If value of minimum is greater than leftMin then store the value of leftMin into minimum
                minimum = min(minimum, leftMin);
                
            #It will find smallest element in right subtree
            if(temp.right != None):
                rightMin = self.smallestElement(temp.right);
                #If value of minimum is greater than rightMin then store the value of rightMin into minimum
                minimum = min(minimum, rightMin);
            
            return minimum;
 
bt = SmallestNode();
#Add nodes to the binary tree
bt.root = Node(4);
bt.root.left = Node(2);
bt.root.right = Node(3);
bt.root.left.left = Node(1);
bt.root.right.left = Node(5);
bt.root.right.right = Node(6);
 
#Display smallest node in the binary tree
print("Smallest element in the binary tree: " + str(bt.smallestElement(bt.root)));

输出:

Smallest element in the binary tree: 1

C

#include 
#include 
#include 
 
//Represent a node of binary tree
struct node{
    int data;
    struct node *left;
    struct node *right;
};
 
//Represent the root of binary tree
struct node *root = NULL;
 
//createNode() will create a new node
struct node* createNode(int data){
    //Create a new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    //Assign data to newNode, set left and right children to NULL
    newNode->data = data;
    newNode->left = NULL;
    newNode->right = NULL;
    
    return newNode;
}
 
//smallestElement() will find out the smallest node in the binary tree
int smallestElement(struct node *temp){
    //Check whether tree is empty
    if(root == NULL) {
        printf("Tree is empty\n");
        return 0;
    }
    else{
        int leftMin, rightMin;
        //Min will store temp's data
        int min = temp->data;
        
        //It will find smallest element in left subtree
        if(temp->left != NULL){
            leftMin = smallestElement(temp->left);
            //If min is greater than leftMin then store the value of leftMin into min
            min = (min > leftMin) ? leftMin : min;
        }
      
        //It will find smallest element in right subtree
        if(temp->right != NULL){
            rightMin = smallestElement(temp->right);
            //If min is greater than rightMin then store the value of rightMin into min
            min = (min > rightMin) ? rightMin : min;
        }
    return min;
    }
}
      
int main()
{
    //Add nodes to the binary tree
    root = createNode(4);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(1);
    root->right->left = createNode(5);
    root->right->right = createNode(6);
        
    //Display smallest node in the binary tree
    printf("Smallest element in the binary tree: %d", smallestElement(root));
    return 0;
}

输出:

Smallest element in the binary tree: 1

JAVA

public class SmallestNode {
      //Represent the node of binary tree
      public static class Node{
        int data;
        Node left;
        Node right;
        
        public Node(int data){
            //Assign data to the new node, set left and right children to null
            this.data = data;
            this.left = null;
            this.right = null;
        }
      }
      
      //Represent the root of binary tree
      public Node root;
      
      public SmallestNode(){
          root = null;
      }
      
      //smallestElement() will find out the smallest node in the binary tree
      public int smallestElement(Node temp){
          //Check whether tree is empty
          if(root == null) {
              System.out.println("Tree is empty");
              return 0;
          }
          else {
                int leftMin, rightMin;
                //Min will store temp's data
                int min = temp.data;
                
                //It will find smallest element in left subtree
                if(temp.left != null){
                  leftMin = smallestElement(temp.left);
                  //If min is greater than leftMin then store the value of leftMin into min
                  min = Math.min(min, leftMin);
                }
                
                //It will find smallest element in right subtree
                if(temp.right != null){
                  rightMin = smallestElement(temp.right);
                  //If min is greater than rightMin then store the value of rightMin into min
                  min = Math.min(min, rightMin);
                }
                return min;
          }
      }
      
      public static void main(String[] args) {
        
        SmallestNode bt = new SmallestNode();
        //Add nodes to the binary tree
        bt.root = new Node(4);
        bt.root.left = new Node(2);
        bt.root.right = new Node(3);
        bt.root.left.left = new Node(1);
        bt.root.right.left = new Node(5);
        bt.root.right.right = new Node(6);
        
        //Display smallest node in the binary tree
        System.out.println("Smallest element in the binary tree: " + bt.smallestElement(bt.root));
      }
}

输出:

Smallest element in the binary tree: 1

C#

using System; 
namespace Tree 
{                     
    public class Program
    {
        //Represent a node of binary tree
        public class Node{
            public T data;
            public Node left;
            public Node right;
            
            public Node(T data) {
                //Assign data to the new node, set left and right children to NULL
                this.data = data;
                this.left = null;
                this.right = null;
            }
        }
        
        public class SmallestNode where T : IComparable{
            //Represent the root of binary tree
            public Node root;
 
            public SmallestNode(){
                root = null;
            }
        
        //smallestElement() will find out the smallest node in the binary tree
        public T smallestElement(Node temp){
            //Check whether tree is empty
            if(root == null) {
                Console.WriteLine("Tree is empty");
                return default(T);
            }
            T leftMin, rightMin;
            //Min will store temp's data
            T min = temp.data;
 
            //It will find smallest element in left subtree
            if(temp.left != null){
                leftMin = smallestElement(temp.left);
                //If min is greater than leftMin then store the value of leftMin into min
                min = (min.CompareTo(leftMin) > 0) ? leftMin : min;
            }
 
            //It will find smallest element in right subtree
            if(temp.right != null){
                rightMin = smallestElement(temp.right);
                //If min is greater than rightMin then store the value of rightMin into min
                min = (min.CompareTo(rightMin) > 0) ? rightMin : min;
            }
            return min;
          }
    }
        
    public static void Main()
    {
        SmallestNode bt = new SmallestNode();
        //Add nodes to the binary tree
        bt.root = new Node(4);
        bt.root.left = new Node(2);
        bt.root.right = new Node(3);
        bt.root.left.left = new Node(1);
        bt.root.right.left = new Node(5);
        bt.root.right.right = new Node(6);
        
        //Display smallest node in the binary tree
        Console.WriteLine("Smallest element in the binary tree: " + bt.smallestElement(bt.root));
        }    
    }
}

输出:

Smallest element in the binary tree: 1

PHP:




data = $data;
        $this->left = NULL;
        $this->right = NULL;
    }
}
class SmallestNode{
    //Represent the root of binary tree
    public $root;
    function __construct(){
        $this->root = NULL;
    }
    
     //smallestElement() will find out the smallest node in the binary tree
     function smallestElement($temp){
          //Check whether tree is empty
          if($this->root == NULL) {
              print "Tree is empty
"; return 0; } else { //$min will store $temp's data $min = $temp->data; //It will find smallest element in left subtree if($temp->left != NULL){ $leftMin = $this->smallestElement($temp->left); //If $min is greater than $leftMin then store the value of $leftMin into $min $min = min($min, $leftMin); } //It will find smallest element in right subtree if($temp->right != NULL){ $rightMin = $this->smallestElement($temp->right); //If $min is greater than $rightMin then store the value of $rightMin into $min $min = min($min, $rightMin); } return $min; } } } $bt = new SmallestNode(); //Add nodes to the binary tree $bt->root = new Node(4); $bt->root->left = new Node(2); $bt->root->right = new Node(3); $bt->root->left->left = new Node(1); $bt->root->right->left = new Node(5); $bt->root->right->right = new Node(6); //Display smallest node in the binary tree print "Smallest element in the binary tree: " . $bt->smallestElement($bt->root); ?>

输出:

Smallest element in the binary tree: 1