Data structure from the university after learning, to tell the truth forgotten almost, in the recent review data structure, just see two fork tree traversal here, want to leave a way to traverse the binary tree, in case the landlord forget, blog post language for Swift.
The tree TreeNode node structure is created as follows:
Tree node
class TreeNode
{
var val:int//value
var left:treenode? Left dial hand tree
var right:treenode? Right subtree
init (_ Val:int)
{
self.val = val
self.left = nil
self.right = nil
}
}
The usual way to traverse a binary tree is to use recursion to iterate over it, but in order to consolidate the stacks and queues that you see before, you can also record the way you use stacks to iterate through the stack: using stacks to traverse the tree Stack-the pre-order
Using the stack to implement the pre-sequence traversal
func preordertraversal (root:treenode), [int]
{
var res = [int] ()
var stack = [Treenod E] ()
var node = root
//If the stack is not empty or the node is not nil while
!stack.isempty | | node! = NIL
{
//If the node is not nil if
nod E! = Nil {
res.append (node!. val) //data stack Add Data
stack.append (node!) Add node node = node! in the node stack
. Left//point
to}else{//
If the node is Nil,node points to the right subtree of the last root node, node
= Stack.removelast ().
}
return res
}
Stack-middle order
Using the stack to implement the middle sequence traversal
func inordertraversal (root:treenode), [int]
{
var res = [int] ()
var stack = [TreeNode ] ()
var node = root
//If the stack is not empty and the node is not nil while
!stack.isempty | | node! = NIL
{
//If the node is not nil if
node ! = Nil
{
stack.append (node!)
node = node!. Left
}else {
//Store data
res.append (stack.last!. val)
node = Stack.removelast (). Right
}
}
return res
}
Stack- post
Using the stack to implement the post
-traversal func posordertraversal (Root:treenode?), [int]
{
var res = [int] ()
var stack = [Treenod E] () let
node = root
stack.append (node!)
If the stack is not empty while
!stack.isempty
{let
nodetemp = Stack.removelast ()
res.append (nodetemp.val)
If nodetemp.left! = Nil
{
stack.append (nodetemp.left!);
}
If nodetemp.right! = Nil
{
stack.append (nodetemp.right!);
}
}
Return res.reversed ()
}
using recursion to traverse a tree
Recursive traversal is a more brain-free, but also the most basic one, here is not to say that recursion is not good, after all, in the dynamic planning and division of the rule there is also a very useful, the wording is basically the same, but the order is not the same.
Recursive implementation of pre-sequence traversal
func preordertraversal (root:treenode?,_ res:inout [Int])
{
if root! = Nil
{
Res.append (Root. val)!)
Preordertraversal (Root:root?). Left,&res)
preordertraversal (root:root?). right,&res)
}
}
///recursive implementation of the sequence traversal
func inordertraversal (Root:treenode, _ res:inout [Int])
{
if (root = nil)
{
inordertraversal (root:root?. Left, &res)
res.append (root!. val)
inordertraversal (root:root?). Right, &res)
}
}
//Recursively implement post
-traversal func postordertravelsal (Root:treenode, _ res:inout [Int])
{
if root! = Nil {
postordertravelsal (root:root). Left, &res)
postordertravelsal (root:root?). Right, &res)
res.append (root!. val)
}
}
Test
build a two-pronged tree
Of course, the real construction of the binary tree is definitely not such a stupid way, but only for testing, all please do not spit groove =
Initialize a tree
var root = TreeNode (1)
//create subtree
var tree3 = TreeNode (3)
var tree8 = TreeNode (8)
var tree5 = Tre Enode (5)
var tree9 = TreeNode (9)
var tree2 = TreeNode (2)
var tree7 = TreeNode (7)
var tree10 = TreeNode (10)
//Stitching
tree5.right = tree9
tree3.right = tree5
tree3.left = tree8
tree7.right = Tree10
Tree2.right = tree7
root.left = tree3
root.right = tree2
The structure is as follows:
1-3,2
3, 8,5
8, #,#
5, #,9
9, #,#
2, #,7 7, #,10 10 #,#
Test Results
Print ("
stack Traverse:") print ("
preface: \ (Preordertraversal (root:root))") ("
middle order: \ (Inordertraversal (root : root)
print ("post-shipment: \ (Posordertraversal (root:root))")
print ("Recursive traversal:")
var preorder = [Int] ()
Preordertraversal (Root:root,&preorder)
print ("First Order: \ (Preorder)")
var inorder = [Int] ()
Inordertraversal (Root:root, &inorder)
print ("middle order: \ (inorder)")
var postorder = [Int] ()
Postordertravelsal (Root:root, &postorder)
print ("post-post: \ (postorder)")
Stack traversal:
First Order: [1, 3, 8, 5, 9, 2, 7, ten]
preface: [8, 3, 5, 9, 1, 2, 7, ten] Post order
: [8, 9, 5, 3, ten, 7, 2, 1]
recursive traversal:
: [1, 3, 8, 5, 9, 2, 7, ten]
preface: [8, 3, 5, 9, 1, 2, 7, ten] post-order
: [8, 9, 5, 3, ten, 7, 2, 1] program
ended wit H Exit code:0