“Drzewo binarne” Kod odpowiedzi

Drzewo binarne

#include <iostream>
#include <queue>
using namespace std;
struct Node{
   int data;
   struct Node* left, *right;
};
// Function to count the full Nodes in a binary tree
int fullcount(struct Node* node){
   // Check if tree is empty
   if (!node){
      return 0;
   }  
   queue<Node *> myqueue;
   // traverse using level order traversing
   int result = 0;
   myqueue.push(node);
   while (!myqueue.empty()){
      struct Node *temp = myqueue.front();
      myqueue.pop();
      if (temp->left && temp->right){
         result++;
      }
      if (temp->left != NULL){
         myqueue.push(temp->left);
      }
      if (temp->right != NULL){
         myqueue.push(temp->right);
      }
   }
   return result;
}
struct Node* newNode(int data){
   struct Node* node = new Node;
   node->data = data;
   node->left = node->right = NULL;
   return (node);
}
int main(void){
   struct Node *root = newNode(10);
   root->left = newNode(20);
   root->right = newNode(30);
   root->left->left = newNode(40);
   root->left->right = newNode(50);
   root->left->left->right = newNode(60);
   root->left->right->right = newNode(70);
   cout <<"count is: "<<fullcount(root);
   return 0;
}
Mohith

Drzewo binarne

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataStructure
{
    namespace Tree
    {
        internal class BinaryTree
        {
            private Node root;
            public void Insert(int data)
            {
                var node = new Node(data);
                if (root == null)
                {
                    root = node;
                    return;
                }

                var current = root;

                while (true)
                {
                    if (current.data > data)
                    {
                        if (current.leftChild == null)
                        {
                            current.leftChild = node;
                            break;
                        }
                        current = current.leftChild;
                    }
                    else if (current.data < data)
                    {
                        if (current.rightChild == null)
                        {
                            current.rightChild = node;
                            break;
                        }
                        current = current.rightChild;
                    }
                }
            }
            public void InOrder()
            {
                InOrder(root);
            }
            public void PreOrder()
            {
                InOrder(root);
            }
            public void PostOrder()
            {
                InOrder(root);
            }
            private void InOrder(Node node)
            {
                if (node == null)
                    return;
                InOrder(node.leftChild);
                Console.Write(node.data);
                InOrder(node.rightChild);
            }
            private void PreOrder(Node node)
            {
                if (node == null)
                    return;
                Console.Write(node.data);
                PreOrder(node.leftChild);
                PreOrder(node.rightChild);
            }
            private void PostOrder(Node node)
            {
                if (node == null)
                    return;
                PostOrder(node.leftChild);
                PostOrder(node.rightChild);
                Console.Write(node.data);
            }

            public int Height()
            {
                return Height(root);
            }
            private int Height(Node node)
            {
                if (node == null)
                {
                    return -1;
                }
                if (IsLeaf(node))
                {
                    return 0;
                }
                return 1 + Math.Max(Height(node.leftChild), Height(node.rightChild));
            }
            private bool IsLeaf(Node node)
            {
                if (node.leftChild == null && node.rightChild == null)
                {
                    return true;
                }
                return false;
            }

            public bool IsEqual(Node first, Node second)
            {
                if (first == null && second == null)
                    return true;
                if (first != null && second != null)
                {
                    return first.data == second.data &&
                        IsEqual(first.rightChild, second.rightChild) &&
                        IsEqual(first.leftChild, second.leftChild);
                }
                return false;
            }
        }
        public class Node
        {
            public int data;
            public Node leftChild;
            public Node rightChild;
            public Node(int data)
            {
                this.data = data;
            }
        }
    }
}
PrashantUnity

Drzewo binarne

void preorder(node *n) {
    cout << n->value;
    if(n->left!=NULL)
        preorder(n->left);
    if (n->right != NULL)
        preorder(n->right);
}
void inorder(node *n) {
    if (n->left != NULL)
        inorder(n->left);
    cout << n->value;
    if (n->right != NULL)
        inorder(n->right);
}
void postorder(node *n) {
    if (n->left != NULL)
        postorder(n->left);
    if (n->right != NULL)
        postorder(n->right);
    cout << n->value;
}
Anxious Armadillo

Drzewo binarne

Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
shyam makwana

Drzewo binarne

void preorder(node *n) {
    cout << n->value;
    if(n->left!=NULL)
        preorder(n->left);
    if (n->right != NULL)
        preorder(n->right);
}
void inorder(node *n) {
    if (n->left != NULL)
        inorder(n->left);
    cout << n->value;
    if (n->right != NULL)
        inorder(n->right);
}
void postorder(node *n) {
    if (n->left != NULL)
        postorder(n->left);
    if (n->right != NULL)
        postorder(n->right);
    cout << n->value;
}
Anxious Armadillo

Odpowiedzi podobne do “Drzewo binarne”

Pytania podobne do “Drzewo binarne”

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu