-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree-traversal.html
More file actions
1067 lines (747 loc) · 47.7 KB
/
tree-traversal.html
File metadata and controls
1067 lines (747 loc) · 47.7 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tree Traversal</title>
<!-- Google tag (gtag.js) -->
<script async="" src="https://www.googletagmanager.com/gtag/js?id=G-HCERBDV76D"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-HCERBDV76D');
</script>
<meta content="B0487B46A104E90209E8A3BEA24ECA0E" name="msvalidate.01"/>
<meta content="f044b3a12c7918f1" name="yandex-verification"/>
<!--end-->
<meta content="Python tutorial to learn Python.The Complete Python Programming Course,Learning Python for Data Analysis and Visualization,Python for Beginners." name="description"/>
<meta content="learnPython" name="author"/>
<meta content="Learn Python for free,learn python for beginners,Core Python,Web frameworks,Multiprocess architecture,Serverside templating language,python tutorials,python4" name="keywords"/>
<meta content="website" property="og:type"/>
<meta content="learn about data types, variables, lists, tuples, dictionaries, decision-making statements, and loops,user-defined functions, object-oriented programming, threading, and Python scripting." property="og:description"/>
<meta content="US-CA" name="”geo.region”"/>
<meta content="353 Jane Stanford Way, Stanford, CA 94305, United States" name="”geo.placename”"/>
<meta content="37.430089898615456;-122.17332683124829" name="”geo.position”"/>
<meta content="37.430089898615456, -122.17332683124829" name="”ICBM”"/>
<link href="https://pythonread.github.io/?m=1" rel="alternate"/>
<link href="/favicon.png" rel="icon"/>
<meta charset="utf-8"/>
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<link href="/style1.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style2.css" media="all" rel="stylesheet" type="text/css"/>
<link href="/style0.css" media="all" rel="stylesheet" type="text/css"/>
<!--<![endif]-->
</head>
<body>
<div class="nav-wrapper">
<div class="container">
<nav>
<div class="logo wave">
<a href="/" id="logo">
Python Tutorial
</a>
</div>
<div class="nav-toggle-icon" id="nav-toggle-icon" onclick="mobileMenu()">
<div class="material-hamburger">
<span>
</span>
<span>
</span>
<span>
</span>
</div>
</div>
<div class="menu-wrapper" id="menu-wrapper">
<div class="nav-indicator">
</div>
<ul class="menus">
<li>
<a class="wave" href="/">
Home
</a>
</li>
<li>
<a class="wave" href="/projects.html">
Projects
</a>
</li>
<li>
<a class="wave" href="/free-course.html" target="_blank">
Free Course
</a>
</li>
<li>
<a class="wave" href="/dsa.html">
DSA
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="contents contents--neg" style="margin-top: 60px">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="d-flex">
<div class="left-bar d-none d-lg-block">
<div class="card-alt mb-10x">
<h3>Page Index</h3>
<div class="list">
<ul>
<li><a href="/dsa.html#data-structure-1" title="Data Structures (I)">Data Structures
(I)</a></li>
<li><a href="/dsa.html#data-structure-2" title="Data Structures (II)">Data Structures
(II)</a></li>
<li><a href="/dsa.html#tree-1" title="Tree based DSA (I)">Tree based DSA (I)</a></li>
<li><a href="/dsa.html#tree-2" title="Tree based DSA (II)">Tree based DSA (II)</a></li>
<li><a href="/dsa.html#graph" title="Graph Data Structures and Algorithm">Graph based
DSA</a></li>
<li><a href="/dsa.html#sorting-searching" title="Sorting and Searching">Sorting and
Searching</a></li>
<li><a href="/dsa.html#greedy-algorithm" title="Greedy Algorithms">Greedy Algorithms</a>
</li>
<li><a href="/dsa.html#dynamic-programming" title="Dynamic Programming">Dynamic
Programming</a></li>
<li><a href="/dsa.html#other-algorithms" title="Other Algorithms">Other Algorithms</a>
</li>
</ul>
</div>
</div>
</div>
<div class="right-bar">
<!--first part end-------------------------------------------->
<div class="editor-contents">
<h1>Tree Traversal - inorder, preorder and postorder</h1>
<p class="editor-contents__short-description">In this tutorial, you will learn about different tree traversal techniques. Also, you will find working examples of different tree traversal methods in C, C++, Java and Python.</p>
<div id="node-1017" class="node node-algorithm clearfix" about="/dsa/tree-traversal" typeof="sioc:Item foaf:Document">
<span property="dc:title" content="Tree Traversal - inorder, preorder and postorder" class="rdf-meta element-hidden"></span>
<div class="content">
<p id="introduction">Traversing a tree means visiting every node in the tree. You might, for instance, want to add all the values in the tree or find the largest one. For all these operations, you will need to visit each node of the tree.</p>
<p>Linear data structures like arrays, <a href="/dsa/stack.html">stacks</a>, <a href="/dsa/queue.html">queues</a>, and <a href="/dsa/linked-list.html">linked list</a> have only one way to read the data. But a hierarchical data structure like a <a href="/dsa/trees.html">tree</a> can be traversed in different ways.</p>
<figure><img alt="sample tree to learn tree traversal - root node contains 1 with leftchild as 12 and right child as 9. The left child of root further has left child 5 and right child 6" src="//cdn.programiz.com/sites/tutorial2program/files/tree_traversal_tree-traversal.png" title="Tree Traversal" width="730" height="292"><figcaption>Tree traversal</figcaption></figure><p>Let's think about how we can read the elements of the tree in the image shown above.</p>
<p>Starting from top, Left to right</p>
<pre style="max-height: 600px;"><code class="dsa hljs yaml"><span class="hljs-number">1</span> <span class="hljs-string">-></span> <span class="hljs-number">12</span> <span class="hljs-string">-></span> <span class="hljs-number">5</span> <span class="hljs-string">-></span> <span class="hljs-number">6</span> <span class="hljs-string">-></span> <span class="hljs-number">9</span></code></pre>
<p>Starting from bottom, Left to right</p>
<pre style="max-height: 600px;"><code class="dsa hljs yaml"><span class="hljs-number">5</span> <span class="hljs-string">-></span> <span class="hljs-number">6</span> <span class="hljs-string">-></span> <span class="hljs-number">12</span> <span class="hljs-string">-></span> <span class="hljs-number">9</span> <span class="hljs-string">-></span> <span class="hljs-number">1</span></code></pre>
<p>Although this process is somewhat easy, it doesn't respect the hierarchy of the tree, only the depth of the nodes.</p>
<p>Instead, we use traversal methods that take into account the basic structure of a tree i.e.</p>
<pre style="max-height: 600px;"><code class="dsa hljs cpp"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> {</span>
<span class="hljs-keyword">int</span> data;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>* <span class="hljs-title">left</span>;</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>* <span class="hljs-title">right</span>;</span>
}</code></pre>
<p>The struct node pointed to by <var>left</var> and <var>right</var> might have other left and right children so we should think of them as sub-trees instead of sub-nodes.</p>
<p>According to this structure, every tree is a combination of</p>
<ul><li>A node carrying data</li>
<li>Two subtrees</li>
</ul><figure><img alt="root node with left subtree and right subtree" src="//cdn.programiz.com/sites/tutorial2program/files/tree_traversal_sub-tree-concept.png" title="Left and Right Subtree" width="730" height="323"><figcaption>Left and Right Subtree</figcaption></figure><p>Remember that our goal is to visit each node, so we need to visit all the nodes in the subtree, visit the root node and visit all the nodes in the right subtree as well.</p>
<p>Depending on the order in which we do this, there can be three types of traversal.</p>
<hr><h2 id="inorder">Inorder traversal</h2>
<ol><li>First, visit all the nodes in the left subtree</li>
<li>Then the root node</li>
<li>Visit all the nodes in the right subtree</li>
</ol><pre style="max-height: 600px;"><code class="dsa hljs less"><span class="hljs-selector-tag">inorder</span>(root->left)
<span class="hljs-selector-tag">display</span>(root->data)
<span class="hljs-selector-tag">inorder</span>(root->right)</code></pre>
<hr><h2 id="preorder">Preorder traversal</h2>
<ol><li>Visit root node</li>
<li>Visit all the nodes in the left subtree</li>
<li>Visit all the nodes in the right subtree</li>
</ol><pre style="max-height: 600px;"><code class="dsa hljs less"><span class="hljs-selector-tag">display</span>(root->data)
<span class="hljs-selector-tag">preorder</span>(root->left)
<span class="hljs-selector-tag">preorder</span>(root->right)</code></pre>
<hr><h2 id="postorder">Postorder traversal</h2>
<ol><li>Visit all the nodes in the left subtree</li>
<li>Visit all the nodes in the right subtree</li>
<li>Visit the root node</li>
</ol><pre style="max-height: 600px;"><code class="dsa hljs less"><span class="hljs-selector-tag">postorder</span>(root->left)
<span class="hljs-selector-tag">postorder</span>(root->right)
<span class="hljs-selector-tag">display</span>(root->data)</code></pre>
<p></p><div class="clearfix"></div><p>Let's visualize in-order traversal. We start from the root node.</p>
<figure><img alt="outlining left subtree, right subtree and root node" src="//cdn.programiz.com/sites/tutorial2program/files/tree_traversal_inorder-traversal.png" title="Left and Right Subtree" width="730" height="364"><figcaption>Left and Right Subtree</figcaption></figure><p>We traverse the left subtree first. We also need to remember to visit the root node and the right subtree when this tree is done.</p>
<p>Let's put all this in a <a href="/dsa/stack.html">stack</a> so that we remember.</p>
<figure><img alt="we put the left subtree, root node and right subtree in a stack in that order so that we can display root node and traverse right subtree when we are done with left subtree" src="//cdn.programiz.com/sites/tutorial2program/files/tree_traversal_inorder-stack_0.png" title="Stack" width="730" height="400"><figcaption>Stack</figcaption></figure><p>Now we traverse to the subtree pointed on the TOP of the stack.</p>
<p>Again, we follow the same rule of inorder</p>
<pre>Left subtree -> root -> right subtree</pre>
<p>After traversing the left subtree, we are left with</p>
<figure><img alt="situation of stack after traversing left subtree, stack now contains the elements of left subtree, followed by root, followed by right child of root" src="//cdn.programiz.com/sites/tutorial2program/files/tree_traversal_inorder-stack_1.png" title="Stack" width="730" height="468"><figcaption>Final Stack</figcaption></figure><p>Since the node "5" doesn't have any subtrees, we print it directly. After that we print its parent "12" and then the right child "6".</p>
<p>Putting everything on a stack was helpful because now that the left-subtree of the root node has been traversed, we can print it and go to the right subtree.</p>
<p>After going through all the elements, we get the inorder traversal as</p>
<pre><samp>5 -> 12 -> 6 -> 1 -> 9</samp></pre>
<p>We don't have to create the stack ourselves because recursion maintains the correct order for us.</p>
<hr><h2 id="code">Python, Java and C/C++ Examples</h2>
<div class="tabbed-editor">
<div id="py1" onclick="changepy()" class="tabbed-editor__node tabbed-editor__node--active"><a href="#python-code">Python</a></div>
<div id="java1" onclick="changejava()" class="tabbed-editor__node"><a href="#java-code">Java</a></div>
<div id="c1" onclick="changec()" class="tabbed-editor__node"><a href="#c-code">C</a></div>
<div id="cpp1" onclick="changecpp()" class="tabbed-editor__node"><a href="#cpp-code">C+</a></div>
</div>
<div class="code-editor code-editor--tabbed">
<div class="code-editor__area code-editor__area--active" id="python-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="python hljs"><span class="hljs-comment"># Tree traversal in Python</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, item)</span>:</span>
self.left = <span class="hljs-literal">None</span>
self.right = <span class="hljs-literal">None</span>
self.val = item
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">inorder</span><span class="hljs-params">(root)</span>:</span>
<span class="hljs-keyword">if</span> root:
<span class="hljs-comment"># Traverse left</span>
inorder(root.left)
<span class="hljs-comment"># Traverse root</span>
<span class="hljs-keyword">print</span>(str(root.val) + <span class="hljs-string">"->"</span>, end=<span class="hljs-string">''</span>)
<span class="hljs-comment"># Traverse right</span>
inorder(root.right)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">postorder</span><span class="hljs-params">(root)</span>:</span>
<span class="hljs-keyword">if</span> root:
<span class="hljs-comment"># Traverse left</span>
postorder(root.left)
<span class="hljs-comment"># Traverse right</span>
postorder(root.right)
<span class="hljs-comment"># Traverse root</span>
<span class="hljs-keyword">print</span>(str(root.val) + <span class="hljs-string">"->"</span>, end=<span class="hljs-string">''</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">preorder</span><span class="hljs-params">(root)</span>:</span>
<span class="hljs-keyword">if</span> root:
<span class="hljs-comment"># Traverse root</span>
<span class="hljs-keyword">print</span>(str(root.val) + <span class="hljs-string">"->"</span>, end=<span class="hljs-string">''</span>)
<span class="hljs-comment"># Traverse left</span>
preorder(root.left)
<span class="hljs-comment"># Traverse right</span>
preorder(root.right)
root = Node(<span class="hljs-number">1</span>)
root.left = Node(<span class="hljs-number">2</span>)
root.right = Node(<span class="hljs-number">3</span>)
root.left.left = Node(<span class="hljs-number">4</span>)
root.left.right = Node(<span class="hljs-number">5</span>)
<span class="hljs-keyword">print</span>(<span class="hljs-string">"Inorder traversal "</span>)
inorder(root)
<span class="hljs-keyword">print</span>(<span class="hljs-string">"\nPreorder traversal "</span>)
preorder(root)
<span class="hljs-keyword">print</span>(<span class="hljs-string">"\nPostorder traversal "</span>)
postorder(root)</code></pre></div>
</div>
<div class="code-editor__area" id="java-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="java hljs"><span class="hljs-comment">// Tree traversal in Java</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span> </span>{
<span class="hljs-keyword">int</span> item;
Node left, right;
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Node</span><span class="hljs-params">(<span class="hljs-keyword">int</span> key)</span> </span>{
item = key;
left = right = <span class="hljs-keyword">null</span>;
}
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BinaryTree</span> </span>{
<span class="hljs-comment">// Root of Binary Tree</span>
Node root;
BinaryTree() {
root = <span class="hljs-keyword">null</span>;
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">postorder</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">if</span> (node == <span class="hljs-keyword">null</span>)
<span class="hljs-keyword">return</span>;
<span class="hljs-comment">// Traverse left</span>
postorder(node.left);
<span class="hljs-comment">// Traverse right</span>
postorder(node.right);
<span class="hljs-comment">// Traverse root</span>
System.out.print(node.item + <span class="hljs-string">"->"</span>);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">inorder</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">if</span> (node == <span class="hljs-keyword">null</span>)
<span class="hljs-keyword">return</span>;
<span class="hljs-comment">// Traverse left</span>
inorder(node.left);
<span class="hljs-comment">// Traverse root</span>
System.out.print(node.item + <span class="hljs-string">"->"</span>);
<span class="hljs-comment">// Traverse right</span>
inorder(node.right);
}
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">preorder</span><span class="hljs-params">(Node node)</span> </span>{
<span class="hljs-keyword">if</span> (node == <span class="hljs-keyword">null</span>)
<span class="hljs-keyword">return</span>;
<span class="hljs-comment">// Traverse root</span>
System.out.print(node.item + <span class="hljs-string">"->"</span>);
<span class="hljs-comment">// Traverse left</span>
preorder(node.left);
<span class="hljs-comment">// Traverse right</span>
preorder(node.right);
}
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
BinaryTree tree = <span class="hljs-keyword">new</span> BinaryTree();
tree.root = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">1</span>);
tree.root.left = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">12</span>);
tree.root.right = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">9</span>);
tree.root.left.left = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">5</span>);
tree.root.left.right = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">6</span>);
System.out.println(<span class="hljs-string">"Inorder traversal"</span>);
tree.inorder(tree.root);
System.out.println(<span class="hljs-string">"\nPreorder traversal "</span>);
tree.preorder(tree.root);
System.out.println(<span class="hljs-string">"\nPostorder traversal"</span>);
tree.postorder(tree.root);
}
}</code></pre></div>
</div>
<div class="code-editor__area" id="c-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="c hljs cpp"><span class="hljs-comment">// Tree traversal in C</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdio.h></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><stdlib.h></span></span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> {</span>
<span class="hljs-keyword">int</span> item;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>* <span class="hljs-title">left</span>;</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>* <span class="hljs-title">right</span>;</span>
};
<span class="hljs-comment">// Inorder traversal</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">inorderTraversal</span><span class="hljs-params">(struct node* root)</span> </span>{
<span class="hljs-keyword">if</span> (root == <span class="hljs-literal">NULL</span>) <span class="hljs-keyword">return</span>;
inorderTraversal(root->left);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d ->"</span>, root->item);
inorderTraversal(root->right);
}
<span class="hljs-comment">// preorderTraversal traversal</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">preorderTraversal</span><span class="hljs-params">(struct node* root)</span> </span>{
<span class="hljs-keyword">if</span> (root == <span class="hljs-literal">NULL</span>) <span class="hljs-keyword">return</span>;
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d ->"</span>, root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
<span class="hljs-comment">// postorderTraversal traversal</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">postorderTraversal</span><span class="hljs-params">(struct node* root)</span> </span>{
<span class="hljs-keyword">if</span> (root == <span class="hljs-literal">NULL</span>) <span class="hljs-keyword">return</span>;
postorderTraversal(root->left);
postorderTraversal(root->right);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d ->"</span>, root->item);
}
<span class="hljs-comment">// Create a new Node</span>
<span class="hljs-function">struct node* <span class="hljs-title">createNode</span><span class="hljs-params">(value)</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>* <span class="hljs-title">newNode</span> = <span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">node</span>));</span>
newNode->item = value;
newNode->left = <span class="hljs-literal">NULL</span>;
newNode->right = <span class="hljs-literal">NULL</span>;
<span class="hljs-keyword">return</span> newNode;
}
<span class="hljs-comment">// Insert on the left of the node</span>
<span class="hljs-function">struct node* <span class="hljs-title">insertLeft</span><span class="hljs-params">(struct node* root, <span class="hljs-keyword">int</span> value)</span> </span>{
root->left = createNode(value);
<span class="hljs-keyword">return</span> root->left;
}
<span class="hljs-comment">// Insert on the right of the node</span>
<span class="hljs-function">struct node* <span class="hljs-title">insertRight</span><span class="hljs-params">(struct node* root, <span class="hljs-keyword">int</span> value)</span> </span>{
root->right = createNode(value);
<span class="hljs-keyword">return</span> root->right;
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>* <span class="hljs-title">root</span> = <span class="hljs-title">createNode</span>(1);</span>
insertLeft(root, <span class="hljs-number">12</span>);
insertRight(root, <span class="hljs-number">9</span>);
insertLeft(root->left, <span class="hljs-number">5</span>);
insertRight(root->left, <span class="hljs-number">6</span>);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"Inorder traversal \n"</span>);
inorderTraversal(root);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nPreorder traversal \n"</span>);
preorderTraversal(root);
<span class="hljs-built_in">printf</span>(<span class="hljs-string">"\nPostorder traversal \n"</span>);
postorderTraversal(root);
}</code></pre></div>
</div>
<div class="code-editor__area" id="cpp-code">
<div class="pre-code-wrapper"><div title="Click to copy" class="copy-code-button"></div><pre class="exec" style="max-height: 600px;"><code class="cpp hljs"><span class="hljs-comment">// Tree traversal in C++</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> {</span>
<span class="hljs-keyword">int</span> data;
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span> *<span class="hljs-title">left</span>, *<span class="hljs-title">right</span>;</span>
Node(<span class="hljs-keyword">int</span> data) {
<span class="hljs-keyword">this</span>->data = data;
left = right = <span class="hljs-literal">NULL</span>;
}
};
<span class="hljs-comment">// Preorder traversal</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">preorderTraversal</span><span class="hljs-params">(struct Node* node)</span> </span>{
<span class="hljs-keyword">if</span> (node == <span class="hljs-literal">NULL</span>)
<span class="hljs-keyword">return</span>;
<span class="hljs-built_in">cout</span> << node->data << <span class="hljs-string">"->"</span>;
preorderTraversal(node->left);
preorderTraversal(node->right);
}
<span class="hljs-comment">// Postorder traversal</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">postorderTraversal</span><span class="hljs-params">(struct Node* node)</span> </span>{
<span class="hljs-keyword">if</span> (node == <span class="hljs-literal">NULL</span>)
<span class="hljs-keyword">return</span>;
postorderTraversal(node->left);
postorderTraversal(node->right);
<span class="hljs-built_in">cout</span> << node->data << <span class="hljs-string">"->"</span>;
}
<span class="hljs-comment">// Inorder traversal</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">inorderTraversal</span><span class="hljs-params">(struct Node* node)</span> </span>{
<span class="hljs-keyword">if</span> (node == <span class="hljs-literal">NULL</span>)
<span class="hljs-keyword">return</span>;
inorderTraversal(node->left);
<span class="hljs-built_in">cout</span> << node->data << <span class="hljs-string">"->"</span>;
inorderTraversal(node->right);
}
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">Node</span>* <span class="hljs-title">root</span> = <span class="hljs-title">new</span> <span class="hljs-title">Node</span>(1);</span>
root->left = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">12</span>);
root->right = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">9</span>);
root->left->left = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">5</span>);
root->left->right = <span class="hljs-keyword">new</span> Node(<span class="hljs-number">6</span>);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Inorder traversal "</span>;
inorderTraversal(root);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\nPreorder traversal "</span>;
preorderTraversal(root);
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"\nPostorder traversal "</span>;
postorderTraversal(root);</code></pre></div>
</div>
</div>
</div>
</div>
<div class="tutorial-toc"><div class="tutorial-toc__inner"><h3 class="tutorial-toc__title">Table of Contents
<button class="btn btn--clear align-items-center">
<svg class="programiz-icon"><use xlink:href="/sites/all/themes/programiz/assets/feather-sprite.svg#x"></use></svg></button></h3><div class="tutorial-toc__links"><ul><li><a href="#introduction">Introduction</a></li>
<li><a href="#inorder">Inorder traversal</a></li>
<li><a href="#preorder">Preorder traversal</a></li>
<li><a href="#postorder">Postorder traversal</a></li>
<li><a href="#code">Python, Java and C/C++ Examples</a></li>
</ul></div></div></div> </div>
<!--second------------------------------------------->
<div>
<ul>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/stack.html">
Stack</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/queue.html">
Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/types-of-queue.html">
Types of Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/circular-queue.html">
Circular Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/priority-queue.html">
Priority Queue</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deque.html">
Deque</a></button>
<br/>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/linked-list.html">
Linked List</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/linked-list-operations.html">
Linked List Operations</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/linked-list-types.html">
Types of Linked List</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/hash-table.html">
Hash Table</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/heap-data-structure.html">
Heap Data Structure</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/fibonacci-heap.html">
Fibonacci Heap</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/decrease-key-and-delete-node-from-a-fibonacci-heap.html">
Decrease Key and Delete node from Fibonacci Heap</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/trees.html">
Tree Data Structure</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/tree-traversal.html">
Tree Traversal</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/binary-tree.html">
Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/full-binary-tree.html">
Full Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/perfect-binary-tree.html">
Perfect Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/complete-binary-tree.html">
Complete Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/balanced-binary-tree.html">
Balanced Binary Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/binary-search-tree.html">
Binary Search Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/avl-tree.html">
AVL Tree</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/b-tree.html">
B Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-into-a-b-tree.html">
Insertion into B-tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deletion-from-a-b-tree.html">
Deletion from B-tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/b-plus-tree.html">
B+ Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-on-a-b-plus-tree.html">
Insertion on a B+ Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deletion-from-a-b-plus-tree.html">
Deletion from a B+ Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/red-black-tree.html">
Red Black Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-in-a-red-black-tree.html">
Insertion in Red Black Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/deletion-from-a-red-black-tree.html">
Deletion from Red Black Tree</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph.html">
Graph Data Structure</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/spanning-tree-and-minimum-spanning-tree.html">
Spanning Tree</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/strongly-connected-components.html">
Strongly Connected Components</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-adjacency-matrix.html">
Adjacency Matrix</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-adjacency-list.html">
Adjacency List</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-dfs.html">
DFS Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/graph-bfs.html">
Breadth-first Search</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/bellman-ford-algorithm.html">
Bellman Ford's Algorithm</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/bubble-sort.html">
Bubble Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/selection-sort.html">
Selection Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/insertion-sort.html">
Insertion Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/merge-sort.html">
Merge Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/quick-sort.html">
Quick Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/counting-sort.html">
Counting Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/radix-sort.html">
Radix Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/bucket-sort.html">
Bucket Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/heap-sort.html">
Heap Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/shell-sort.html">
Shell Sort</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/linear-search.html">
Linear Search</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/binary-search.html">
Binary Search</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/greedy-algorithm.html">
Greedy Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/ford-fulkerson-algorithm.html">
Ford-Fulkerson Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/dijkstra-algorithm.html">
Dijkstra's Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/kruskal-algorithm.html">
Kruskal's Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/prim-algorithm.html">
Prim's Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/huffman-coding.html">
Huffman Code</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/dynamic-programming.html">
Dynamic Programming</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/floyd-warshall-algorithm.html">
Floyd Warshall Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/longest-common-subsequence.html">
Longest Common Subsequence</a></button>
<br>
<br/>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/backtracking-algorithm.html">
Backtracking Algorithm</a></button>
<button style="padding:5px; margin-bottom:2px"><a href="
/dsa/rabin-karp-algorithm.html">
Rabin-Karp Algorithm</a></button>
<br>
<br/>
<h2>Free Courses on YouTube</h2>
<br/>
<img height="35px" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT9Spnddn1UerjXEbcWQUPhfDHLL9rppkmUp1x4JRPKsdTwmX0kI26Rd0-2qKlwGAAnbgiPz7dZMm5Rv5Fd1QLhkQC6n3ob13ug1bTYtizkDpk2B0LxRiA8QUIjeTmfu6vZHoKJ7pXUgt73sHoXvSpGWjKQ6ZM01IIbJnXmwzXq4RdNYA4swdULgNy/s16000/youtube-logo.png" width="35px"/>
<button style="padding:5px; margin-bottom:2px"><a href="
https://www.youtube.com/watch?v=IbSXF7eT-AU&list=PLR_5PTwg_uAQw40OhdvAwN4NvzWv0xOZ2">
Python Full Course Playlist</a></button>
<br/>
<img height="35px" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT9Spnddn1UerjXEbcWQUPhfDHLL9rppkmUp1x4JRPKsdTwmX0kI26Rd0-2qKlwGAAnbgiPz7dZMm5Rv5Fd1QLhkQC6n3ob13ug1bTYtizkDpk2B0LxRiA8QUIjeTmfu6vZHoKJ7pXUgt73sHoXvSpGWjKQ6ZM01IIbJnXmwzXq4RdNYA4swdULgNy/s16000/youtube-logo.png" width="35px"/>
<button style="padding:5px; margin-bottom:2px"><a href="https://www.youtube.com/watch?v=UEl6wB90Gs8">
105 STL Algorithms in Less Than an Hour</a></button>
<br/>
<img height="35px" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT9Spnddn1UerjXEbcWQUPhfDHLL9rppkmUp1x4JRPKsdTwmX0kI26Rd0-2qKlwGAAnbgiPz7dZMm5Rv5Fd1QLhkQC6n3ob13ug1bTYtizkDpk2B0LxRiA8QUIjeTmfu6vZHoKJ7pXUgt73sHoXvSpGWjKQ6ZM01IIbJnXmwzXq4RdNYA4swdULgNy/s16000/youtube-logo.png" width="35px"/>
<button style="padding:5px; margin-bottom:2px"><a href="https://www.youtube.com/watch?v=zZXTgN7L1UU&list=PLR_5PTwg_uAS6C1cSMjU6oVhOcCX8Zu_4">
C++ STL Playlist</a></button>
<br/>
<img height="35px" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT9Spnddn1UerjXEbcWQUPhfDHLL9rppkmUp1x4JRPKsdTwmX0kI26Rd0-2qKlwGAAnbgiPz7dZMm5Rv5Fd1QLhkQC6n3ob13ug1bTYtizkDpk2B0LxRiA8QUIjeTmfu6vZHoKJ7pXUgt73sHoXvSpGWjKQ6ZM01IIbJnXmwzXq4RdNYA4swdULgNy/s16000/youtube-logo.png" width="35px"/>
<button style="padding:5px; margin-bottom:2px"><a href="https://youtu.be/ty9756v3kc8">
Learn Node.js </a></button>
<br/>
<img height="35px" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT9Spnddn1UerjXEbcWQUPhfDHLL9rppkmUp1x4JRPKsdTwmX0kI26Rd0-2qKlwGAAnbgiPz7dZMm5Rv5Fd1QLhkQC6n3ob13ug1bTYtizkDpk2B0LxRiA8QUIjeTmfu6vZHoKJ7pXUgt73sHoXvSpGWjKQ6ZM01IIbJnXmwzXq4RdNYA4swdULgNy/s16000/youtube-logo.png" width="35px"/>
<button style="padding:5px; margin-bottom:2px"><a href="https://youtu.be/exrIpdS3Crc">
Learn Data Science Full course</a></button>
<br/>
<img height="35px" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT9Spnddn1UerjXEbcWQUPhfDHLL9rppkmUp1x4JRPKsdTwmX0kI26Rd0-2qKlwGAAnbgiPz7dZMm5Rv5Fd1QLhkQC6n3ob13ug1bTYtizkDpk2B0LxRiA8QUIjeTmfu6vZHoKJ7pXUgt73sHoXvSpGWjKQ6ZM01IIbJnXmwzXq4RdNYA4swdULgNy/s16000/youtube-logo.png" width="35px"/>
<button style="padding:5px; margin-bottom:2px"><a href="https://youtu.be/Q8tDO2uSk5U">
Learn Computer Networking Full course</a></button>
<br/>
</br></br></br></br></br></br></br></br></ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<footer class="afooter" style="margin-left:20px">
<p>
<a href="/cookie-policy.html">Cookie policy</a> |
<a href="/privacy-policy.html">Privacy policy</a> |
<a href="/terms-of-use.html">Terms of use</a> |
<a href="/disclaimer.html">Disclaimer</a> |
<a href="/about-us.html">About Us</a>
</p>
<p>
© 2023 <a href="https://pythonread.github.io">https://pythonread.github.io</a>
</p>
</footer>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () { htmlTableOfContents() }); function htmlTableOfContents(documentRef) { var documentRef = documentRef || document; var toc = documentRef.getElementById("toc"); var headings = [].slice.call(documentRef.body.querySelectorAll('h2, h3')); headings.forEach(function (heading, index) { var ref = "toc" + index; if (heading.hasAttribute("id")) { ref = heading.getAttribute("id") } else { heading.setAttribute("id", ref) } var link = documentRef.createElement("a"); link.setAttribute("href", "#" + ref); link.textContent = heading.textContent; var div = documentRef.createElement("div"); div.setAttribute("class", heading.tagName.toLowerCase()); div.appendChild(link); toc.appendChild(div) }) } try { module.exports = htmlTableOfContents } catch (e) { } function isScrolledIntoView(el) { var rect = el.getBoundingClientRect(); var elemTop = rect.top; var elemBottom = rect.bottom; var isVisible = (elemTop >= -10) && (elemBottom <= (window.innerHeight)); return isVisible } function highlightMenu() { var headers = document.querySelectorAll("h2, h3"); for (var i = 0; i < headers.length; i += 1) { if (isScrolledIntoView(headers[i])) { var childDivs = document.getElementById('toc').getElementsByTagName('div'); var c = childDivs[i].classList; c.add("active"); for (var j = 0; j < i; j += 1) { var c = childDivs[j].classList; c.remove("active") } for (var j = i + 1; j < childDivs.length; j += 1) { var c = childDivs[j].classList; c.remove("active") } break } } } function highlightMenuLastItem() { var childDivs = document.getElementById('toc').getElementsByTagName('div'); for (var j = 0; j < childDivs.length - 1; j += 1) { var c = childDivs[j].classList; c.remove("active") } var c = childDivs[childDivs.length - 1].classList; c.add("active") } window.addEventListener("click", function (event) { highlightMenu() }); window.onscroll = function (e) { highlightMenu(); if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) { highlightMenuLastItem() } }; function mobileMenu() { var classes = document.getElementById('nav-toggle-icon').classList; if (classes.contains("active")) { classes.remove("active") } else { classes.add("active") } var classes = document.getElementById('menu-wrapper').classList; if (classes.contains("active")) { classes.remove("active") } else { classes.add("active") } }
</script>
<script type="text/javascript">
function changepy(){
document.getElementById('py1').className = 'tabbed-editor__node tabbed-editor__node--active';
document.getElementById('java1').className = 'tabbed-editor__node';
document.getElementById('c1').className = 'tabbed-editor__node';
document.getElementById('cpp1').className = 'tabbed-editor__node';
document.getElementById('python-code').className = 'code-editor__area code-editor__area--active';
document.getElementById('java-code').className = 'code-editor__area';
document.getElementById('c-code').className = 'code-editor__area';
document.getElementById('cpp-code').className = 'code-editor__area';
}
function changecpp(){
document.getElementById('py1').className = 'tabbed-editor__node';
document.getElementById('java1').className = 'tabbed-editor__node';
document.getElementById('c1').className = 'tabbed-editor__node';
document.getElementById('cpp1').className = 'tabbed-editor__node tabbed-editor__node--active';
document.getElementById('python-code').className = 'code-editor__area';
document.getElementById('java-code').className = 'code-editor__area';
document.getElementById('c-code').className = 'code-editor__area';
document.getElementById('cpp-code').className = 'code-editor__area code-editor__area--active';
}
function changejava(){
document.getElementById('py1').className = 'tabbed-editor__node';
document.getElementById('java1').className = 'tabbed-editor__node tabbed-editor__node--active';
document.getElementById('c1').className = 'tabbed-editor__node';
document.getElementById('cpp1').className = 'tabbed-editor__node';
document.getElementById('python-code').className = 'code-editor__area';
document.getElementById('java-code').className = 'code-editor__area code-editor__area--active';
document.getElementById('c-code').className = 'code-editor__area';