Binary tree. How to calculate the amount?

  • 0
    Hi, student, I ask for help in the implementation of the task, I spent a lot of time to get a working tree, but I can’t think of how to add it (( Task. Implement a binary tree, each edge of which corresponds to an integer (weight). It is necessary to calculate the total weight edges of the path from the root to the given leaf of the tree .
    I ask for your help, I'm new to programming. I would be very grateful if you could add a function to an existing program, otherwise it’s a little difficult for me to understand someone else’s code. the level is still too low, but I'm learning) Thanks in advance to all the helpers)))
    #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    struct Node
    {
    	int cell;
    	Node *left, *right;
    };
    
    void add_node(int node, Node *&Tree){
    	if (Tree == NULL){
    		Tree = new Node;
    		Tree->cell = node;
    		Tree->left = NULL;
    		Tree->right = NULL;
    	}
    	if (node < Tree->cell){
    		add_node(node, Tree->left);
    	}
    	if (node > Tree->cell){
    		add_node(node, Tree->right);
    	}
    }
    
    void printTree(Node *Tree)
    {
    	if (!Tree)
    		return;
    	printf("%d", Tree->cell);
    	//printTree(Tree->left);
    	printTree(Tree->right);
    }
    
    
    
    int main()
    {
    	Node *Tree = NULL;
    	printf("enter count elements: ");
    	int count;//кол-во эл
    	scanf_s("%i", &count);
    	int InputNum;//числа
    	for (int i = 0; i < count; i++)
    		cin >> InputNum,
    		add_node(InputNum, Tree);
    
    	printTree(Tree);
    
    
    	system("pause");
    	return 0;
    }
    C++ Hanna Potter, Aug 12, 2019

  • 1 Answers
  • 0
    struct Node
    {
    int cell;
    Node *left, *right;
    };


    Where is the weight of the transition to this Node? Summarize and that's it.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!