Tree traversal - PowerPoint PPT Presentation

1 / 71
About This Presentation
Title:

Tree traversal

Description:

Preorder traversal (Node-Left-Right; NLR) Postorder traversal (Left-Right-Node; LRN) ... PreOrder. To load from a file: Read each item from file. Insert into ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 72
Provided by: www1Csi
Category:

less

Transcript and Presenter's Notes

Title: Tree traversal


1
Tree traversal
  • void TraverseTree(BintreePtr Root)
  • if(Root ! NULL)
  • TraverseTree(Root.getLeft())
  • System.out.print(Root.getValue()"\t")
  • TraverseTree(Root.getRight())

Base case if the tree is empty do nothing
2
Tree traversal
  • void TraverseTree(BintreePtr Root)
  • if(Root ! NULL)
  • TraverseTree(Root.getLeft())
  • System.out.print(Root.getValue()"\t")
  • TraverseTree(Root.getRight())

Recursively call TraverseTree() to traverse left
subtree...
3
Tree traversal
  • void TraverseTree(BintreePtr Root)
  • if(Root ! NULL)
  • TraverseTree(Root.getLeft())
  • System.out.print(Root.getValue()"\t")
  • TraverseTree(Root.getRight())

Print current node (can alter this step to
do anything to current node)
4
Tree traversal
  • void TraverseTree(BintreePtr Root)
  • if(Root ! NULL)
  • TraverseTree(Root.getLeft())
  • System.out.print(Root.getValue()"\t")
  • TraverseTree(Root.getRight())

Use recursion to traverse right subtree
5
Other types of traversal
  • Inorder traversal (Left-Node-Right LNR)
  • Preorder traversal (Node-Left-Right NLR)
  • Postorder traversal (Left-Right-Node LRN)

6
Inorder traversal (LNR)
3
1
9
12
2
Left
10
Right
7
Inorder traversal (LNR)
3
1
9
12
2
Left 1 2
10
Right 9 10 12
Traversal 1 2 3 9 10 12
8
Preorder traversal (NLR)
3
1
9
12
2
Left
10
Right
9
Preorder traversal (NLR)
3
1
9
12
2
Left 1 2
10
Right 9 12 10
Traversal 3 1 2 9 12 10
10
Postorder traversal (LRN)
3
1
9
12
2
Left
10
Right
11
Postorder traversal (LRN)
3
1
9
12
2
Left 2 1
10
Right 10 12 9
Traversal 2 1 10 12 9 3
12
Deleting nodes from a tree
  • Ensure that BST properties still hold after
    deletion
  • Three types of nodes to delete
  • Leaf
  • Node with one child
  • Node with 2 children

13
Deleting a leaf
X
Delete D
V
F
C
T
Z
U
Y
W
E
D
J
Q
14
Deleting a leaf
X
Delete D
V
F
C
T
Z
U
Y
W
E
J
Q
15
Deleting a node with 1 child
X
Delete F Connect child to parent of F
V
F
C
T
Z
U
Y
W
E
J
Q
16
Deleting a node with 1 child
X
Delete F
V
Z
C
T
Y
W
U
E
J
Q
17
Deleting a node with 2 children
X
Delete X
V
Z
C
T
Y
W
U
E
J
Q
Two ways of doing it...go left or go right
18
Go left...
X
Delete X
V
Z
C
T
Y
W
U
E
J
Q
  • Find RIGHTMOST node in LEFT subtree
  • Usually referred to as InOrder Predecessor
  • Has to have AT MOST one child

19
Go left...
T
Delete X
V
Z
C
T
Y
W
U
E
J
Q
  • Copy VALUE in this node to replace value in
    delete node

20
Go left...
T
Delete X
V
Z
C
T
Y
W
U
E
J
Q
  • Copy VALUE in this node to replace value in
    delete node
  • Then delete InOrder Predecessor

21
Go left...
T
Delete X
V
Z
U
C
Y
W
E
J
Q
Deletion completed
22
Go right...
X
Delete X
V
Z
C
T
Y
W
U
E
J
Q
  • Find LEFTMOST node in RIGHT subtree
  • Usually referred to as InOrder Successor
  • Has to have AT MOST one child

23
Go right...
E
Delete X
V
Z
C
T
Y
W
U
E
J
Q
  • Copy VALUE in this node to replace value in
    delete node

24
Go right...
E
Delete X
V
Z
C
T
Y
W
U
E
J
Q
  • Copy VALUE in this node to replace value in
    delete node
  • Then delete InOrder Successor

25
Go right...
E
Delete X
V
Z
C
T
Y
W
U
J
Q
Deletion completed
26
Counting nodes in a tree
  • int CountNodes(BintreePtr Root)
  • if(Root ! NULL)
  • return 1 CountNodes(Root.getLeft())
  • CountNodes(Root.getRight())
  • else
  • return 0

27
Counting nodes in a tree
  • int CountNodes(BintreePtr Root)
  • if(Root ! NULL)
  • return 1 CountNodes(Root.getLeft())
  • CountNodes(Root.getRight())
  • else
  • return 0

Base case Check that Root isn't NULL
28
Counting nodes in a tree
  • int CountNodes(BintreePtr Root)
  • if(Root ! NULL)
  • return 1 CountNodes(Root.getLeft())
  • CountNodes(Root.getRight())
  • else
  • return 0

Number of nodes is 1 (for current node)...
29
Counting nodes in a tree
  • int CouNpNodes(BintreePtr Root)
  • if(Root ! NULL)
  • return 1 CountNodes(Root.getLeft())
  • CountNodes(Root.getRight())
  • else
  • return 0

Number of nodes in the left subtree...
30
Counting nodes in a tree
  • int CountNodes(BintreePtr Root)
  • if(Root ! NULL)
  • return 1 CountNodes(Root.getLeft())
  • CountNodes(Root.getRight())
  • else
  • return 0

Number of nodes in the right subtree.
31
Counting nodes in a tree
  • int CountNodes(BintreePtr Root)
  • if(Root ! NULL)
  • return 1 CountNodes(Root.getLeft())
  • CountNodes(Root.getRight())
  • else
  • return 0

If Root is NULL, there are no nodes, so return 0
32
Finding the depth of a tree
  • The depth of a tree is the maximum path length
    from the root to a leaf
  • Depth of tree 5

33
Finding the depth of a tree
  • Recursive algorithm
  • if(Root ! NULL) / base case /
  • Find the depth of the left branch
  • ...and of right branch
  • Depth 1 max of depth(left) and
    depth(right)
  • Else
  • Depth 0

34
Finding the depth of a tree
If Root is NULL, there are no nodes, so return 0
  • int Depth(BintreePtr Root)
  • int DepthLeft, DepthRight
  • if(Root ! NULL)
  • DepthLeft Depth(Root.getLeft())
  • DepthRight Depth(Root.getRight())
  • if (DepthLeft gt DepthRight)
  • return 1 DepthLeft
  • else
  • return 1 DepthRight
  • else
  • return 0

35
Finding the depth of a tree
Recursively find depths of left and right branches
  • int Depth(BintreePtr Root)
  • int DepthLeft, DepthRight
  • if(Root ! NULL)
  • DepthLeft Depth(Root.getLeft())
  • DepthRight Depth(Root.getRight())
  • if (DepthLeft gt DepthRight)
  • return 1 DepthLeft
  • else
  • return 1 DepthRight
  • else
  • return 0

36
Finding the depth of a tree
Find which branch has larger depth
  • int Depth(BintreePtr Root)
  • int DepthLeft, DepthRight
  • if(Root ! NULL)
  • DepthLeft Depth(Root.getLeft())
  • DepthRight Depth(Root.getRight())
  • if (DepthLeft gt DepthRight)
  • return 1 DepthLeft
  • else
  • return 1 DepthRight
  • else
  • return 0

37
Finding the depth of a tree
...and return 1 (for current node) larger depth
  • int Depth(BintreePtr Root)
  • int DepthLeft, DepthRight
  • if(Root ! NULL)
  • DepthLeft Depth(Root.getLeft())
  • DepthRight Depth(Root.getRight())
  • if (DepthLeft gt DepthRight)
  • return 1 DepthLeft
  • else
  • return 1 DepthRight
  • else
  • return 0

38
Finding the depth of a tree
If tree is empty, depth is zero
  • int Depth(BintreePtr Root)
  • int DepthLeft, DepthRight
  • if(Root ! NULL)
  • DepthLeft Depth(Root.getLeft())
  • DepthRight Depth(Root.getRight())
  • if (DepthLeft gt DepthRight)
  • return 1 DepthLeft
  • else
  • return 1 DepthRight
  • else
  • return 0

39
Counting the leaves in a tree
  • Recursive algorithm
  • if(Root ! NULL) / base case /
  • If (Root is a leaf)
  • return 1
  • Else
  • return Leaves(Root.getLeft()) Leaves(Root.getRig
    ht())
  • Else
  • return 0

40
Counting the leaves in a tree
  • int Leaves(BintreePtr Root)
  • if(Root ! NULL)
  • if (Root.getLeft() NULL Root.getRight()
    NULL)
  • return 1
  • else
  • return Leaves(Root.getLeft())
  • Leaves(Root.getRight())
  • else
  • return 0

41
Saving a tree to a file
  • Other data structures (stacks, queues, lists,
    etc) easy to save to disk
  • linear data structures
  • traverse list or array, save to file in order
  • Trees are 2D data structures
  • File structure is linear
  • What to do?

42
Saving a tree to a file
  • Traverse the tree
  • Save data in traversal order
  • Which traversal order?
  • Inorder results in right-degenerate tree
  • PostOrder not right-degenerate but not the same
    as the tree we saved
  • PreOrder ?
  • To load from a file
  • Read each item from file
  • Insert into (initially empty) tree

43
Save tree
3
1
9
12
2
10
Preorder traversal gives 3 1 2 9 12 10
44
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Output -gt E V Z C T Y W U Q J
45
Breath-first Traversal
  • BreathFirst(Node Root)
  • Queue Q new Queue()
  • if(Root ! NULL) Q.Join(Root) // insert
    node into Q
  • while ( !Q.isempty())
  • Root Q.Leave() // remove next node
  • Visit(Root) // visit node
  • // insert left subtree in Q
  • if(Root.getLeft() ! NULL) Q.Join(Root.getLeft
    ())
  • // insert right subtree in Q
  • if(Root.getRight() ! NULL) Q.Join(Root.getRig
    ht())

46
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt E
updated Q -gt V Z
Output -gt E
47
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt V Z
updated Q -gt Z C T
Output -gt E V
48
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt Z C T
updated Q -gt C T Y W
Output -gt E V Z
49
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt C T Y W
updated Q -gt T Y W
Output -gt E V Z C
50
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt T Y W
updated Q -gt Y W U
Output -gt E V Z C T
51
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt Y W U
updated Q -gt W U
Output -gt E V Z C T Y
52
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt W U
updated Q -gt U Q J
Output -gt E V Z C T Y W
53
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt U Q J
updated Q -gt Q J
Output -gt E V Z C T Y W U
54
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt Q J
updated Q -gt J
Output -gt E V Z C T Y W U Q
55
Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt J
updated Q -gt empty
Output -gt E V Z C T Y W U Q J
56
Reconstructing a tree from its traversals
  • Can draw a binary tree given
  • Inorder traversal (essential)
  • One of
  • Preorder traversal
  • Postorder traversal

57
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Start with postorder
  • Last item (X) must be root of tree

X
58
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Locate X in inorder traversal
  • Items to left of X are in left subtree
  • Items to right of X are in right subtree

X
59
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Construct left subtree
  • Find root of left subtree from postorder (V)

X
V
60
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Find V in inorder traversal
  • C is left child of V

X
V
C
61
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • U T are right subtree of V
  • Use postorder to find root of right subtree (T)

X
V
C
T
62
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Find T in inorder
  • U is left child of T

X
V
C
T
U
63
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Now consider right subtree of X
  • Find root from postorder (F)

X
V
F
C
T
U
64
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Find F in inorder
  • All other nodes are in right subtree of F

X
V
F
C
T
U
65
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Find right subtree of F in postorder
  • Root is Z

X
V
F
C
T
Z
U
66
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Find Z in inorder
  • E Y D in left subtree of Z
  • Q W J in right subtree of Z

X
V
F
C
T
Z
U
67
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Consider left subtree ( E Y D)
  • Find root from postorder (Y)

X
V
F
C
T
Z
U
Y
68
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Find Y in inorder
  • E is left child of Y
  • D is right child of Y

X
V
F
C
T
Z
U
Y
E
D
69
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Consider right subtree of Z
  • Find root from postorder (W)

X
V
F
C
T
Z
U
Y
W
E
D
70
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Find W in inorder
  • Q is left child of W
  • J is right child of W

X
V
F
C
T
Z
U
Y
W
E
D
J
Q
71
Inorder C V U T X F E Y D Z Q W JPostorder C U
T V E D Y Q J W Z F X
  • Tree complete

X
V
F
C
T
Z
U
Y
W
E
D
J
Q
Write a Comment
User Comments (0)
About PowerShow.com