Title: Tree traversal
1Tree 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
2Tree 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...
3Tree 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)
4Tree 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
5Other types of traversal
- Inorder traversal (Left-Node-Right LNR)
- Preorder traversal (Node-Left-Right NLR)
- Postorder traversal (Left-Right-Node LRN)
6Inorder traversal (LNR)
3
1
9
12
2
Left
10
Right
7Inorder traversal (LNR)
3
1
9
12
2
Left 1 2
10
Right 9 10 12
Traversal 1 2 3 9 10 12
8Preorder traversal (NLR)
3
1
9
12
2
Left
10
Right
9Preorder traversal (NLR)
3
1
9
12
2
Left 1 2
10
Right 9 12 10
Traversal 3 1 2 9 12 10
10Postorder traversal (LRN)
3
1
9
12
2
Left
10
Right
11Postorder traversal (LRN)
3
1
9
12
2
Left 2 1
10
Right 10 12 9
Traversal 2 1 10 12 9 3
12Deleting 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
13Deleting a leaf
X
Delete D
V
F
C
T
Z
U
Y
W
E
D
J
Q
14Deleting a leaf
X
Delete D
V
F
C
T
Z
U
Y
W
E
J
Q
15Deleting 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
16Deleting a node with 1 child
X
Delete F
V
Z
C
T
Y
W
U
E
J
Q
17Deleting 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
18Go 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
19Go 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
20Go 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
21Go left...
T
Delete X
V
Z
U
C
Y
W
E
J
Q
Deletion completed
22Go 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
23Go 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
24Go 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
25Go right...
E
Delete X
V
Z
C
T
Y
W
U
J
Q
Deletion completed
26Counting nodes in a tree
- int CountNodes(BintreePtr Root)
-
- if(Root ! NULL)
- return 1 CountNodes(Root.getLeft())
- CountNodes(Root.getRight())
- else
- return 0
-
27Counting 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
28Counting 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)...
29Counting 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...
30Counting 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.
31Counting 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
32Finding 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
33Finding 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
34Finding 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
35Finding 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
36Finding 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
37Finding 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
38Finding 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
39Counting 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
40Counting 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
41Saving 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?
42Saving 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
43Save tree
3
1
9
12
2
10
Preorder traversal gives 3 1 2 9 12 10
44Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Output -gt E V Z C T Y W U Q J
45Breath-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()) -
46Breath-first Traversal
E
V
Z
C
T
Y
W
U
J
Q
Q -gt E
updated Q -gt V Z
Output -gt E
47Breath-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
48Breath-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
49Breath-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
50Breath-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
51Breath-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
52Breath-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
53Breath-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
54Breath-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
55Breath-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
56Reconstructing a tree from its traversals
- Can draw a binary tree given
- Inorder traversal (essential)
- One of
- Preorder traversal
- Postorder traversal
57Inorder 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
58Inorder 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
59Inorder 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
60Inorder 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
61Inorder 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
62Inorder 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
63Inorder 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
64Inorder 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
65Inorder 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
66Inorder 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
67Inorder 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
68Inorder 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
69Inorder 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
70Inorder 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
71Inorder 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
X
V
F
C
T
Z
U
Y
W
E
D
J
Q