MCPcopy Create free account
hub / github.com/careercup/ctci / createTestTree

Method createTestTree

java/Chapter 4/Question4_5/QuestionB.java:61–82  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

59
60 /* Create a tree that may or may not be a BST */
61 public static TreeNode createTestTree() {
62 /* Create a random BST */
63 TreeNode head = AssortedMethods.randomBST(10, -10, 10);
64
65 /* Insert an element into the BST and potentially ruin the BST property */
66 TreeNode node = head;
67 do {
68 int n = AssortedMethods.randomIntInRange(-10, 10);
69 int rand = AssortedMethods.randomIntInRange(0, 5);
70 if (rand == 0) {
71 node.data = n;
72 } else if (rand == 1) {
73 node = node.left;
74 } else if (rand == 2) {
75 node = node.right;
76 } else if (rand == 3 || rand == 4) {
77 break;
78 }
79 } while (node != null);
80
81 return head;
82 }
83
84 public static void main(String[] args) {
85 /* Simple test -- create one */

Callers

nothing calls this directly

Calls 2

randomBSTMethod · 0.95
randomIntInRangeMethod · 0.95

Tested by

no test coverage detected