Skip to content

Commit

Permalink
fix code linting
Browse files Browse the repository at this point in the history
  • Loading branch information
flexelem committed Sep 13, 2024
1 parent 1ca23aa commit e10f24f
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 290 deletions.
20 changes: 10 additions & 10 deletions _posts/2014-05-20-bubble-sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ Finally, our array is sorted, however, it needs an additional iteration to figur

<h2> Code </h2>

<pre>
<code class="language-java">public class BubbleSort {
```java
public class BubbleSort {

public void bubbleSort(int[] numbers) {
boolean swapped = true;

while (swapped) { // continue until there is no swap
swapped = false;
for (int k = 0; k &lt; numbers.length - 1; k++) {
if (numbers[k] &gt; numbers[k + 1]) {
for (int k = 0; k < numbers.length - 1; k++) {
if (numbers[k] > numbers[k + 1]) {
swap(numbers, k);
swapped = true; // if there is at least one swap make swapped true
}
Expand All @@ -116,13 +116,13 @@ Finally, our array is sorted, however, it needs an additional iteration to figur
numbers[k + 1] = numbers[k];
numbers[k] = temp;
}
}</code>
</pre>
}
```

And here are a bunch of test cases.

<pre>
<code class="language-java">public class BubbleSortTest {
```java
public class BubbleSortTest {

private BubbleSort testClass;

Expand Down Expand Up @@ -155,5 +155,5 @@ And here are a bunch of test cases.
assertArrayEquals(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, numbers);
}

}</code>
</pre>
}
```
108 changes: 58 additions & 50 deletions _posts/2014-06-02-binary-search-trees.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ There are many possible trees for same set of keys. Thus, a height of a tree cou

![height_of_bst]({{ site.url }}/assets/img/2014/06/height_of_BST.png)

<pre><code class="language-java">public int height(Node root) {
```java
public int height(Node root) {
if (root == null) {
return 0;
}
Expand All @@ -97,8 +98,8 @@ There are many possible trees for same set of keys. Thus, a height of a tree cou
int rightHeight = height(root.getRightChild());
int max = Math.max(leftHeight, rightHeight) + 1;
return max;
}</code>
</pre>
}
```

<h3> Tree Walk </h3>

Expand All @@ -121,15 +122,15 @@ The binary search tree property allows us to print keys in sorted order by a sim

inorderTreeWalk procedure is follows;

<pre><code class="language-java">public void inorderTreeWalk(Node x) {
```java
public void inorderTreeWalk(Node x) {
if (x != null) {

inorderTreeWalk(x.getLeftChild());
System.out.println(x.getKey());
inorderTreeWalk(x.getRightChild());
}
}</code>
</pre>
}
```

In order tree walk take \(O(n)\) time because it traverses each node in the tree recursively.

Expand All @@ -153,15 +154,15 @@ Traverse order;

Its running time is \(O(n)\) as well. Its procedure is as follows;

<pre><code class="language-java" >public void preorderTreeWalk(Node x) {
```java
public void preorderTreeWalk(Node x) {
if (x != null) {

System.out.println(x.getKey());
System.out.println(x.getKey());
preorderTreeWalk(x.getLeftChild());
preorderTreeWalk(x.getRightChild());
}
}</code>
</pre>
}
```

<b>Postorder Tree Walk: </b><br>
Traverse order;
Expand All @@ -182,15 +183,15 @@ Traverse order;

Its running time is \(O(n)\) as well. Its procedure is as follows;

<pre><code class="language-java">public void postorderTreeWalk(Node x) {
```java
public void postorderTreeWalk(Node x) {
if (x != null) {

System.out.println(x.getKey());
postorderTreeWalk(x.getLeftChild());
postorderTreeWalk(x.getRightChild());
System.out.println(x.getKey());
postorderTreeWalk(x.getLeftChild());
postorderTreeWalk(x.getRightChild());
}
}</code>
</pre>
}
```

In addition, the iterative versions of tree walks could be find at its <a href="http://en.wikipedia.org/wiki/Tree_traversal" title="wiki">wiki page</a>

Expand All @@ -213,19 +214,19 @@ To search for a key <b>k</b> in BST;
</ul>
</div>

<pre><code class="language-java" >public Node search(Node x, int key) {
```java
public Node search(Node x, int key) {
while (x != null && key != x.getKey()) {

if (key &lt; x.getKey()) {
if (key < x.getKey()) {
x = x.getLeftChild();
} else {
x = x.getRightChild();
}
}

return x;
}</code>
</pre>
}
```

The running time of search procedure is \(O(height)\).

Expand All @@ -245,23 +246,25 @@ The running time of search procedure is \(O(height)\).
</ul>
</div>

<pre><code class="language-java" >public Node treeMinimum(Node x) {
```java
public Node treeMinimum(Node x) {
while (x.getLeftChild() != null) {
x = x.getLeftChild();
}

return x;
}</code>
</pre>
}
```

<pre><code class="language-java" >public Node treeMaximum(Node x) {
```java
public Node treeMaximum(Node x) {
while (x.getRightChild() != null) {
x = x.getRightChild();
}

return x;
}</code>
</pre>
}
```

Both the procedures run in \(O(height)\) time.

Expand All @@ -282,7 +285,8 @@ The successor of a node <b>x</b> is the node with smallest key greater than <b>x

The procedure is as follow;

<pre><code class="language-java">public Node treeSuccessor(Node x) {
```java
public Node treeSuccessor(Node x) {
if (x.getRightChild() != null) {
return treeMinimum(x.getRightChild());
}
Expand All @@ -295,8 +299,8 @@ The procedure is as follow;
}

return parent;
}</code>
</pre>
}
```


<b>Predecessor : </b><br>
Expand All @@ -318,20 +322,21 @@ The predecessor of a node <b>x</b> is the node with greatest key less than <b>x<

The procedure is as follow;

<pre><code class="language-java">public Node treePredecessor(Node x) {
```java
public Node treePredecessor(Node x) {
if (x.getLeftChild() != null) {
return treeMaximum(x.getLeftChild());
}

Node parent = x.getParent();

while (parent != null && parent.getKey() &gt; x.getKey()) {
while (parent != null && parent.getKey() > x.getKey()) {
parent = parent.getParent();
}

return parent;
}</code>
</pre>
}
```

Both the procedures run in \(O(height)\) time.

Expand Down Expand Up @@ -361,33 +366,34 @@ To insert a key <b>k</b>;

insert procedure is as follows;

<pre><code class="language-java">public void insert(Node x) {
```java
public void insert(Node x) {
Node y = null;
Node z = root;

while (z != null) {
y = z;

if (x.getKey() &lt;= z.getKey()) {
if (x.getKey() <= z.getKey()) {
z = z.getLeftChild();
} else {
z = z.getRightChild();
}
}

// set y as x's parent
// set y as x's parent
x.setParent(y);

// if root is already null then set x as root
// if root is already null then set x as root
if (root == null) {
root = x;
} else if (x.getKey() &lt;= y.getKey()) { // set x as left child of its parent y
} else if (x.getKey() <= y.getKey()) { // set x as left child of its parent y
y.setLeftChild(x);
} else { // set x as right child of its parent y
y.setRightChild(x);
}
}</code>
</pre>
}
```

The running time of insertion is \(O(height)\).

Expand Down Expand Up @@ -417,7 +423,8 @@ An illustration of deleting a key for each case;

The delete procedure will use a subroutine which is called <em>transplant</em> to handle with the cases when <b>x</b> has no children, or only one child. Transplant and delete procedures are as follows;

<pre><code class="language-java">public void delete(int key) {
```java
public void delete(int key) {
Node delNode = search(root, key);

if (delNode.getLeftChild() == null) {
Expand All @@ -431,10 +438,11 @@ The delete procedure will use a subroutine which is called <em>transplant</em> t
Node z = y.getLeftChild();
transplant(y, z);
}
}</code>
</pre>
}
```

<pre><code class="language-java">private void transplant(Node u, Node v) {
```java
private void transplant(Node u, Node v) {
if (u.getParent() == null) { // we delete the root
root = v;
} else if (u.getParent().getLeftChild() == u) {
Expand All @@ -446,8 +454,8 @@ The delete procedure will use a subroutine which is called <em>transplant</em> t
if (v != null) {
v.setParent(u.getParent());
}
}</code>
</pre>
}
```

The running time of delete procedure is \(O(height)\).

Expand Down
Loading

0 comments on commit e10f24f

Please sign in to comment.