Categories > WeAreDevs > Hangout >

Ask me anything ( programming related )

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Posted

Ask me anything about cpp, python and lua that you have questions on ( including memory-related questions ).

I'll try my best to answer your questions as detailed as possible.

 

  • 0

https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png

Ballistic

Ballistic

Posts: 30

Threads: 3

Joined: Aug, 2022

Reputation: 5

Replied

why does my script not work

if if then
  print("if")
end

  • 0

Ballistic Discord Invite: https://discord.com/invite/J85S63RzRj

Posts: 1430

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

how do you invert a binary tree?

  • 0

i use arch btw

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@Whoman

 

no need to cross your questions, all questions are allowed and encouraged no need to me embarrassed :heart:

 

In light of your question I decided to make a little basic C++ demonstration of a binary tree with a few functions. Inverting a binary tree recursively is a lot simpler than it sounds.

 

it's quite a small function which just swaps nodes until it finds the end of the binary tree.

I added a couple more functions just to demonstrate some more about binary trees.

hope this helps!

#include <iostream>
#include <memory>
#include <vector>

template< typename data_t >
struct node
{

	std::unique_ptr< node > left_child{ nullptr };
	std::unique_ptr< node > right_child{ nullptr };

	data_t data{ };	

	explicit node( const data_t node_data )
	{
		this->data = node_data;
	}

};


template< typename data_t >
void invert_tree( node< data_t >* tree_root )
{
	if( tree_root == nullptr )
		return;

	invert_tree( tree_root->left_child.get( ) );
	invert_tree( tree_root->right_child.get( ) );

	tree_root->right_child.swap( tree_root->left_child );

	return;

}

template< typename data_t >
void traverse_tree_primitive( node< data_t >* root_node, std::vector< node< data_t >* >& cached_nodes ) 
{

	if (root_node == nullptr) 
		return;

	traverse_tree_primitive( root_node->left_child.get( ), cached_nodes );

	cached_nodes.push_back( root_node );

	traverse_tree_primitive( root_node->right_child.get( ), cached_nodes );

}

template< typename data_t >
void output_tree( node< data_t >* tree_root ) 
{ 
	if (tree_root == nullptr) 
		return; 

	output_tree( tree_root->left_child.get( ) );

	std::cout << tree_root->data << " ";

	output_tree( tree_root->right_child.get( ) ); 
}

int main( )
{
	node< std::uint32_t > root{ 10 };
	std::vector< node< std::uint32_t >* > cached_nodes;

	root.left_child = std::unique_ptr< node< std::uint32_t > >( new node< std::uint32_t >( 10 ) );	
	root.right_child = std::unique_ptr< node< std::uint32_t > >( new node< std::uint32_t >( 20 ) );
		
	root.right_child->left_child = std::unique_ptr< node< std::uint32_t > >( new node< std::uint32_t >( 30 ) );
	root.right_child->right_child = std::unique_ptr< node< std::uint32_t > >( new node< std::uint32_t >( 40 ) );


	output_tree( &root );
	invert_tree( &root ), std::cout << "\n";
	output_tree( &root ), std::cout << "\n";

	traverse_tree_primitive( &root, cached_nodes );

	for ( const auto& node : cached_nodes )
	{
		std::printf( "%u ", node->data );
	}

	return 0;
}

https://media.discordapp.net/attachments/988849296336126053/1006238350312742932/unknown.png

 

  • 0

https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png

Posts: 2014

Threads: 198

Joined: Apr, 2021

Reputation: 16

Replied

@luxiferrwoo me when im trying to understand the code:

 

yes

  • 0

Random quote here...

Posts: 1430

Threads: 71

Joined: May, 2022

Reputation: 20

Replied

@luxiferrwoo

nice now i can finish my interview heheahw

 

  • 0

Added

@_realnickk

OMG YOU SAVED MY A** I WAS GONNA SAY WHAT THE OTHER GUY SAID THAT SAID HE SAID

  • 0

Added

  • 0

i use arch btw

Posts: 283

Threads: 48

Joined: May, 2022

Reputation: -4

Replied

@VoidableMethod

 

it's not hard code to understand as long as you actually try to read it lol

anything you don't know about? search it up, learn about it. feel free to ask me questions, here's my discord:

! exprssn.sentiment#9999

 

  • 0

https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png

Moon

Moon

vip

Posts: 7441

Threads: 314

Joined: Aug, 2020

Reputation: 80

Replied

  • 0

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )