Tree: Level Order Traversal

Sort by

recency

|

576 Discussions

|

  • + 0 comments

    class Node: def init(self, info): self.info = info
    self.left = None
    self.right = None self.level = None

    def __str__(self):
        return str(self.info) 
    

    class BinarySearchTree: def init(self): self.root = None

    def create(self, val):  
        if self.root == None:
            self.root = Node(val)
        else:
            current = self.root
    
            while True:
                if val < current.info:
                    if current.left:
                        current = current.left
                    else:
                        current.left = Node(val)
                        break
                elif val > current.info:
                    if current.right:
                        current = current.right
                    else:
                        current.right = Node(val)
                        break
                else:
                    break
    

    """ Node is defined as self.left (the left child of the node) self.right (the right child of the node) self.info (the value of the node) """ def levelOrder(root): if not root: return queue = [root] while queue: cur = queue.pop(0) print(cur.info, end=" ") if cur.left: queue.append(cur.left) if cur.right: queue.append(cur.right)

    tree = BinarySearchTree() t = int(input())

    arr = list(map(int, input().split()))

    for i in range(t): tree.create(arr[i])

    levelOrder(tree.root)

  • + 0 comments
    int height(struct node* root){
        if(root == NULL){
            return 0;
       }else{
             int leftHeight = height(root->left);
             int rightHeight = height(root->right);
            if(leftHeight > rightHeight){
                return (leftHeight + 1);
            }else{
                return (rightHeight + 1);
            }
        }
    }
    
    void printCurrentLevelOrder(struct node* root,int level){
        if(root == NULL){
            return;
        }
        if(level == 1){
            printf("%d ",root->data);
        }else if(level > 1){
            printCurrentLevelOrder(root->left,level - 1);
            printCurrentLevelOrder(root->right,level - 1);
        }
    }
    
    void levelOrder( struct node *root) {
        int h = height(root);
        for(int i = 1;i <= h;i++){
            printCurrentLevelOrder(root,i);
        }
    }
    
  • + 0 comments

    Better clarity in the comments public static void levelOrder(Node root) { // Use ArrayDeque as the queue for level order traversal Queue nodeQueue = new ArrayDeque<>();

        // Start by offering the root node to the queue
        nodeQueue.offer(root);
    
        // Process nodes until the queue is empty
        while (nodeQueue.size() > 0) {
            // Remove the front element from the queue
            Node node = nodeQueue.poll();
    
            // Print the current node's data
            System.out.print(node.data + " ");
    
            // If the left child is not null, add it to the queue
            if (node.left != null) {
                nodeQueue.offer(node.left);
            }
    
            // If the right child is not null, add it to the queue
            if (node.right != null) {
                nodeQueue.offer(node.right);
            }
        }
    }
    
  • + 0 comments
    void levelOrder(Node * root) {
    
        queue<Node*>q;
        q.push(root);
        while(!q.empty()){
            int size = q.size(); 
            for(int i=0;i<size;i++){
    
              Node* popnode=q.front();
              q.pop();
              cout<<popnode->data<<" ";
              if(popnode->left!=NULL){
                  q.push(popnode->left);
              }
              if(popnode->right!=NULL){
                 q.push(popnode->right);
              }
            }
    
        }
    
    
    }
    
  • + 0 comments

    My C code 😎😁

    int height(struct node* root){
        if(root == NULL){
            return 0;
        }else{
            const int leftHeight = height(root->left);
            const int rightHeight = height(root->right);
            if(leftHeight > rightHeight){
                return (leftHeight + 1);
            }else{
                return (rightHeight + 1);
            }
        }
    }
    
    void printCurrentLevelOrder(struct node* root,int level){
        if(root == NULL){
            return;
        }
        if(level == 1){
            printf("%d ",root->data);
        }else if(level > 1){
            printCurrentLevelOrder(root->left,level - 1);
            printCurrentLevelOrder(root->right,level - 1);
        }
    }
    
    void levelOrder( struct node *root) {
        int h = height(root);
        for(int i = 1;i <= h;i++){
            printCurrentLevelOrder(root,i);
        }
    }