Binary Tree Geeksforgeeks Code Example


Example 1: binary tree search

/* This is just the seaching function you need to write the required code. 	Thank you. */  void searchNode(Node *root, int data) {     if(root == NULL)     {         cout << "Tree is empty\n";         return;     }      queue q;     q.push(root);      while(!q.empty())     {         Node *temp = q.front();         q.pop();          if(temp->data == data)         {             cout << "Node found\n";             return;         }          if(temp->left != NULL)             q.push(temp->left);         if(temp->right != NULL)             q.push(temp->right);     }      cout << "Node not found\n"; }

Example 2: binary search tree

# Driver Code  arr = [ 2, 3, 4, 10, 40 ]  x = 10    # Function call  result = binarySearch(arr, 0, len(arr)-1, x)     if result != -1:      print ("Element is present at index % d" % result)  else:      print ("Element is not present in array")

Comments

Popular posts from this blog

Chemistry - Bond Angles In NH3 And NCl3

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Change The Font Size Of Visual Studio Solution Explorer