tree data structure
class TreeNode:
def __init__(self, data):
self.data = data
self.children = []
self.parent = None
def add_child(self, child):
child.parent = self
self.children.append(child)
def getlevel(self):
level = 0
p = self.parent
while p:
level += 1
p = p.parent
return level
def printt(self):
prefix = (" " * 4 * self.getlevel()) + ("|--" if self.parent else "")
print(prefix + self.data)
if self.children:
for child in self.children:
child.printt()
def build_tree():
root = TreeNode("Food")
italy = TreeNode("Italy")
italy.add_child(TreeNode("Pizza"))
italy.add_child(TreeNode("Lasgna"))
italy.add_child(TreeNode("Pistacho Ice"))
chinese = TreeNode("Chineese")
chinese.add_child(TreeNode("Noodles"))
chinese.add_child(TreeNode("Rice balls"))
chinese.add_child(TreeNode("Fried Rice"))
mexican = TreeNode("Mexican")
mexican.add_child(TreeNode('Tacos'))
mexican.add_child(TreeNode('Gyro'))
mexican.add_child(TreeNode('Shawarma'))
root.add_child(italy)
root.add_child(chinese)
root.add_child(mexican)
return root
# mexican.printt()
if __name__ == "__main__":
root = build_tree()
root.printt()
3.78
9
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
Thank you!
9
0
0
0
void insert(int data) {
struct node *tempNode = (struct node*) malloc(sizeof(struct node));
struct node *current;
struct node *parent;
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//if tree is empty, create root node
if(root == NULL) {
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
//go to left of the tree
if(data < parent->data) {
current = current->leftChild;
//insert to the left
if(current == NULL) {
parent->leftChild = tempNode;
return;
}
}
//go to right of the tree
else {
current = current->rightChild;
//insert to the right
if(current == NULL) {
parent->rightChild = tempNode;
return;
}
}
}
}
}
Thank you!
0
0
3.5
4
If root is NULL
then create root node
return
If root exists then
compare the data with node.data
while until insertion position is located
If data is greater than node.data
goto right subtree
else
goto left subtree
endwhile
insert data
end If
Thank you!
4
0
4
1
// Tree traversal in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
// Create a new Node
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("Inorder traversal \n");
inorderTraversal(root);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
}
Thank you!
1
0
Are there any code examples left?
New code examples in category Python