tree in c++ code example

Example 1: tree in c++ stl

// C++ STL library does not provide any non linear data structure like
// Trees and Graphs
// it only provides linear data structures like 
// vector , list , map , stack , queue etc..
// https://www.geeksforgeeks.org/binary-tree-data-structure/
// https://www.geeksforgeeks.org/graph-implementation-using-stl-for-competitive-programming-set-1-dfs-of-unweighted-and-undirected/

Example 2: binary tree in c implementation

11 void insert(node ** tree, int val) {
12 node *temp = NULL;
13 if(!(*tree)) {
14   temp = (node *)malloc(sizeof(node));
15   temp->left = temp->right = NULL;
16   temp->data = val;
17   *tree = temp;
18   return;
19 }
20
21 if(val < (*tree)->data) {
22      insert(&(*tree)->left, val);
23   } else if(val > (*tree)->data) {
24     insert(&(*tree)->right, val);
25   }
26 }

Tags:

Misc Example