Answers for "tree data structure in python"

3

tree data structure in python

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()
Posted by: Guest on July-25-2021
0

tree python

import random
class Node:
    def __init__(self, v, dx=None, sx=None):
        self.dx = dx
        self.sx = sx
        self.v = v

    def nodeCount(self):
        if self is None:
            return 0
        x, y = 0, 0
        if self.sx:
            x = self.sx.nodeCount()
        if self.dx:
            y = self.dx.nodeCount()
        return x + y + 1

    def minValue(self):
        if self is None:
            return float("+inf")
        x = y = float("+inf")
        if self.sx:
            x = self.sx.minValue()
        if self.dx:
            y = self.dx.minValue()
        return min(self.v, x, y) 

    def maxValue(self):
        if self is None:
            return float("-inf")
        x = y = float("-inf")
        if self.sx:
            x = self.sx.maxValue()
        if self.dx:
            y = self.dx.maxValue()
        return max(self.v, x, y)

    def printNode(self):
        if self is None:
            return
        print(self.v)
        if self.sx:
            self.sx.printNode()
        if self.dx:
            self.dx.printNode()
    
class binaryTree:
    def __init__(self, Node=None):
        self.Node = Node

    def buildTree(self, numberOfNode, valueLimit):
        if numberOfNode == 0: 
            return None

        node = Node(random.randint(1, valueLimit))
        numberOfNode -= 1
        if numberOfNode >= 0:
            x = random.randint(0, numberOfNode)
            node.sx = self.buildTree(x, valueLimit)
            node.dx = self.buildTree(numberOfNode-x, valueLimit)

        return node
Posted by: Guest on September-04-2021
-1

tree ds

// 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);
}
Posted by: Guest on July-13-2020

Code answers related to "tree data structure in python"

Python Answers by Framework

Browse Popular Code Answers by Language