Jump to content

Recommended Posts

Posted (edited)

 

Hello, I needed a tree structure for a project I'm working on so I pretty much copied the code from: https://www.geeksforgeeks.org/dsa/introduction-to-tree-data-structure/

This is very simple but seemingly actually works! Enjoy and please let me know if you find any bugs or make improvements!

Tree Data Structure.zip

#Region ; Tree Data Structure

; Basically lifted from the C# code on this webpage: https://www.geeksforgeeks.org/dsa/introduction-to-tree-data-structure/

#include-once

#include <Array.au3>

Func Node(Const $data)
    Local $node = _AutoItObject_Create()

    _AutoItObject_AddProperty($node, "Data", $ELSCOPE_PUBLIC, $data)
    
    Local $children[0]

    _AutoItObject_AddProperty($node, "Children", $ELSCOPE_PUBLIC, $children)

    Return $node 
EndFunc

Func AddChild(ByRef $parent, Const ByRef $child)
    Local $children = $parent.Children

    _ArrayAdd($children, $child)

    $parent.Children = $children 
EndFunc

Func PrintParents(ByRef $node, ByRef $parent)
    If $parent = Null Then 
        ConsoleWrite($node.Data & " -> Null" & @CRLF)
    Else
        ConsoleWrite($node.Data & " -> " & $parent.Data & @CRLF)
    EndIf

    Local Const $children = $node.Children

    For $child in $children
        PrintParents($child, $node)
    Next
EndFunc

Func PrintChildren(ByRef $node)
    If IsObj($node) Then
        ConsoleWrite($node.Data & " --> ")

        Local Const $children = $node.Children

        For $child in $children
            ConsoleWrite($child.Data & ' ')
        Next

        ConsoleWrite(@CRLF)

        For $child in $children
            PrintChildren($child)
        Next
    EndIf
EndFunc

Func PrintLeaves(ByRef $node)
    If IsObj($node) Then
        If UBound($node.Children) = 0 Then 
            ConsoleWrite($node.Data & @CRLF)

            Return
        Else
            Local Const $children = $node.Children

            For $child in $children
                PrintLeaves($child)
            Next
        EndIf
    EndIf
EndFunc

#EndRegion ; Tree Data Structure

 

#Region ; Example

#include-once 

#include "AutoItObject.au3"
#include "Tree.au3"

_AutoItObject_StartUp() 

example() 

Func example()
    Local $root = Node(0)

    Local $child1 = Node(1)

    Local $child2 = Node(2)

    Local $child3 = Node(3)

    Local $child4 = Node(4)

    Local $child5 = Node(5)

    Local $child6 = Node(6)

    AddChild($root, $child1)

    AddChild($root, $child2)

    AddChild($child1, $child3)

    AddChild($child2, $child4)

    AddChild($child2, $child5)

    AddChild($child2, $child6)

    ConsoleWrite("Leaves:" & @CRLF)

    PrintLeaves($root)

    ConsoleWrite(@CRLF & "Children:" & @CRLF)

    PrintChildren($root)

    ConsoleWrite(@CRLF & "Parents:" & @CRLF)

    PrintParents($child1, $root)

    ConsoleWrite(@CRLF)

    PrintParents($child2, $root)

    ConsoleWrite(@CRLF)

    PrintParents($child3, $root)

    ConsoleWrite(@CRLF)

    PrintParents($child4, $root)

    ConsoleWrite(@CRLF)

    PrintParents($child5, $root)

    ConsoleWrite(@CRLF)

    PrintParents($child6, $root)
EndFunc

#EndRegion ; Example

 

Edited by jaberwacky

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...