📅  最后修改于: 2020-10-15 06:40:52             🧑  作者: Mango
树是非线性数据结构,用于分层存储数据。树是称为节点的元素的集合。节点通过边连接并包含数据。树的第一个节点称为根。每个节点可能有或没有子节点。没有任何子节点的节点称为叶子。
二叉树是另一种树数据结构,其中每个节点最多可以有两个孩子。也就是说,二叉树中的每个节点都会有数据,即左子节点和右子节点。
上图表示二叉树,其中1表示树的根节点。节点2的左子节点为4,节点3的左子节点为5,右子节点为6。节点4,5和6是叶节点,因为它们没有任何子节点。
在此程序中,我们将在二叉树中搜索特定值。如果存在,则print消息“二叉树中存在元素”,否则print消息“二叉树中不存在元素”。简而言之,我们首先将根的数据与要搜索的节点的数据进行比较。如果找到匹配项,则将标志设置为true。否则,在左子树中搜索节点,然后在右子树中搜索。
#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 SearchBinaryTree:
def __init__(self):
#Represent the root of binary tree
self.root = None;
self.flag = False;
#searchNode() will search for the particular node in the binary tree
def searchNode(self, temp, value):
#Check whether tree is empty
if(self.root == None):
print("Tree is empty");
else:
#If value is found in the given binary tree then, set the flag to true
if(temp.data == value):
self.flag = True;
return;
#Search in left subtree
if(self.flag == False and temp.left != None):
self.searchNode(temp.left, value);
#Search in right subtree
if(self.flag == False and temp.right != None):
self.searchNode(temp.right, value);
bt = SearchBinaryTree();
#Add nodes to the binary tree
bt.root = Node(1);
bt.root.left = Node(2);
bt.root.right = Node(3);
bt.root.left.left = Node(4);
bt.root.right.left = Node(5);
bt.root.right.right = Node(6);
#Search for node 5 in the binary tree
bt.searchNode(bt.root, 5);
if(bt.flag):
print("Element is present in the binary tree");
else:
print("Element is not present in the binary tree");
输出:
Element is present in the binary tree
#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;
static bool flag = false;
//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;
}
//searchNode() will search for the particular node in the binary tree
void searchNode(struct node *temp, int value){
//Check whether tree is empty
if(root == NULL){
printf("Tree is empty\n");
}
else{
//If value is found in the given binary tree then, set the flag to true
if(temp->data == value){
flag = true;
return;
}
//Search in left subtree
if(flag == false && temp->left != NULL){
searchNode(temp->left, value);
}
//Search in right subtree
if(flag == false && temp->right != NULL){
searchNode(temp->right, value);
}
}
}
int main()
{
//Add nodes to the binary tree
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->right->left = createNode(5);
root->right->right = createNode(6);
//Search for node 5 in the binary tree
searchNode(root, 5);
if(flag)
printf("Element is present in the binary tree");
else
printf("Element is not present in the binary tree");
return 0;
}
输出:
Element is present in the binary tree
public class SearchBinaryTree {
//Represent a 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 static boolean flag = false;
public SearchBinaryTree(){
root = null;
}
//searchNode() will search for the particular node in the binary tree
public void searchNode(Node temp, int value){
//Check whether tree is empty
if(root == null){
System.out.println("Tree is empty");
}
else{
//If value is found in the given binary tree then, set the flag to true
if(temp.data == value){
flag = true;
return;
}
//Search in left subtree
if(flag == false && temp.left != null){
searchNode(temp.left, value);
}
//Search in right subtree
if(flag == false && temp.right != null){
searchNode(temp.right, value);
}
}
}
public static void main(String[] args) {
SearchBinaryTree bt = new SearchBinaryTree();
//Add nodes to the binary tree
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.right.left = new Node(5);
bt.root.right.right = new Node(6);
//Search for node 5 in the binary tree
bt.searchNode(bt.root, 5);
if(flag)
System.out.println("Element is present in the binary tree");
else
System.out.println("Element is not present in the binary tree");
}
}
输出:
Element is present in the binary tree
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 SearchBinaryTree{
//Represent the root of binary tree
public Node root;
public static Boolean flag = false;
public SearchBinaryTree(){
root = null;
}
//searchNode() will search for the particular node in the binary tree
public void searchNode(Node temp, T value){
//Check whether tree is empty
if(root == null){
Console.WriteLine("Tree is empty");
}
else{
//If value is found in the given binary tree then, set the flag to true
if(temp.data.Equals(value)){
flag = true;
return;
}
//Search in left subtree
if(flag == false && temp.left != null){
searchNode(temp.left, value);
}
//Search in right subtree
if(flag == false && temp.right != null){
searchNode(temp.right, value);
}
}
}
}
public static void Main()
{
SearchBinaryTree bt = new SearchBinaryTree();
//Add nodes to the binary tree
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.right.left = new Node(5);
bt.root.right.right = new Node(6);
//Search for node 5 in the binary tree
bt.searchNode(bt.root, 5);
if(SearchBinaryTree.flag)
Console.WriteLine("Element is present in the binary tree");
else
Console.WriteLine("Element is not present in the binary tree");
}
}
}
输出:
Element is present in the binary tree
data = $data;
$this->left = NULL;
$this->right = NULL;
}
}
class SearchBinaryTree{
//Represent the root of binary tree
public $root;
public $flag;
function __construct(){
$this->root = NULL;
$this->flag = false;
}
//searchNode() will search for the particular node in the binary tree
function searchNode($temp, $value){
//Check whether tree is empty
if($this->root == NULL){
echo "Tree is empty
";
}
else{
//If value is found in the given binary tree then, set the flag to true
if($temp->data == $value){
$this->flag = true;
return;
}
//Search in left subtree
if($this->flag == false && $temp->left != NULL){
$this->searchNode($temp->left, $value);
}
//Search in right subtree
if($this->flag == false && $temp->right != NULL){
$this->searchNode($temp->right, $value);
}
}
}
}
$bt = new SearchBinaryTree();
//Add nodes to the binary tree
$bt->root = new Node(1);
$bt->root->left = new Node(2);
$bt->root->right = new Node(3);
$bt->root->left->left = new Node(4);
$bt->root->right->left = new Node(5);
$bt->root->right->right = new Node(6);
//Search for node 5 in the binary tree
$bt->searchNode($bt->root, 5);
if($bt->flag)
echo "Element is present in the binary tree";
else
echo "Element is not present in the binary tree";
?>
输出:
Element is present in the binary tree