Categories > WeAreDevs > Hangout >
Ask me anything ( programming related )
Posted
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Replied
why does my script not work
if if then
print("if")
end
Cancel
Post
Ballistic Discord Invite: https://discord.com/invite/J85S63RzRj
i use arch btw
Replied
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
Cancel
Post
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Random quote here...
Added
Cancel
Post
Added
Cancel
Post
i use arch btw
Replied
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
Cancel
Post
https://media.discordapp.net/attachments/1044764388546068510/1051935933836050482/Signature_4.png
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post