github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/tree/tree.go (about)

     1  package tree
     2  
     3  import "fmt"
     4  
     5  type Node struct {
     6  	Item        int
     7  	Height      int
     8  	Left, Right *Node
     9  }
    10  
    11  type BinaryTree struct {
    12  	Root *Node
    13  }
    14  
    15  func CreateBinaryTree() *BinaryTree {
    16  	return &BinaryTree{Root: nil}
    17  }
    18  
    19  /*
    20  Recursive implementation
    21  */
    22  
    23  func PostOrder(n *Node) {
    24  	if n == nil {
    25  		return
    26  	}
    27  
    28  	PostOrder(n.Left)
    29  	PostOrder(n.Right)
    30  	fmt.Print(n.Item, " -> ")
    31  }
    32  
    33  func InOrder(n *Node) {
    34  	if n == nil {
    35  		return
    36  	}
    37  
    38  	InOrder(n.Left)
    39  	fmt.Print(n.Item, " -> ")
    40  	InOrder(n.Right)
    41  }
    42  
    43  func PreOrder(n *Node) {
    44  	if n == nil {
    45  		return
    46  	}
    47  
    48  	fmt.Print(n.Item, " -> ")
    49  	PreOrder(n.Left)
    50  	PreOrder(n.Right)
    51  }
    52  
    53  /*
    54  loop implementation by stack (non-recursive)
    55  */
    56  
    57  /*
    58  General loop implementation (non-recursive)
    59  */