| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | package tree
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import "fmt"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Print prints a Tree. Main use is to aid in debugging.
 | 
					
						
							|  |  |  | func (t *Tree) Print() {
 | 
					
						
							|  |  |  | 	if t.Root == nil {
 | 
					
						
							|  |  |  | 		fmt.Println("<nil>")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	t.Root.print()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (n *Node) print() {
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | 	q := newQueue()
 | 
					
						
							|  |  |  | 	q.push(n)
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	nodesInCurrentLevel := 1
 | 
					
						
							|  |  |  | 	nodesInNextLevel := 0
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | 	for !q.empty() {
 | 
					
						
							|  |  |  | 		do := q.pop()
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		nodesInCurrentLevel--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if do != nil {
 | 
					
						
							|  |  |  | 			fmt.Print(do.Elem.Name(), " ")
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | 			q.push(do.Left)
 | 
					
						
							|  |  |  | 			q.push(do.Right)
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 			nodesInNextLevel += 2
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		if nodesInCurrentLevel == 0 {
 | 
					
						
							|  |  |  | 			fmt.Println()
 | 
					
						
							| 
									
										
										
										
											2021-11-20 00:18:31 +08:00
										 |  |  | 			nodesInCurrentLevel = nodesInNextLevel
 | 
					
						
							|  |  |  | 			nodesInNextLevel = 0
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	fmt.Println()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type queue []*Node
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | // newQueue returns a new queue.
 | 
					
						
							|  |  |  | func newQueue() queue {
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	q := queue([]*Node{})
 | 
					
						
							|  |  |  | 	return q
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | // push pushes n to the end of the queue.
 | 
					
						
							|  |  |  | func (q *queue) push(n *Node) {
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	*q = append(*q, n)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | // pop pops the first element off the queue.
 | 
					
						
							|  |  |  | func (q *queue) pop() *Node {
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	n := (*q)[0]
 | 
					
						
							|  |  |  | 	*q = (*q)[1:]
 | 
					
						
							|  |  |  | 	return n
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-13 14:03:12 +00:00
										 |  |  | // empty returns true when the queue contains zero nodes.
 | 
					
						
							| 
									
										
										
										
											2016-11-07 07:43:38 +00:00
										 |  |  | func (q *queue) empty() bool {
 | 
					
						
							| 
									
										
										
										
											2016-11-05 14:39:49 +00:00
										 |  |  | 	return len(*q) == 0
 | 
					
						
							|  |  |  | }
 |