forked from careercup/CtCI-6th-Edition-Python
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solution careercup#6 + code readability improvements
- Loading branch information
1 parent
b8238f3
commit 82ba031
Showing
11 changed files
with
189 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
(ns chapter-4.chapter-4-q4 | ||
(:import (data_structures.tree BinaryTreeNode))) | ||
|
||
|
||
(defn tree-height [^BinaryTreeNode node] | ||
(if (nil? node) | ||
0 | ||
(+ 1 (tree-height (:left node)) (tree-height (:right node))))) | ||
(cond (nil? node) 0 | ||
:else (+ 1 (-> node :left tree-height) (-> node :right tree-height)))) | ||
|
||
(defn tree-balanced? [tree] | ||
(let [root tree | ||
height-delta (- (tree-height (:left root)) (tree-height (:right root)))] | ||
(let [height-delta (- (-> tree :left tree-height) (-> tree :right tree-height))] | ||
(<= (Math/abs height-delta) 1))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(ns chapter-4.chapter-4-q6 | ||
(:require [clojure.zip :as zip] | ||
[data-structures.tree :refer :all])) | ||
|
||
(defn- smallest [zipper-node] | ||
"Returns the smallest node in a zipper of a BST" | ||
(if (-> zipper-node zip/children empty?) | ||
(zip/node zipper-node) | ||
(if (some? (zip-left-child zipper-node)) | ||
(smallest (zip-left-child zipper-node)) | ||
(smallest (zip-right-child zipper-node))))) | ||
|
||
(defn- ancestor-successor [zipper-node node-data] | ||
(let [node (zip/node zipper-node) | ||
parent (zip-parent zipper-node)] | ||
(if (some? parent) | ||
(if (> (:data node) (-> parent zip/node :data)) | ||
(ancestor-successor parent node-data) | ||
(zip/node parent)) | ||
nil))) | ||
|
||
(defn successor [zipper-node] | ||
"Given a zipper structured node reference, returns its successor." | ||
(let [node (zip/node zipper-node)] | ||
(if (some? (:right node)) | ||
(smallest (zip-right-child zipper-node)) | ||
(ancestor-successor zipper-node (:data node))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(ns chapter-4.chapter-4-q6-test | ||
(:require [clojure.test :refer :all] | ||
[data-structures.tree :refer :all] | ||
[chapter-4.chapter-4-q6 :refer :all] | ||
[clojure.zip :as zip])) | ||
|
||
(deftest sucessor-test | ||
(let [tree (create-tree-zipper [4 | ||
[3 | ||
[2]] | ||
[6 | ||
[5] | ||
[8]]]) | ||
left-child (zip-left-child tree) | ||
right-child (zip-right-child tree) | ||
right-left-child (-> right-child zip/down) | ||
lowest (-> left-child zip/down) | ||
highest (-> right-child zip/down zip/right)] | ||
(is (= 5 (-> tree successor :data))) | ||
(is (= 4 (-> left-child successor :data))) | ||
(is (= 8 (-> right-child successor :data))) | ||
(is (= 6 (-> right-left-child successor :data))) | ||
(is (= 3 (-> lowest successor :data))) | ||
(is (nil? (-> highest successor :data)))) | ||
|
||
(let [largest-right-subtree-node (create-tree-zipper [6 | ||
[3 | ||
[2] [4 | ||
[] | ||
[5]]] | ||
[]]) | ||
lowest-left (-> largest-right-subtree-node | ||
zip-left-child | ||
zip-right-child | ||
zip-left-child)] | ||
(is (= 6 (-> lowest-left successor :data)))) | ||
|
||
(let [largest-right-subtree-node-2 (create-tree-zipper [10 | ||
[2 | ||
[0] [4 | ||
[3] | ||
[5 | ||
[6] | ||
[8]]]] | ||
[12]]) | ||
lowest-left (-> largest-right-subtree-node-2 | ||
zip-left-child | ||
zip-right-child | ||
zip-right-child | ||
zip-right-child)] | ||
(is (= 10 (-> lowest-left successor :data)))) | ||
) | ||
|
||
(run-tests) |