1class TreeNode:
2 def __init__(self, data):
3 self.data = data
4 self.children = []
5 self.parent = None
6
7 def add_child(self, child):
8 child.parent = self
9 self.children.append(child)
10
11 def getlevel(self):
12 level = 0
13 p = self.parent
14 while p:
15 level += 1
16 p = p.parent
17 return level
18
19 def printt(self):
20 prefix = (" " * 4 * self.getlevel()) + ("|--" if self.parent else "")
21 print(prefix + self.data)
22 if self.children:
23 for child in self.children:
24 child.printt()
25
26
27def build_tree():
28 root = TreeNode("Food")
29
30 italy = TreeNode("Italy")
31 italy.add_child(TreeNode("Pizza"))
32 italy.add_child(TreeNode("Lasgna"))
33 italy.add_child(TreeNode("Pistacho Ice"))
34
35 chinese = TreeNode("Chineese")
36 chinese.add_child(TreeNode("Noodles"))
37 chinese.add_child(TreeNode("Rice balls"))
38 chinese.add_child(TreeNode("Fried Rice"))
39
40 mexican = TreeNode("Mexican")
41 mexican.add_child(TreeNode('Tacos'))
42 mexican.add_child(TreeNode('Gyro'))
43 mexican.add_child(TreeNode('Shawarma'))
44
45 root.add_child(italy)
46 root.add_child(chinese)
47 root.add_child(mexican)
48
49 return root
50
51 # mexican.printt()
52
53
54if __name__ == "__main__":
55 root = build_tree()
56 root.printt()
57
1struct node {
2 int data;
3 struct node *leftChild;
4 struct node *rightChild;
5};
1void insert(int data) {
2 struct node *tempNode = (struct node*) malloc(sizeof(struct node));
3 struct node *current;
4 struct node *parent;
5
6 tempNode->data = data;
7 tempNode->leftChild = NULL;
8 tempNode->rightChild = NULL;
9
10 //if tree is empty, create root node
11 if(root == NULL) {
12 root = tempNode;
13 } else {
14 current = root;
15 parent = NULL;
16
17 while(1) {
18 parent = current;
19
20 //go to left of the tree
21 if(data < parent->data) {
22 current = current->leftChild;
23
24 //insert to the left
25 if(current == NULL) {
26 parent->leftChild = tempNode;
27 return;
28 }
29 }
30
31 //go to right of the tree
32 else {
33 current = current->rightChild;
34
35 //insert to the right
36 if(current == NULL) {
37 parent->rightChild = tempNode;
38 return;
39 }
40 }
41 }
42 }
43}
1If root is NULL
2 then create root node
3return
4
5If root exists then
6 compare the data with node.data
7
8 while until insertion position is located
9
10 If data is greater than node.data
11 goto right subtree
12 else
13 goto left subtree
14
15 endwhile
16
17 insert data
18
19end If
1// Tree traversal in C
2
3#include <stdio.h>
4#include <stdlib.h>
5
6struct node {
7 int item;
8 struct node* left;
9 struct node* right;
10};
11
12// Inorder traversal
13void inorderTraversal(struct node* root) {
14 if (root == NULL) return;
15 inorderTraversal(root->left);
16 printf("%d ->", root->item);
17 inorderTraversal(root->right);
18}
19
20// preorderTraversal traversal
21void preorderTraversal(struct node* root) {
22 if (root == NULL) return;
23 printf("%d ->", root->item);
24 preorderTraversal(root->left);
25 preorderTraversal(root->right);
26}
27
28// postorderTraversal traversal
29void postorderTraversal(struct node* root) {
30 if (root == NULL) return;
31 postorderTraversal(root->left);
32 postorderTraversal(root->right);
33 printf("%d ->", root->item);
34}
35
36// Create a new Node
37struct node* createNode(value) {
38 struct node* newNode = malloc(sizeof(struct node));
39 newNode->item = value;
40 newNode->left = NULL;
41 newNode->right = NULL;
42
43 return newNode;
44}
45
46// Insert on the left of the node
47struct node* insertLeft(struct node* root, int value) {
48 root->left = createNode(value);
49 return root->left;
50}
51
52// Insert on the right of the node
53struct node* insertRight(struct node* root, int value) {
54 root->right = createNode(value);
55 return root->right;
56}
57
58int main() {
59 struct node* root = createNode(1);
60 insertLeft(root, 12);
61 insertRight(root, 9);
62
63 insertLeft(root->left, 5);
64 insertRight(root->left, 6);
65
66 printf("Inorder traversal \n");
67 inorderTraversal(root);
68
69 printf("\nPreorder traversal \n");
70 preorderTraversal(root);
71
72 printf("\nPostorder traversal \n");
73 postorderTraversal(root);
74}