Answers for "tree data structure"

3

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

tree data structure

struct node {
   int data;   
   struct node *leftChild;
   struct node *rightChild;
};
Posted by: Guest on November-26-2020
1

binary tree in data structure

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;
            }
         }
      }            
   }
}
Posted by: Guest on September-07-2020
0

tree data structure

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
Posted by: Guest on October-05-2020
-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"

Python Answers by Framework

Browse Popular Code Answers by Language