C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

二分探索木で要素を検索するC++プログラム


このプログラムでは、する必要があります。二分探索を実装して、二分探索ツリー内の検索シーケンスの存在を見つけます。二分探索の最悪の場合の時間計算量はO(n)ですが、平均的な場合はO(log(n))です。

アルゴリズム

Begin
   Construct binary search tree for the given unsorted data array by inserting data into tree one by one.
   Take the input of data to be searched in the BST.
   Now starting from the root node, compare the data with data part of the node.
   if data < temp->d, move the temp pointer to the left child.
   if data > temp->d move the temp pointer to the right child.
   if data = temp->d print the tree depth where it is found and return to main.
   Else print item not found.
End

サンプルコード

#include<iostream>
using namespace std;
struct node {
   int d;
   node *left;
   node *right;
};
node* CreateNode(int d) {
   node *newnode = new node;
   newnode->d = d;
   newnode->left = NULL;
   newnode->right = NULL;
   return newnode;
}
node* InsertIntoTree(node* root, int d) {
   node *temp = CreateNode(d);
   node *t = new node;
   t = root;
   if(root == NULL)
      root = temp;
   else {
      while(t != NULL) {
         if(t->d < d) {
            if(t->right == NULL) {
               t->right = temp;
               break;
            }
            t = t->right;
         } else if(t->d > d) {
            if(t->left == NULL) {
               t->left = temp;
               break;
            }
            t = t->left;
         }
      }
   }
   return root;
}
void Search(node *root, int d) {
   int depth = 0;
   node *temp = new node;
   temp = root;
   while(temp != NULL) {
      depth++;
      if(temp->d == d) {
         cout<<"\nitem found at depth: "<<depth;
         return;
      } else if(temp->d > d)
         temp = temp->left;
         else
            temp = temp->right;
   }
   cout<<"\n item not found";
   return;
}
int main() {
   char ch;
   int n, i, a[10] = {93, 53, 45, 2, 7, 67, 32, 26, 71, 76};
   node *root = new node;
   root = NULL;
   for (i = 0; i < 10; i++)
      root = InsertIntoTree(root, a[i]);
   up:
   cout<<"\nEnter the Element to be searched: ";
   cin>>n;
   Search(root, n);
   cout<<"\n\n\tDo you want to search more...enter choice(y/n)?";
   cin>>ch;
   if(c == 'y' || c == 'Y')
      goto up;
   return 0;
}

出力

Enter the Element to be searched: 26
item found at depth: 7
Do you want to search more...enter choice(y/n)?
Enter the Element to be searched: 1
item not found
Do you want to search more...enter choice(y/n)?

  1. C++での2進数から10進数への変換プログラム

    2進数を入力として指定すると、タスクは指定された2進数を10進数に変換することです。 コンピューターの10進数は10進数で表され、2進数は2進数の0と1の2つしかないため、2進数で表されますが、10進数は0〜9から始まる任意の数値にすることができます。 2進数を10進数に変換するには、右から左に向かって残りの数字を抽出し、0から始まる2の累乗を掛けて、(桁数)–1まで1ずつ増やします。乗算された値を加算し続けて、最終的な10進数値を取得します。 以下に、2進数を10進数に変換する図を示します。 例 Input-: 1010    0 will be conver

  2. 二分探索木で左回転を実行するC++プログラム

    二分探索木は、すべてのノードが次の2つのプロパティを持つソートされた二分木です- ノードの右側のサブツリーには、親ノードのキーよりも大きいすべてのキーがあります。 ノードの左側のサブツリーには、親ノードのキーよりも少ないすべてのキーがあります。各ノードには2つ以上の子を含めることはできません。 木の回転は、二分木の要素の順序を妨げることなく構造を変更する操作です。ツリー内で1つのノードを上に移動し、1つのノードを下に移動します。これは、ツリーの形状を変更したり、小さいサブツリーを下に移動したり、大きいサブツリーを上に移動したりして高さを低くしたりするために使用され、多くのツリー操作の