-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundaryTraversal.cpp
More file actions
80 lines (73 loc) · 1.31 KB
/
Copy pathBoundaryTraversal.cpp
File metadata and controls
80 lines (73 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "stdafx.h"
#include "tree.h"
#include <conio.h>
void PrintLeaves(Node root)
{
if(!root)
return;
PrintLeaves(root->left);
if(!root->left && !root->right) // leaf node
printf("C%d ",root->value);
PrintLeaves(root->right);
}
void LeftBoundary(Node root) // top down
{
if(!root)
return;
if(root->left)
{
printf("L%d ",root->value);
LeftBoundary(root->left);
}
else if(root->right)
{
printf("LR%d ",root->value);
LeftBoundary(root->right);
}
}
void RightBoundary(Node root) // bottom up
{
if(!root)
return;
if(root->right)
{
RightBoundary(root->right);
printf("R%d ",root->value);
}
else if(root->left)
{
RightBoundary(root->left);
printf("RL%d ",root->value);
}
}
void BoundaryTraversal(Node root)
{
if(!root)
return;
printf("B%d ",root->value);
LeftBoundary(root->left);
PrintLeaves(root->left);
PrintLeaves(root->right);
RightBoundary(root->right);
}
int main()
{
Node root = NULL;
root = Insert(root,8);
root = Insert(root,6);
root = Insert(root,7);
root = Insert(root,9);
root = Insert(root,2);
root = Insert(root,4);
root = Insert(root,11);
root = Insert(root,10);
root = Insert(root,12);
root = Insert(root,5);
root = Insert(root,3);
root = Insert(root,1);
PrintTree(root);
printf("\nBoundary order:\n");
BoundaryTraversal(root);
getch();
return 0;
}