-
Notifications
You must be signed in to change notification settings - Fork 2
/
codechallenge_117.py
132 lines (96 loc) · 3.01 KB
/
codechallenge_117.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'''
Date: 05/08/2022
Problem description:
====================
Build an inorder binary tree from a preorder and inorder traversal.
Algorithm:
===========
Inorder traversal
-----------------
1. Traverse the left subtree
2. Visit the root
3. Traverse the right subtree
Use cases: In the case of binary search trees (BST), Inorder traversal gives
nodes in non-decreasing order. To get nodes of BST in non-increasing order,
a variation of Inorder traversal where Inorder traversal s reversed can be used.
Preorder traversal
----------------
1. Visit the root
2. Traverse the left subtree
3. Traverse the right subtree
Use cases: Preorder traversal is used to create a copy of the tree. Preorder
traversal is also used to get prefix expression on an expression tree.
Please see http://en.wikipedia.org/wiki/Polish_notation know why prefix expressions are useful.
Postorder traversal
-----------------
1. Traverse the left subtree
2. Traverse the right subtree
3. Visit the root
Use cases: Postorder traversal is used to delete the tree. Please see the question
for the deletion of a tree for details. Postorder traversal is also useful to get
the postfix expression of an expression tree.
Please see http://en.wikipedia.org/wiki/Reverse_Polish_notation for the usage of postfix expression.
'''
from binarytree import Node
# Python program to for tree traversals
# A class that represents an individual node in a
# Binary Tree
class binarytreeNode:
def __init__(self, key):
self.left = None
self.right = None
self.val = key
# A function to do inorder tree traversal
def printInorder(root):
if root:
# First recur on left child
printInorder(root.left)
# then print the data of node
print(root.val),
# now recur on right child
printInorder(root.right)
# A function to do postorder tree traversal
def printPostorder(root):
if root:
# First recur on left child
printPostorder(root.left)
# the recur on right child
printPostorder(root.right)
# now print the data of node
print(root.val),
# A function to do preorder tree traversal
def printPreorder(root):
if root:
# First print the data of node
print(root.val),
# Then recur on left child
printPreorder(root.left)
# Finally recur on right child
printPreorder(root.right)
def buildBtreeWithNodeLib():
hHeap = Node('*')
hHeap.left = Node('*')
hHeap.left.right = Node('a')
hHeap.left.left = Node('*')
hHeap.left.left.left = Node('c')
hHeap
hHeap.right = Node('*')
hHeap.right.left = Node('t')
hHeap.right.right = Node('*')
hHeap.right.right.right = Node('s')
print(hHeap)
#
# main driver
#
def main():
root = binarytreeNode([1,5,2,9,4,9])
print ("Preorder traversal of binary tree is")
printPreorder(root)
print ("\nInorder traversal of binary tree is")
printInorder(root)
print ("\nPostorder traversal of binary tree is")
printPostorder(root)
print("\nBuilding a binary tree with Node library")
buildBtreeWithNodeLib()
if '__main__' == __name__:
main()