Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/engrsakib/traversal_and_check_bst_or_not


https://github.com/engrsakib/traversal_and_check_bst_or_not

Last synced: about 5 hours ago
JSON representation

Awesome Lists containing this project

README

        

Project Name:

Traversal a tree and check is BST or not
not.

Background Study

A binary search tree is a method to organize data, together with operations on these data (i.e., it is a data structure). In particular, the operation that this organization wants to perform really fast is searching. The data are records that may contain many fields, but one field is specifically used for ordering the data records; this field is called the record Node. In order to simplify things, from now on we will assume that the data record contains only this Node, and the Nodes are integers; but we don't require that Nodes are unique (so, the same Node may show up multiple times). Then a binary search tree consists of nodes that are of the following form:

[Node, Left subtree, Right subtree]

Notice that a node is itself a record. It is a list with three elements: the first element is an integer (key), and the next two elements are themselves binary search trees, a Left subtree and a Right subtree!
So the definition of a binary search tree is a recursive one. Before we define binary search trees though, it is simpler to first define binary trees (without the “search” component)

We can draw a binary tree using “nodes” to hold the keys and lines (edges) that show what are the left and right subtrees of the “nodes”. Here is such a conceptual drawing of two binary trees.

Tree Traversal


Traversing a tree means visiting every node in the tree. You might, for instance, want to add all the values in the tree or find the largest one. For all these operations, you will need to visit each node of the tree.

Linear data structures like arrays, stacks, queues, and linked list have only one way to read the data. But a hierarchical data structure like a tree can be traversed in different ways.

```
1. Depth First Search or DFS
Inorder Traversal
Preorder Traversal
Postorder Traversal
2. Level Order Traversal or Breadth First Search or BFS
Boundary Traversal
Diagonal Traversal
```

Operation Details

Inorder traversal



1. First, visit all the nodes in the left subtree

2. Then the root node

3. Visit all the nodes in the right subtree

```
inorder(root->left)
display(root->data)
inorder(root->right)
```

Preorder traversal



1. Visit root node

2. Visit all the nodes in the left subtree

3. Visit all the nodes in the right subtree

```
display(root->data)
preorder(root->left)
preorder(root->right)
```

Postorder traversal



1. Visit all the nodes in the left subtree

2. Visit all the nodes in the right subtree

3. Visit the root node

```
display(root->data)
preorder(root->left)
preorder(root->right)
```

Source Code

Code File

```cpp
#include

using namespace std;
class Node
{
public:
int val;
Node *left;
Node *right;
Node(int val)
{
this->val = val;
this->left = NULL;
this->right = NULL;
}
};
Node *convert(vector v, int n, int l, int r)
{
if (l > r)
return NULL;
int mid = (l + r) / 2;
Node *root = new Node(v[mid]);
Node *left_Root = convert(v, n, l, mid - 1);
Node *right_Root = convert(v, n, mid + 1, r);
root->left = left_Root;
root->right = right_Root;
return root;
}
// in order
void in_Order(Node *root)
{
if (root == NULL)
return;
in_Order(root->left);
cout << root->val << " ";
in_Order(root->right);
}
// pre Oreder
void pre_order(Node *root)
{
if (root == NULL)
return;
cout << root->val << " ";
pre_order(root->left);
pre_order(root->right);
}
// Post Order
void post_Order(Node *root)
{
if (root == NULL)
return;
post_Order(root->left);
post_Order(root->right);
cout << root->val << " ";
}
int main()
{
// Md. Nazmus Sakib
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector v;
int data;

while (cin >> data)
{
v.emplace_back(data);
}
sort(v.begin(), v.end());
Node *root = convert(v, v.size(), 0, v.size() - 1);
cout << "print Pre_Order: " << endl;
pre_order(root);
cout << endl
<< "Print In_Order: " << endl;
in_Order(root);
cout << endl
<< "Print Post_Order: " << endl;
post_Order(root);
return 0;
}
```

Binary Search Tree(BST)

Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers.




  • It is called a binary tree because each tree node has a maximum of two children.


  • It is called a search tree because it can be used to search for the presence of a number in O(log(n)) time.

  • The properties that separate a binary search tree from a regular binary tree is


  • All nodes of left subtree are less than the root node

  • All nodes of right subtree are more than the root node

  • Both subtrees of each node are also BSTs i.e. they have the above two properties

    Search Operation


    The algorithm depends on the property of BST that if each left subtree has values below root and each right subtree has values above the root.

    If the value is below the root, we can say for sure that the value is not in the right subtree; we need to only search in the left subtree and if the value is above the root, we can say for sure that the value is not in the left subtree; we need to only search in the right subtree.

    code

    Traversal and Check BST or Not ?

    ```cpp
    #include

    using namespace std;
    class Node
    {
    public:
    int val;
    Node *left;
    Node *right;
    Node(int val)
    {
    this->val = val;
    this->left = NULL;
    this->right = NULL;
    }
    };
    vectorvv;
    Node *convert(vector v, int n, int l, int r)
    {
    if (l > r)
    return NULL;
    int mid = (l + r) / 2;
    Node *root = new Node(v[mid]);
    Node *left_Root = convert(v, n, l, mid - 1);
    Node *right_Root = convert(v, n, mid + 1, r);
    root->left = left_Root;
    root->right = right_Root;
    return root;
    }
    // in order
    void in_Order(Node *root)
    {
    if (root == NULL)
    return;
    in_Order(root->left);
    cout << root->val << " ";
    vv.emplace_back(root->val);
    in_Order(root->right);
    }
    // pre Oreder
    void pre_order(Node *root)
    {
    if (root == NULL)
    return;
    cout << root->val << " ";
    pre_order(root->left);
    pre_order(root->right);
    }
    // Post Order
    void post_Order(Node *root)
    {
    if (root == NULL)
    return;
    post_Order(root->left);
    post_Order(root->right);
    cout << root->val << " ";
    }
    // Check if the tree is a binary search tree (BST)
    // Check BST is or Not
    bool is_BST(Node *root)
    {
    for(int i = 0; i < vv.size(); i++)
    {
    for(int j = i + 1; j < vv.size()-1; j++)
    {
    if(vv[i] > vv[j]) return false;
    }
    }
    return true;
    }
    int main()
    {
    // Md. Nazmus Sakib
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    vector v;
    int data;

    while (cin >> data)
    {
    v.emplace_back(data);
    }
    //sort(v.begin(), v.end());
    Node *root = convert(v, v.size(), 0, v.size() - 1);
    cout << "print Pre_Order: " << endl;
    pre_order(root);
    cout << endl
    << "Print In_Order: " << endl;
    in_Order(root);
    cout << endl
    << "Print Post_Order: " << endl;
    post_Order(root);

    if (is_BST(root))
    cout << endl << endl
    << "This is a Binary Search Tree" << endl;
    else
    cout << endl << endl
    << "This is not a Binary Search Tree" << endl;
    return 0;
    }
    ```

    Binary Search Tree Complexities


    | Name | Link |
    | ---------------- | ----------------------------- |
    | Md. Nazmus Sakib | BSc In Engr. |
    | Email | [email protected] |
    | LinkedIn | [LinkedIn][linked_in_link] |
    | Facebook | [Facebook][facebook_link] |
    | Code Force | [Code Force][Code_force_link] |
    | CodeChef | [CodeChef][CodeChef_link] |
    | LeetCode | [Code Force][LeetCode_link] |

    [linked_in_link]: https://www.linkedin.com/in/engrsakib/
    [facebook_link]: https://www.facebook.com/engrsakib02/
    [code_force_link]: https://codeforces.com/profile/engrsakib
    [LeetCode_link]: https://leetcode.com/engrsakib/
    [CodeChef_link]: https://www.codechef.com/users/engrsakib