0% found this document useful (0 votes)
83 views4 pages

DSA Quiz 3 Solution 26122020 035014pm

The document provides code to print only the leaf nodes of a binary tree. The code defines Node and BT classes to represent nodes and the tree. The BT class contains functions to insert nodes, traverse the tree inorder, and print leaf nodes by recursively checking if each node's left and right children are null. The main function creates a binary tree of at least 15 nodes by inserting values from an array, prints the inorder traversal, and calls the LeafNodes function to output just the leaf nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

DSA Quiz 3 Solution 26122020 035014pm

The document provides code to print only the leaf nodes of a binary tree. The code defines Node and BT classes to represent nodes and the tree. The BT class contains functions to insert nodes, traverse the tree inorder, and print leaf nodes by recursively checking if each node's left and right children are null. The main function creates a binary tree of at least 15 nodes by inserting values from an array, prints the inorder traversal, and calls the LeafNodes function to output just the leaf nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DSA Quiz 3 Solution

Write a program to print only the leaf nodes of a tree. Your tree must have at least 15 nodes. You can
use the code from previous lecture to perform this task.

Code:
#include<iostream>
#include<conio.h>
using namespace std;
class Node
{
public:
int data;
Node* left;
Node* right;
};
class BT
{
private:
Node* root;
void insert(int val, Node* tree)
{
Node* n = newNode(val);
if (val <= tree->data)
{
if (tree->left == NULL)
tree->left = n;
else
insert(val, tree->left);
}
else
{
if (tree->right == NULL)
tree->right = n;
else
insert(val, tree->right);
}
}
void inorder(Node* tree)
{
if (tree != NULL)
{
inorder(tree->left);
cout << tree->data << " ";
inorder(tree->right);
}
}
public:
BT()
{
root = NULL;
}
Node* newNode(int val)
{
Node* n = new Node;
n->data = val;
n->left = NULL;
n->right = NULL;
return n;
}
void insert(int val)
{
if (root == NULL)
{
Node* n = newNode(val);
root = n;
}
else
insert(val, root);
}
void inorder()
{
inorder(root);
cout << endl;
}
void LeafNodes(Node* Root)
{
if (Root == NULL)
{
Root = root;
}
if (Root == NULL)
{
cout << "There is no Node in Tree...";
return;
}
if (Root->left == NULL && Root->right == NULL)
{
cout << Root->data << " ";
return;
}
if (Root->right)
LeafNodes(Root->right);
if (Root->left)
LeafNodes(Root->left);
}
};

int main()
{
BT bt;
Node* Root = NULL;
int arr[8];
for (int i = 0; i < 8; i++)
{
cout << "Enter the Values: ";
cin >> arr[i];
bt.insert(arr[i]);
}
cout << "\Inorder Form: ";
bt.inorder();
cout << "\nLeaf Nodes Are: ";
bt.LeafNodes(Root);
_getch();
return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy