├── resources
├── data_structures
│ ├── graphs.md
│ ├── heaps.md
│ ├── sets.md
│ ├── stacks.md
│ ├── linked_list.md
│ ├── hash_maps.md
│ ├── arrays.md
│ ├── queues.md
│ ├── matrix.md
│ ├── binary_search_trees.md
│ └── binary_trees.md
└── algorithms
│ ├── sorting
│ ├── heapsort.md
│ ├── quicksort.md
│ ├── radix_sort.md
│ ├── bubble_sort.md
│ ├── bucket_sort.md
│ ├── insertion_sort.md
│ └── merge_sort.md
│ ├── graph_algorithms
│ ├── implementation.md
│ ├── depth_first_traversal.md
│ ├── dijkstra.md
│ ├── bellman_ford_algorithm.md
│ ├── breadth_first_traversal.md
│ └── floyd_warshall_algorithm.md
│ └── searching
│ ├── binary_search.md
│ └── linear_search.md
├── index.html
├── index.css
├── README.md
├── index.js
└── data.json
/resources/data_structures/graphs.md:
--------------------------------------------------------------------------------
1 | # Graphs
--------------------------------------------------------------------------------
/resources/data_structures/heaps.md:
--------------------------------------------------------------------------------
1 | # Heaps
--------------------------------------------------------------------------------
/resources/data_structures/sets.md:
--------------------------------------------------------------------------------
1 | # Sets
--------------------------------------------------------------------------------
/resources/data_structures/stacks.md:
--------------------------------------------------------------------------------
1 | # Stacks
--------------------------------------------------------------------------------
/resources/data_structures/linked_list.md:
--------------------------------------------------------------------------------
1 | # Linked Lists
--------------------------------------------------------------------------------
/resources/algorithms/sorting/heapsort.md:
--------------------------------------------------------------------------------
1 | # Sorting: Heapsort
--------------------------------------------------------------------------------
/resources/algorithms/sorting/quicksort.md:
--------------------------------------------------------------------------------
1 | # Sorting: Quicksort
--------------------------------------------------------------------------------
/resources/algorithms/sorting/radix_sort.md:
--------------------------------------------------------------------------------
1 | # Sorting: Radix Sort
--------------------------------------------------------------------------------
/resources/algorithms/sorting/bubble_sort.md:
--------------------------------------------------------------------------------
1 | # Sorting: Bubble Sort
--------------------------------------------------------------------------------
/resources/algorithms/sorting/bucket_sort.md:
--------------------------------------------------------------------------------
1 | # Sorting: Bucket Sort
--------------------------------------------------------------------------------
/resources/data_structures/hash_maps.md:
--------------------------------------------------------------------------------
1 | # Hash Maps / Dictionaries
--------------------------------------------------------------------------------
/resources/algorithms/sorting/insertion_sort.md:
--------------------------------------------------------------------------------
1 | # Sorting: Insertion Sort
--------------------------------------------------------------------------------
/resources/algorithms/graph_algorithms/implementation.md:
--------------------------------------------------------------------------------
1 | # Graphs: Implementation
--------------------------------------------------------------------------------
/resources/algorithms/searching/binary_search.md:
--------------------------------------------------------------------------------
1 | # Searching: Binary Search Algorithm
--------------------------------------------------------------------------------
/resources/algorithms/searching/linear_search.md:
--------------------------------------------------------------------------------
1 | # Searching: Linear Search Algorithm
--------------------------------------------------------------------------------
/resources/algorithms/graph_algorithms/depth_first_traversal.md:
--------------------------------------------------------------------------------
1 | # Graphs: Depth First Traversal
--------------------------------------------------------------------------------
/resources/algorithms/graph_algorithms/dijkstra.md:
--------------------------------------------------------------------------------
1 | # Graphs: Dijkstra's Shortest Path Algorithm
--------------------------------------------------------------------------------
/resources/algorithms/graph_algorithms/bellman_ford_algorithm.md:
--------------------------------------------------------------------------------
1 | # Graphs: Bellman Ford Algorithm
--------------------------------------------------------------------------------
/resources/algorithms/graph_algorithms/breadth_first_traversal.md:
--------------------------------------------------------------------------------
1 | # Graphs: Breadth First Traversal
--------------------------------------------------------------------------------
/resources/algorithms/graph_algorithms/floyd_warshall_algorithm.md:
--------------------------------------------------------------------------------
1 | # Graphs: Floyd Warshall Algorithm
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Coding Interview Prep
7 |
8 |
9 |
10 |
11 | Technical Interview Preparation Guide
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/index.css:
--------------------------------------------------------------------------------
1 | .node {
2 | cursor: pointer;
3 | }
4 |
5 | .node circle {
6 | fill: #fff;
7 | stroke: steelblue;
8 | stroke-width: 1.5px;
9 | }
10 |
11 | .node text {
12 | font: 11px sans-serif;
13 | font-family: monospace;
14 | }
15 |
16 | .link {
17 | fill: none;
18 | stroke: #ccc;
19 | stroke-width: 1.5px;
20 | }
21 |
22 | h1{
23 | color: darkblue;
24 | font-weight: normal;
25 | font-family: monospace;
26 | line-height: 1.1em;
27 | margin: 0.8cm 0 .5em 0;
28 | text-align: center;
29 | text-decoration: underline;
30 | }
31 |
32 | body{
33 | font-family: arial;
34 | font-size: 80%;
35 | line-height: 1.2em;
36 | width: 100%;
37 | margin: 0;
38 | margin-left: 20px;
39 | background: #deefea;
40 | }
41 |
42 | footer{
43 | font-family: arial;
44 | text-align: center;
45 |
46 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Interview Prep Guide
2 | This repository contains the development for a preparation tool
3 | to interviewees and students in the field of Computer Science,
4 | or otherwise similar fields.
5 |
6 | The goal of the webpage is to offer a reference tool which will
7 | point users in the right direction for the subject they need
8 | to brush up on--without wasting a lot of time searching for
9 | the right learning resources on their own.
10 |
11 | The material covered consists of general computer science knowledge and can be implemented in any programming language
12 | of your choice. However, we focus on three popular technical interview programming languages namely **C++**, **Java**, and **Python**.
13 |
14 |
15 | ## Todo:
16 | * [X] Implement url open action for clicking an end node
17 | * [X] Create inital resource document structure
18 | * [ ] Fill out the resource documents
19 | * [ ] Algorithms documents
20 | * [ ] Data Structures documents
21 |
--------------------------------------------------------------------------------
/resources/algorithms/sorting/merge_sort.md:
--------------------------------------------------------------------------------
1 | # Sorting: Merge Sort
2 |
3 | - Merge sort is a recursive algorithm that continually splits a list in half.
4 | - If the list is empty or has one item, it is sorted by definition (the base case).
5 | - If the list has more than one item, we split the list and recursively invoke a merge sort on both halves.
6 | - Once the two halves are sorted, the fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list.
7 |
8 | ```python
9 | def mergeSort(alist):
10 |
11 | if len (alist) > 1:
12 |
13 | mid = len(alist) // 2
14 | lefthalf = alist[:mid]
15 | righthalf = alis[mid:]
16 |
17 | mergeSort(lefthalf)
18 | mergeSort(righthalf)
19 |
20 | i = 0
21 | j = 0
22 | k = 0
23 |
24 | while i < len(lefthalf) and j < len(righthalf):
25 | if lefthalf[i] < righthalf[j]:
26 | alist[k] = lefthalf[i]
27 | i += 1
28 |
29 | else:
30 | alist[k] = righthalf[j]
31 | j += 1
32 |
33 | k += 1
34 |
35 | while i < len(lefthalf):
36 | alist[k] = lefthalf[i]
37 | i += 1
38 | k += 1
39 |
40 | while j < len(righthalf):
41 | alist[k] = righthalf[j]
42 | j += 1
43 | k += 1
44 |
45 |
46 | ```
47 |
--------------------------------------------------------------------------------
/resources/data_structures/arrays.md:
--------------------------------------------------------------------------------
1 | # Arrays / Lists
2 |
3 | #### 1. Find the number of Occurrences of a number in an array
4 |
5 | Given a sorted(ascending order) array of integers, find out the number of Occurrences of a number in that array
6 |
7 |
8 | #### Python Implementation
9 |
10 | ```python
11 | def findOccurences(alist, x):
12 |
13 | count = 0
14 | startIndex = findFirstOccurrence(alist, x, 0, len(alist) - 1)
15 | if startIndex < 0:
16 | return -1
17 |
18 | lastIndex = findLastOccurrence(alist, x, 0, len(alist) - 1)
19 | count = lastIndex - startIndex + 1
20 |
21 | return count
22 |
23 |
24 | def findFirstOccurrence(alist, x, start, end):
25 | if end >= start:
26 | mid = (start+end)/2
27 |
28 | if ( mid == 0 or alist[mid-1] < x ) and (alist[mid] == x):
29 | return mid
30 |
31 | elif alist[mid] < x:
32 | return findFirstOccurrence(alist, x, mid+1, end)
33 |
34 | else:
35 | return findFirstOccurrence(alist, x, start, mid-1)
36 |
37 | else:
38 | return -1
39 |
40 |
41 | def findLastOccurrence(alist, x, start, end):
42 | if end >= start:
43 | mid = (start + end)/ 2
44 |
45 | if (mid == 0 or alist[mid+1] > x) and (alist[mid] == x):
46 | return mid
47 |
48 | elif alist[mid] > x:
49 | return findLastOccurrence(alist, x, start, mid-1)
50 |
51 | else:
52 | return findLastOccurrence(alist, x, mid+1, end)
53 |
54 | else:
55 | return -1
56 |
57 |
58 | Array = [1,2,2,2,2,2,2,2,3,4,5,5,6]
59 | x = 2
60 |
61 | print "No of Occurrence is:", findOccurences(Array, x)
62 | ```
63 | The output is : 7
64 | Time Complexity is O(log n)
65 |
66 | _____________________________________________________________________
67 |
--------------------------------------------------------------------------------
/resources/data_structures/queues.md:
--------------------------------------------------------------------------------
1 | # Queues
2 |
3 | #### 1. Implement Queue Using Stacks
4 |
5 | Algorithm:
6 | - Take 2 stacks, stack1 and stack2
7 | - Stack1 will be used as the back of the Queues
8 | - Stack2 will be used as Front of the Queue
9 | - Enqueue() operation will be done on Stack1
10 | - Peek() and Dequeue() operations will be done on Stack2
11 | - when Peek() and Dequeue() are called, check if Stack2 is empty, if yes then move all the elements from Stack1 and push them into Stack2.
12 |
13 | #### Python Implementation
14 | ```python
15 | class QueueUsingStacks():
16 | def __init__(self):
17 | self.stack1 = []
18 | self.stack2 = []
19 |
20 | # Push into stack1
21 | def Enqueue(self, item):
22 | self.stack1.append(item)
23 |
24 | def peek(self):
25 | if len(self.stack2) == 0:
26 | self.moveItems(self.stack1, self.stack2)
27 | return self.stack2[-1] # return the top element in stack2
28 |
29 | def Dequeue(self):
30 | if len(self.stack2) == 0:
31 | self.moveItems(self.stack1, self.stack2)
32 | return self.stack2.pop() # Return the top element in stack2 and remove it from stack2
33 |
34 | def moveItems(self, stack1, stack2):
35 | length = len(self.stack1)
36 | while(length != 0):
37 | # Move all the elements from stack1 to stack2
38 | self.stack2.append(self.stack1.pop())
39 |
40 | length -= 1
41 |
42 |
43 | s = QueueUsingStacks()
44 | s.Enqueue(10)
45 | s.Enqueue(20)
46 | print(s.peek())
47 | print(s.Dequeue())
48 | print(s.Dequeue())
49 |
50 |
51 |
52 | ```
53 |
54 | #### Java Implementation
55 | ```java
56 | import java.util.Stack;
57 |
58 | public class QueueUsingStacks {
59 |
60 | Stack stack1 = new Stack<>(); //act as back of the Queue
61 | Stack stack2 = new Stack<>(); // act as the front of the Queue
62 |
63 | public void Enqueue(int x) { // push into stack 1
64 | stack1.push(x);
65 | }
66 |
67 | public int peek() {
68 | if (stack2.isEmpty()) {
69 | moveItems(stack1, stack2);
70 | }
71 | return stack2.peek(); // return the top element in stack2
72 | }
73 |
74 | public int Dequeue() {
75 | if (stack2.isEmpty()) {
76 | moveItems(stack1, stack2);
77 | }
78 | return stack2.pop(); // return the top element in stack2
79 | }
80 |
81 | public void moveItems(Stack s1, Stack s2) {
82 | while (!stack1.isEmpty()) {
83 | s2.push(s1.pop()); // move all the elements from stack 1 to stack 2
84 | }
85 | }
86 |
87 |
88 | }
89 | ```
90 |
--------------------------------------------------------------------------------
/resources/data_structures/matrix.md:
--------------------------------------------------------------------------------
1 | # Matrices / 2D Arrays
2 |
3 | #### 1. Search in a row wise and column wise sorted matrix
4 |
5 | Given an n x n matrix, where every row and column is sorted in increasing order.
6 | Given a number x, how to decide whether this x is in the matrix.
7 | The designed algorithm should have linear time complexity.
8 |
9 |
10 | ```python
11 | '''Searches the element x in mat[][]. If the element is found,
12 | then prints its position and returns true, otherwise prints
13 | "not found" and returns false'''
14 | def search(a, element):
15 |
16 | rows = len(a)
17 | columns = len(a[0])
18 |
19 | # set indexes for top right element
20 | i = 0
21 | j = columns - 1
22 |
23 | while( i < rows and j >= 0):
24 | # return its position if they are equal
25 | if a[i][j] == element:
26 | print 'found at', i, j
27 | return
28 |
29 | # move to the left if the element is less than a[i][j]
30 | if a[i][j] > element:
31 | j -= 1
32 |
33 | # move down if the element is greater than a[i][j]
34 | else:
35 | i += 1
36 |
37 | print 'not found'
38 |
39 |
40 | a = [[1,2,3],
41 | [4,5,6],
42 | [7,8,9]]
43 | search(a, 5)
44 | ```
45 |
46 | __________________________________________________________________________________
47 |
48 | #### 2. A Boolean Matrix
49 |
50 | Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell
51 | mat[i][j]is 1 (or true) then make all the cells of ith row and jth column as 1.
52 |
53 | ```python
54 | def modifyMatrix(a):
55 |
56 | rows = len(a) - 1
57 | cols = len(a[0]) - 1
58 |
59 |
60 | rowFlag = False
61 | colFlag = False
62 |
63 | # Scan the first row and set rowFlag to indicate whether we need to set all 1s in the first row or not
64 |
65 | for i in range(cols + 1):
66 | if a[0][i] == 1:
67 | rowFlag = True
68 | break
69 |
70 |
71 |
72 | # Scan the first col and set colFlag
73 |
74 | for i in range(rows + 1):
75 | if a[i][0] == 1:
76 | colFlag = True
77 | break
78 |
79 |
80 | """Use first row and first column as the auxiliary arrays row[] and col[] respectively,
81 | consider the matrix as submatrix starting from
82 | econd row and second column and apply method 1"""
83 | for i in range(1, rows + 1):
84 |
85 | for j in range(1, cols + 1):
86 |
87 | if a[i][j]:
88 |
89 | a[i][0] = 1
90 | a[0][j] = 1
91 |
92 |
93 | # Finally, using rowFlag and colFlag, update first row and first column if needed
94 | for i in range(1, rows+1):
95 |
96 | for j in range(1, cols+1):
97 |
98 | if a[i][0] or a[0][j]:
99 | a[i][j] = 1
100 |
101 |
102 | if rowFlag:
103 | for i in range(rows+1):
104 | a[i][0]=1
105 |
106 | if colFlag:
107 | for i in range(cols+1):
108 | a[0][i]=1
109 |
110 | return a
111 |
112 |
113 | a = [[0,0,0],
114 | [0,0,1]]
115 | print(modifyMatrix(a))
116 |
117 |
118 | ```
119 |
120 | _______________________________
121 |
122 |
123 | #### 3. Print all Diagonals of a given matrix
124 |
125 |
126 | ```python
127 | """
128 | ex: a = [[1,2,3,4],
129 | [5,6,7,8],
130 | [9,10,11,12],
131 | [13,14,15,16]]
132 |
133 |
134 | answer is:
135 | 1
136 | 5 2
137 | 9 6 3
138 | 13 10 7 4
139 | 14 11 8
140 | 15 12
141 | 16
142 | """
143 |
144 |
145 | def PrintDiagonals(alist):
146 |
147 | # Print first half
148 | row = 0
149 |
150 | while(row < len(alist)):
151 |
152 | col = 0
153 | rowTemp = row
154 |
155 | while (rowTemp >= 0):
156 | print alist[rowTemp][col],
157 | rowTemp -= 1
158 | col += 1
159 |
160 | print""
161 | row += 1
162 |
163 | # Print second half
164 | col = 1
165 |
166 | while(col < len(alist[0])):
167 | colTemp = col
168 | row = len(alist) - 1
169 |
170 | while(colTemp <= len(alist[0])- 1):
171 | print alist[row][colTemp],
172 |
173 | row -= 1
174 | colTemp += 1
175 |
176 | print""
177 | col += 1
178 |
179 |
180 |
181 | a = [[1,2,3,4],
182 | [5,6,7,8],
183 | [9,10,11,12],
184 | [13,14,15,16]]
185 |
186 |
187 | PrintDiagonals(a)
188 | ```
189 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 |
2 | // Helper variables
3 | var closedNodeColor = "steelblue";
4 | var openedNodeColor = "#def";
5 | var nodeStartRadius = 15;
6 | var nodeEndRadius = 10;
7 | var depthSeparation = 160;
8 |
9 |
10 |
11 | var margin = {top: 20, right: 120, bottom: 20, left: 120},
12 | width = 960 - margin.right - margin.left,
13 | height = 800 - margin.top - margin.bottom;
14 |
15 | var i = 0,
16 | duration = 750,
17 | root;
18 |
19 | // Create a D3 tree.
20 | // Keep in mind, ._children is the list of hidden child nodes
21 | // and .children is the list of shown child nodes
22 | var tree = d3.layout.tree()
23 | .size([height, width]);
24 |
25 | // Create a D3 diagonal function for drawing the links
26 | var diagonal = d3.svg.diagonal()
27 | .projection(function(d) { return [d.y, d.x]; });
28 |
29 | // Grap the svg canvas and apply attributes to it.
30 | // Then add a graphics element to it.
31 | var svg = d3.select("body").append("svg")
32 | .attr("width", width + margin.right + margin.left)
33 | .attr("height", height + margin.top + margin.bottom)
34 | .append("g")
35 | .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
36 |
37 | // Load the json data in
38 | d3.json("data.json", function(error, CS_Foundamentals) {
39 | if (error) throw error;
40 |
41 | // Set the root node and its position
42 | root = CS_Foundamentals;
43 | root.x0 = height / 2;
44 | root.y0 = 0;
45 |
46 | // Collapse function for collapsing subtrees of a node
47 | function collapse(d) {
48 | // If the node has open children
49 | if (d.children) {
50 | // "hide" the shown children by
51 | // setting _children to children
52 | d._children = d.children;
53 |
54 | // Collapse each node in _children list
55 | d._children.forEach(collapse);
56 |
57 | // And set the shown children list to null
58 | d.children = null;
59 | }
60 | }
61 |
62 | // Collapse each child node of root and update it
63 | root.children.forEach(collapse);
64 | update(root);
65 | });
66 |
67 | d3.select(self.frameElement).style("height", "800px");
68 |
69 |
70 |
71 | function update(source) {
72 |
73 | // Compute the new tree layout.
74 | var nodes = tree.nodes(root).reverse(),
75 | links = tree.links(nodes);
76 |
77 | // Normalize for fixed-depth.
78 | nodes.forEach(function(d) { d.y = d.depth * depthSeparation; });
79 |
80 | // Update the nodes…
81 | var node = svg.selectAll("g.node")
82 | .data(nodes, function(d) { return d.id || (d.id = ++i); });
83 |
84 | // Enter new nodes at the parent's previous position.
85 | var nodeEnter = node.enter().append("g")
86 | .attr("class", "node")
87 | .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
88 | .on("click", click);
89 |
90 | // Create the svg circle for the newly entered nodes
91 | nodeEnter.append("circle")
92 | .attr("r", nodeStartRadius)
93 | .style("fill", function(d) { return d._children ? closedNodeColor : openedNodeColor; });
94 |
95 | // Add the text to each newly added node
96 | nodeEnter.append("text")
97 | .attr("x", function(d) { return d.children || d._children ? -nodeStartRadius : nodeStartRadius; })
98 | .attr("dy", ".35em")
99 | .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
100 | .text(function(d) { return d.name; })
101 | .style("fill-opacity", nodeStartRadius);
102 |
103 | // Transition nodes to their new position.
104 | var nodeUpdate = node.transition()
105 | .duration(duration)
106 | .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
107 |
108 | // Set the radius and fill for the circles on the nodes
109 | nodeUpdate.select("circle")
110 | .attr("r", nodeEndRadius)
111 | .style("fill", function(d) { return d._children ? closedNodeColor : openedNodeColor; });
112 |
113 | // Show the text for the updated nodes
114 | nodeUpdate.select("text")
115 | .style("fill-opacity", 1);
116 |
117 | // Transition exiting nodes to the parent's new position.
118 | var nodeExit = node.exit().transition()
119 | .duration(duration)
120 | .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
121 | .remove();
122 |
123 | // Set the radius for the exit node(s)
124 | nodeExit.select("circle")
125 | .attr("r", nodeStartRadius);
126 |
127 | // And show the text
128 | nodeExit.select("text")
129 | .style("fill-opacity", 1);
130 |
131 | // Update the links…
132 | var link = svg.selectAll("path.link")
133 | .data(links, function(d) { return d.target.id; });
134 |
135 | // Enter any new links at the parent's previous position.
136 | link.enter().insert("path", "g")
137 | .attr("class", "link")
138 | .attr("d", function(d) {
139 | var o = {x: source.x0, y: source.y0};
140 | return diagonal({source: o, target: o});
141 | });
142 |
143 | // Transition links to their new position.
144 | link.transition()
145 | .duration(duration)
146 | .attr("d", diagonal);
147 |
148 | // Transition exiting nodes to the parent's new position.
149 | link.exit().transition()
150 | .duration(duration)
151 | .attr("d", function(d) {
152 | var o = {x: source.x, y: source.y};
153 | return diagonal({source: o, target: o});
154 | })
155 | .remove();
156 |
157 | // Stash the old positions for transition.
158 | nodes.forEach(function(d) {
159 | d.x0 = d.x;
160 | d.y0 = d.y;
161 | });
162 | }
163 |
164 |
165 |
166 | // Toggle children on click.
167 | function click(d) {
168 | // If clicked on an opened node (close that node)
169 | if (d.children) {
170 | // Set hidden children to shown children ("closing" the subtree)
171 | d._children = d.children;
172 |
173 | // Set shown children list to null
174 | d.children = null;
175 | } else if (d._children == null) {
176 | // If clicked on end node, open the node's url in a new tab
177 | window.open(d.url, '_blank');
178 | } else { // otherwise, clicked on an unopened node so open it
179 | // Set shown children to hidden children ("opening" the subtree)
180 | d.children = d._children;
181 |
182 | // Set hidden children list to null
183 | d._children = null;
184 | }
185 |
186 | update(d);
187 | }
188 |
189 |
--------------------------------------------------------------------------------
/data.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "CS_Fundamentals",
3 | "children": [
4 |
5 | {
6 | "name": "Algorithms",
7 | "children": [{
8 | "name": "Sorting",
9 | "children": [{
10 | "name": "Merge Sort",
11 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/sorting/merge_sort.md"
12 | }, {
13 | "name": "Quicksort",
14 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/sorting/quicksort.md"
15 | }, {
16 | "name": "Heapsort",
17 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/sorting/heapsort.md"
18 | }, {
19 | "name": "Bucket sort",
20 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/sorting/bucket_sort.md"
21 | }, {
22 | "name": "Radix sort",
23 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/sorting/radix_sort.md"
24 | }, {
25 | "name": "Insertion sort",
26 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/sorting/insertion_sort.md"
27 | }, {
28 | "name": "Bubble sort",
29 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/sorting/bubble_sort.md"
30 | }]
31 | }, {
32 | "name": "Searching",
33 | "children": [{
34 | "name": "Linear Search",
35 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/searching/linear_search.md"
36 | }, {
37 | "name": "Binary Search",
38 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/searching/binary_search.md"
39 | }]
40 | }, {
41 | "name": "Graph Algorithms",
42 | "children": [{
43 | "name": "Implementation",
44 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/graph_algorithms/implementation.md"
45 | }, {
46 | "name": "Breadth First Traversal",
47 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/graph_algorithms/breadth_first_traversal.md"
48 | }, {
49 | "name": "Depth First Traversal",
50 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/graph_algorithms/depth_first_traversal.md"
51 | }, {
52 | "name": "Dijkstra’s shortest path ",
53 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/graph_algorithms/dijkstra.md"
54 | }, {
55 | "name": "Bellman–Ford Algorithm",
56 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/graph_algorithms/bellman_ford_algorithm.md"
57 | }, {
58 | "name": "Floyd Warshall Algorithm",
59 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/graph_algorithms/floyd_warshall_algorithm.md"
60 | }]
61 | },
62 |
63 | {
64 | "name": "Pattern Searching",
65 | "children": [{
66 | "name": "AspectRatioBanker"
67 |
68 | }]
69 | }, {
70 | "name": "Backtracking",
71 | "children": [{
72 | "name": "AspectRatioBanker"
73 |
74 | }]
75 | }, {
76 | "name": "Divide and Conquer",
77 | "children": [{
78 | "name": "AspectRatioBanker"
79 |
80 | }]
81 | }, {
82 | "name": "String Algorithms",
83 | "children": [{
84 | "name": "AspectRatioBanker"
85 |
86 | }]
87 | }, {
88 | "name": "Bit Manipulation",
89 | "children": [{
90 | "name": "AspectRatioBanker"
91 |
92 | }]
93 | }, {
94 | "name": "Geometric Algorithms",
95 | "children": [{
96 | "name": "AspectRatioBanker"
97 |
98 | }]
99 | }, {
100 | "name": "Mathematical Algorithms",
101 | "children": [{
102 | "name": "AspectRatioBanker"
103 |
104 | }]
105 | }
106 | ]
107 | },
108 |
109 |
110 | {
111 | "name": "Data Structures",
112 | "children": [{
113 | "name": "Arrays/Lists",
114 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/arrays.md"
115 |
116 | }, {
117 | "name": "Matrix",
118 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/matrix.md"
119 | }, {
120 | "name": "Linked List",
121 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/linked_list.md"
122 | }, {
123 | "name": "Maps/ HashMaps/ Dictionaries",
124 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/hash_maps.md"
125 |
126 | }, {
127 | "name": "Binary Trees",
128 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/binary_trees.md"
129 | }, {
130 | "name": "Binary Search Trees",
131 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/binary_search_trees.md"
132 | }, {
133 | "name": "Stacks",
134 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/stacks.md"
135 |
136 |
137 | }, {
138 | "name": "Queues",
139 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/queues.md"
140 |
141 | }, {
142 | "name": "Sets",
143 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/sets.md"
144 |
145 | }, {
146 | "name": "Graphs",
147 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/graphs.md"
148 |
149 | }, {
150 | "name": "Heap",
151 | "url": "https://github.com/andersy005/InterviewPrep-Guide/blob/master/resources/data_structures/heaps.md"
152 | }
153 |
154 | ]
155 | }
156 |
157 | ]
158 | }
159 |
--------------------------------------------------------------------------------
/resources/data_structures/binary_search_trees.md:
--------------------------------------------------------------------------------
1 | # Binary Search Trees
2 |
3 | In BST, all the left subtree elements should be less than root data and all
4 | the right subtree elements should be greater than root data. This is called
5 | **binary search tree** property.
6 |
7 | The time complexity for the following operations is O(n) for worst case scenario (skew Trees)
8 |
9 |
10 | #### 1. Finding an Element in BST
11 | Start with the root moving left or right using the BST property. If the data we
12 | are searching is same as nodes data then we return current node.
13 |
14 | If data is greater than the root data, we recur for the right subtree of root node.
15 | Otherwise we recur for left subtree.
16 |
17 | ```python
18 |
19 | # A class that represents an individual node in a BST
20 | class Node:
21 | def __init__(self, data):
22 | self.left = None
23 | self.right = None
24 | self.val = data
25 |
26 |
27 |
28 | def search(root, data):
29 |
30 | if root is None or root.val == data:
31 | return root
32 |
33 | # data is greater than root's data
34 | if root.val < data:
35 | return search(root.right, data)
36 |
37 | # data is smaller than root's data
38 | return search(root.left, data)
39 |
40 |
41 | ```
42 |
43 | ______________________________________________________________________________
44 |
45 |
46 | #### 2. Insertion
47 |
48 | ```python
49 | # A function to insert a new node with the given data
50 | def insert(root, node):
51 | if root is None:
52 | root = node
53 |
54 |
55 | else:
56 | if root.val < node.val:
57 | if root.right is None:
58 | root.right = node
59 |
60 | else:
61 | insert(root.right, node)
62 |
63 | else:
64 | if root.left is None:
65 | root.left = node
66 |
67 | else:
68 | insert(root.left, node)
69 |
70 |
71 | ```
72 |
73 | ___________________________________________________________________________
74 |
75 | #### 3. Deletion
76 | - 1) **Node to be deleted is leaf**: Simply remove from the tree.
77 | - 2) **Node to be deleted has only one child**: Copy the child to the node and delete the child
78 | - 3) **Node to be deleted has two children:** Find inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor. Note that inorder predecessor can also be used.
79 |
80 |
81 | The important thing to note is, inorder successor is needed only when right child is not empty. In this particular case, inorder successor can be obtained by finding the minimum value in right child of the node.
82 |
83 | ```python
84 | # Given a binary search tree and a key,
85 | # delete the key and returns the new root
86 | ```
87 |
88 |
89 | ___________________________________________________________________________
90 |
91 |
92 | #### 4. Find the minimum element
93 | ```python
94 |
95 | def findMin(root):
96 |
97 | if root is None:
98 | return None
99 |
100 | current = root
101 |
102 | while current.left:
103 | current = current.left
104 |
105 | return current
106 | ```
107 |
108 |
109 | ____________________________________________________________________________
110 |
111 | #### 5. Find the maximum element
112 |
113 | ```python
114 |
115 | def findMax(root):
116 |
117 | if root is None:
118 | return None
119 |
120 | current = root
121 |
122 | while current.right:
123 | current = current.right
124 |
125 | return current
126 | ```
127 |
128 |
129 | ______________________________________________________________________________
130 |
131 | #### 6. Find the lowest common ancestor (LCA)
132 | Given two pointers to two nodes in a BST, find the lowest common ancestor. Assume
133 | that both values already exist in the tree.
134 |
135 |
136 | In a given binary tree, The lowest common ancestor of two nodes n1 and n2 will be a node X such that node X will be the lowest node who has n1 and n2 as its descendants.
137 |
138 | Approach:
139 | - Start with the root(Traverse the BST in preorder)
140 | - if root.data > n1.data and root.data > n2.data then lowest common ancestor will be in left subtree
141 | - if root.data < n1.data and root.data < n2.data then lowest common ancestor will be in right subtree
142 | - If step 2 and step 3 are false then we are at the root which is the lowest common ancestor, return it
143 |
144 | Note: **n1.data < LCA.data < n2.data** or **n2.data < LCA.data < n1.data**
145 |
146 | #### Python Implementation
147 | ```python
148 | class Node():
149 | def __init__(self, data):
150 | self.data = data
151 | self.left = None
152 | self.right = None
153 |
154 |
155 | def LCAIterative(root, n1, n2):
156 | while(root is not None):
157 |
158 | # If root > n1 and root > n2 then lowest common ancestor will be in left subtree
159 |
160 | if root.data > n1.data and root.data > n2.data:
161 | root = root.left
162 |
163 | # if root < n1 and root < n2 then lowest common ancestor will be in right subtree
164 |
165 | elif root.data < n1.data and root.data < n1.data:
166 | root = root.right
167 |
168 | # If I am here that means I am at the root which is lowest common ancestor
169 |
170 | else:
171 | return root
172 |
173 |
174 |
175 |
176 |
177 | def LCARecursive(root, n1, n2):
178 | if root is None:
179 | return None
180 |
181 | # If root > n1 and root > n2 then lowest common ancestor will be in left subtree
182 |
183 | if root.data > n1.data and root.data > n1.data:
184 | return LCARecursive(root.left, n1, n2)
185 |
186 | # if root < n1 and root < n2 then lowest common ancestor will be in right subtree
187 |
188 | elif root.data < n1.data and root.data < n2.data:
189 | return LCARecursive(root.right, n1, n2)
190 |
191 |
192 | # If I am here that means I am at the root which is lowest common ancestor
193 | return root
194 |
195 |
196 | ```
197 |
198 |
199 | #### Java Implementation
200 | ```java
201 | public class LowestCommonAncestorBST {
202 | public Node LCA(Node root, Node n1, Node n2) {
203 | if (root == null) {
204 | return null;
205 | }
206 | // If root>n1 and root>n2 then lowest common ancestor will be in left
207 | // subtree.
208 | if (root.data > n1.data && root.data > n2.data) {
209 | return LCA(root.left, n1, n2);
210 |
211 | }
212 | // If rootn1 and root>n2 then lowest common ancestor will be in
225 | // left
226 | // subtree.
227 | if (root.data > n1.data && root.data > n2.data) {
228 | root = root.left;
229 | }
230 | // If rootdata = 1;
100 | root->left = new node; root->left.data = 2;
101 | root->right = new node; root->right.data = 3;
102 |
103 | return 0;
104 | }
105 | ```
106 | ______________________________________________________________________
107 | #### 2. Tree Traversals
108 |
109 | *Depth First Traversals*:
110 | - Inorder
111 | - Preorder
112 | - Postorder
113 |
114 |
115 | **Inorder Traversal:**
116 |
117 | Algorithm Inorder(tree)
118 | 1. Traverse the left subtree, i.e., call Inorder(left-subtree)
119 | 2. Visit the root.
120 | 3. Traverse the right subtree, i.e., call Inorder(right-subtree)
121 |
122 | ```python
123 | # (Python)
124 | # A python function to do Inorder Traversal
125 | def Inorder(root):
126 | if root is None:
127 | return None
128 |
129 | # First recur on left child
130 | Inorder(root.left)
131 |
132 | # then print the data of node
133 | print(root.val)
134 |
135 | # now recur on right child
136 | Inorder(root.right)
137 | ```
138 |
139 | ```java
140 | // (Java)
141 | public void inOrder(Node root)
142 | {
143 | if (root == null)
144 | return null;
145 |
146 | // Traverse the left subtree
147 | inOrder(root.left);
148 |
149 | // Print the node data
150 | System.out.println(root.data);
151 |
152 | // Traverse the right subtree
153 | inOrder(root.right);
154 | }
155 | ```
156 |
157 | ```C++
158 | #include
159 |
160 | // (C++)
161 | void inOrder(node* root)
162 | {
163 | if (root == NULL)
164 | return NULL;
165 |
166 | // Traverse the left subtree
167 | inOrder(root->left);
168 |
169 | // Print the node data
170 | std::cout << root->data << "\n";
171 |
172 | // Traverse the right subtree
173 | inOrder(root->right);
174 | }
175 | ```
176 |
177 | Uses of Inorder
178 |
179 | In 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 itraversal s reversed, can be used.
180 |
181 |
182 | **Preorder Traversal:**
183 |
184 | Algorithm Preorder(tree)
185 | 1. Visit the root.
186 | 2. Traverse the left subtree, i.e., call Preorder(left-subtree)
187 | 3. Traverse the right subtree, i.e., call Preorder(right-subtree)
188 |
189 | ```python
190 | # A python function to do Preorder tree traversal
191 | def Preorder(root):
192 | if root is None:
193 | return None
194 |
195 | # First print the data of node
196 | print(root.val)
197 |
198 | # Then recur on left child
199 | Preorder(root.left)
200 |
201 | # now recur on right child
202 | Preorder(root.right)
203 | ```
204 |
205 | ```java
206 | // (Java)
207 | public void preorder(Node root)
208 | {
209 | if (root == null)
210 | return null;
211 |
212 | // Print the node's data
213 | System.out.println(root.data);
214 |
215 | // Traverse the left subtree
216 | preorder(root.left);
217 |
218 | // Traverse the right subtree
219 | preorder(root.right);
220 | }
221 | ```
222 |
223 | ```c++
224 | #include
225 |
226 | // (C++)
227 | void preorder(node* root)
228 | {
229 | // Print the node's data
230 | std::cout << root->data << "\n";
231 |
232 | // Traverse the left subtree
233 | preorder(root->left);
234 |
235 | // Traverse the right subtree
236 | preorder(root->right);
237 | }
238 | ```
239 |
240 | Uses of Preorder
241 |
242 | - Preorder traversal is used to create a copy of the tree.
243 | - Preorder traversal is also used to get prefix expression on of an expression tree. Please see [Polish_notation](http://en.wikipedia.org/wiki/Polish_notation) to know why prefix expressions are useful.
244 |
245 | **Postorder Traversal:**
246 |
247 | Algorithm Postorder(tree)
248 | 1. Traverse the left subtree, i.e., call Postorder(left-subtree)
249 | 2. Traverse the right subtree, i.e., call Postorder(right-subtree)
250 | 3. Visit the root.
251 |
252 | ```python
253 | # A python function to do Postorder tree traversal
254 | def Postorder(root):
255 | if root is None:
256 | return None
257 |
258 | # First recur on left child
259 | Postorder(root.left)
260 |
261 | # Then recur on Right child
262 | Postorder(root.right)
263 |
264 | # Finally print the data of the node
265 | print(root.val)
266 | ```
267 |
268 | ```java
269 | // (Java)
270 | public void postorder(Node root)
271 | {
272 | // Traverse the left subtree
273 | postorder(root.left);
274 |
275 | // Traverse the right subtree
276 | postorder(root.right);
277 |
278 | // Print the node's data
279 | System.out.println(root.data);
280 | }
281 | ```
282 |
283 | ```c++
284 | #include
285 |
286 | // (C++)
287 | void postorder(node* root)
288 | {
289 | // Traverse the left subtree
290 | postorder(root->left);
291 |
292 | // Traverse the right subtree
293 | postorder(root->right);
294 |
295 | // Print the node's data
296 | std::cout << node->data << "\n";
297 | }
298 | ```
299 |
300 | Uses of Postorder
301 |
302 | - Postorder traversal is used to delete the tree.
303 | - Postorder traversal is also useful to get the postfix expression of an expression tree. Please see [Reverse_Polish_notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation) to for the usage of postfix expression.
304 |
305 | *Breadth First or Level Order Traversal*
306 |
307 | For each node, first the node is visited and then it’s child nodes are put in a FIFO queue.
308 |
309 | printLevelorder(tree)
310 |
311 | 1. Create an empty queue q
312 | 2. temp_node = root /*start from root*/
313 | 3. Loop while temp_node is not NULL
314 | - print temp_node->data.
315 | - Enqueue temp_node’s children (first left then right children) to q
316 | - Dequeue a node from q and assign it’s value to temp_node
317 |
318 |
319 | ```python
320 | def printLevelorder(root):
321 | # Base Case
322 | if root is None:
323 | return
324 |
325 | # Create an empty queue for level order traversal
326 | queue = []
327 |
328 | # Enqueue root
329 | queue.append(root)
330 |
331 | while (len(queue) > 0):
332 | # Print front of queue and remove it form queue
333 | print(queue[0].data)
334 |
335 | node = queue.pop(0)
336 |
337 | # Enqueue left child
338 | if node.left is not None:
339 | queue.append(node.left)
340 |
341 | # Enqueue right child
342 | if node.right is not None:
343 | queue.append(node.right):
344 | ```
345 |
346 | ```java
347 | // (Java)
348 | import java.util.*;
349 |
350 | //...
351 |
352 | public void printLevelOrder(Node root)
353 | {
354 | // Create an empty list/queue for level order traversal
355 | ArrayDeque queue = new ArrayDeque();
356 |
357 | // Enqueue the root
358 | queue.addLast(root);
359 |
360 | while (!queue.isEmpty())
361 | {
362 | // Print the front of the queue and remove it from queue
363 | System.out.println(queue.peekLast().data);
364 | Node node = queue.pop();
365 |
366 | // Enqueue left child
367 | if (node.left != null)
368 | queue.addLast(node.left);
369 |
370 | // Enqueue right child
371 | if (node.right != null)
372 | queue.addLast(node.right);
373 | }
374 | }
375 | ```
376 |
377 | ```c++
378 | // (C++)
379 | #include
380 | #include
381 |
382 | //...
383 |
384 | void printLevelOrder(node* root)
385 | {
386 | // Create an empty queue for level order traversal
387 | deque q;
388 |
389 | // Enqueue the root
390 | q.push_back(root);
391 |
392 | while (!q.empty())
393 | {
394 | // Print the front of the queue and remove it from the queue
395 | node *n = q.front();
396 | q.pop_front();
397 |
398 | // Enqueue left child
399 | if (n->left != NULL)
400 | q.push_back(n->left);
401 |
402 | // Enqueue right child
403 | if (n->right != NULL)
404 | q.push_back(n->right);
405 | }
406 | }
407 | ```
408 |
409 |
410 | **Time Complexity:** O(n) where n is number of nodes in the binary tree
411 |
412 | [References](http://en.wikipedia.org/wiki/Breadth-first_traversal)
413 |
414 |
415 | _________________________________________________________
416 |
417 | #### 3. Determine if Two Trees are identical
418 |
419 | Two trees are identical when they have same data and arrangement of data is also same.
420 |
421 | To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.
422 |
423 | ```python
424 | # (Python)
425 |
426 | # Python Program to determine if two trees are identical
427 | # Given two trees, return true if they are structurally identical
428 | def identicalTrees(T1, T2):
429 | # Both Empty
430 | if T1 is None and T2 is None:
431 | return True
432 |
433 | # Both non-empty - > compare them
434 | if T1 is not None and T2 is not None:
435 | return ((T1.data == T2.data) and
436 | identicalTrees(T1.left, T2.left) and
437 | identicalTrees(T1.right, T2.right))
438 |
439 |
440 | # One empty, one root -- false
441 | return False
442 | ```
443 |
444 | ```java
445 | // (Java)
446 | public bool identicalTrees(Node t1, Node t2)
447 | {
448 | // Both empty?
449 | if (t1 == null && t2 == null)
450 | return true;
451 |
452 | // Both non-empty -> compare them
453 | if (t1 != null && t2 != null)
454 | return ((t1.data == t2.data) &&
455 | identicalTrees(t1.left, t2.left) &&
456 | identicalTrees(t1.right, t2.right));
457 |
458 | // One empty, one not --> false
459 | return false;
460 | }
461 | ```
462 |
463 | ```c++
464 | // (C++)
465 | bool identicalTrees(node* t1, node* t2)
466 | {
467 | // Both empty?
468 | if (t1 == NULL && t2 == NULL)
469 | return true;
470 |
471 | // Both non-empty -> compare them
472 | if (t1 != NULL && t2 != NULL)
473 | return ((t1->data == t2->data) &&
474 | identicalTrees(t1->left, t2->left) &&
475 | identicalTrees(t1->right, t2->right));
476 |
477 | // One empty and the other not?
478 | return false;
479 | }
480 | ```
481 |
482 |
483 | ________________________________________________________________
484 |
485 | #### 4. Find the maximum depth or height of a tree
486 | ```python
487 | # (Python)
488 |
489 | # Compute the maxDepth of a tree -- the number of nodes along the longest path
490 | # from the root node down to the farthest leaf node
491 |
492 | def maxDepth(root):
493 | if root is None:
494 | return 0
495 |
496 | # Compute the depth of each subtree
497 | leftDepth = maxDepth(root.left)
498 | rightDepth = maxDepth(root.right)
499 |
500 | # return max + 1
501 | return max(leftDepth, rightDepth) + 1
502 | ```
503 |
504 | ```java
505 | // (Java)
506 | public int maxDepth(Node root)
507 | {
508 | if (root == null)
509 | return 0;
510 |
511 | // Compute the depth of each subtree
512 | int leftDepth = maxDepth(root.left);
513 | int rightDepth = maxDepth(root.right);
514 |
515 | // Return max + 1
516 | return Math.max(leftDepth, rightDepth);
517 | }
518 | ```
519 |
520 | ```c++
521 | // (C++)
522 | #include
523 |
524 | int maxDepth(node* root)
525 | {
526 | if (root == NULL)
527 | return 0;
528 |
529 | // Compute the depth of each subtree
530 | int leftDepth = maxDepth(root->left);
531 | int rightDepth = maxDepth(root->right);
532 |
533 | // Return max + 1
534 | return std::max(leftDepth, rightDepth);
535 | }
536 | ```
537 |
538 | ___________________________________________________________________
539 |
540 | #### 5. Delete a Tree
541 |
542 | ```python
543 | # (Python)
544 | # Function traverses tree in postorder to delete each and every node of the tree
545 | def deleteTree(root):
546 | if node is None:
547 | return
548 |
549 | # first delete both subtrees
550 | deleteTree(root.left)
551 | deleteTree(root.right)
552 | ```
553 |
554 | ```java
555 | // (Java)
556 | public void deleteTree(Node root)
557 | {
558 | if (root == null)
559 | return;
560 |
561 | // First delete both subtrees
562 | deleteTree(root.left);
563 | deleteTree(root.right);
564 | }
565 | ```
566 |
567 | ```c++
568 | // (C++)
569 | void deleteTree(node* root)
570 | {
571 | if (root == NULL)
572 | return;
573 |
574 | // Delete both subtrees
575 | deleteTree(root->left);
576 | deleteTree(root->right);
577 | }
578 | ```
579 |
580 | __________________________________________________________________
581 |
582 | #### 6. Convert a Tree into its mirror Tree
583 |
584 | Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of
585 | all non-leaf nodes interchanged.
586 |
587 | ```python
588 | # (Python)
589 | def mirror(root):
590 | if root is None:
591 | return
592 |
593 | mirror(root.left)
594 | mirror(root.right)
595 |
596 | # swap the pointer in this nodes
597 | temp = root.left
598 | root.left = root.right
599 | root.right = temp
600 | ```
601 |
602 | ```java
603 | // (Java)
604 | public void mirror(Node root)
605 | {
606 | if (root == null)
607 | return;
608 |
609 | mirror(root.left);
610 | mirror(root.right);
611 |
612 | // Swap the pointer in this node
613 | Node temp = root.left;
614 | root.left = root.right;
615 | root.right = temp;
616 | }
617 | ```
618 |
619 | ```c++
620 | // (C++)
621 | void mirror(node* root)
622 | {
623 | if (root == NULL)
624 | return;
625 |
626 | mirror(root->left);
627 | mirror(root->right);
628 |
629 | // Swap the pointer in this node
630 | node *temp = root->left;
631 | root->left = root->right;
632 | root->right = temp;
633 | }
634 | ```
635 |
--------------------------------------------------------------------------------