{"id":24021089,"url":"https://github.com/zipcodecore/treesgrowdownward","last_synced_at":"2025-07-18T12:36:32.826Z","repository":{"id":147838384,"uuid":"279401697","full_name":"ZipCodeCore/TreesGrowDownward","owner":"ZipCodeCore","description":"build a simple sorted binary tree contains Integer objects.","archived":false,"fork":false,"pushed_at":"2024-02-14T14:08:43.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-08T12:41:13.923Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZipCodeCore.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-13T20:11:15.000Z","updated_at":"2024-09-27T23:32:39.000Z","dependencies_parsed_at":"2025-01-08T12:38:42.553Z","dependency_job_id":"342e06cd-d81f-4d21-9d83-2ca7f9ad9827","html_url":"https://github.com/ZipCodeCore/TreesGrowDownward","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZipCodeCore%2FTreesGrowDownward","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZipCodeCore%2FTreesGrowDownward/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZipCodeCore%2FTreesGrowDownward/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZipCodeCore%2FTreesGrowDownward/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZipCodeCore","download_url":"https://codeload.github.com/ZipCodeCore/TreesGrowDownward/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240766672,"owners_count":19854114,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-08T12:38:37.283Z","updated_at":"2025-02-25T23:46:35.104Z","avatar_url":"https://github.com/ZipCodeCore.png","language":"Java","readme":"# TreesGrowDownward\nbuild a simple sorted binary tree contains Integer objects.\n\nA binary tree is a _recursive data structure_ where each node can have 2 children at most.\n\nA common type of binary tree is a binary search tree, in which every node has a value that is greater\nthan or equal to the node values in the left sub-tree, and less than or equal to the node values in the right sub-tree.\n\nHere's a quick visual representation of this type of binary tree:\n\n![binary tree](./simplebinarytree.png)\n\nTake a look at Node.java, it's a very simple container to hold our Integer object.\n\nNow, what operations do yo need to implement in TreeOneZero.java?\n\n## Some Common Operations\n\nNow, let's see the most common operations we can perform on a binary tree. You should step through each operation,\nadding the methods described. You'll be making all these changes to class __TreeOneZero__, a special\nbinary tree class.\n\n### Inserting Elements\n\nThe first operation we're going to cover is the insertion of new nodes.\n\nFirst, we have to find the place where we want to add a new node in order to keep the tree sorted. We'll follow these rules starting from the root node:\n\n`private Node addRecursive(Node current, Integer value)`\n\n- if current node is null, we've reached a leaf node and we can insert the new node in that position\n- if the new node's value is lower than the current node's, we call addRecurive on the left child\n- if the new node's value is greater than the current node's, we call addRecurive on the right child\n\nNext, we'll create the public method that starts the recursion from the root node:\n\n`public void add(Integer value)`\n\n- we set this.root to `addRecursive` for the value object.\n\nThe idea is to create two methods that allow you to do this:\nNow let's see how we can use this method to create the tree from our example:\n\n```\nprivate TreeOneZero createBinaryTree() {\n    TreeOneZero bt = new TreeOneZero();\n \n    bt.add(Integer(6));\n    bt.add(Integer(4));\n    bt.add(Integer(8));\n    bt.add(Integer(3));\n    bt.add(Integer(5));\n    bt.add(Integer(7));\n    bt.add(Integer(9));\n \n    return bt;\n}\n```\n\n### Finding an Element\n\nLet's now add a method to check if the tree contains a specific value.\n\nWe will use Recursion to make pretty simple method which can return true or false.\nRECURSION is a technique where a method can call **itself**, usually with different parameters, \nbased on the current status of the method. \n\nAs before, we'll first create a recursive method that traverses the tree:\n\n`private boolean containsNodeRecursive(Node current, Integer value)`\n\nAnd it will have two __base cases__, one which returns false, and another which return true. \nIf neither case is true, we change the state and call the method itself with changed parameters:\n\n- if current is null, return false\n- if current's value equals what we're looking for, return true\n- otherwise if the value is less than current's value,\n    - call containsNodeRecursive on current's left node\n    - otherwise call is on the right node.\n    \nBuild a simple start method to the recursion much like we did with the `add` and `addRecursive` methods:\n`public boolean containsNode(Integer value) `\n\nThese two methods need to be able to pass a test like this one:\n\n```aidl\n@Test\npublic void givenABinaryTree_WhenAddingElements_ThenTreeContainsThoseElements() {\n    BinaryTree bt = createBinaryTree();\n \n    assertTrue(bt.containsNode(6));\n    assertTrue(bt.containsNode(4));\n \n    assertFalse(bt.containsNode(1));\n}\n```\n\n### Deleting an Element\n\nAnother common operation is the deletion of a node from the tree.\n\nBy this point, you're starting to see that we have acode pattern here. Define a recursive method, and \nthen define a starter method that kicks off the recursion.\n\nFirst, we have to find the node to delete in a similar way as we did before:\n\n`private Node deleteRecursive(Node current, Integer value)`\n\nNotice here, we want to return the Node we are removing.\n\nNow, Once we find the node to delete, there are 3 main different cases:\n     \n- a node has no children – this is the simplest case; we just need to replace this node with null in its parent node\n- a node has exactly one child – in the parent node, we replace this node with its only child.\n- a node has two children – this is the most complex case because it requires a tree reorganization\n\nWe can implement the first case when the node is a leaf node:\n\n- if both left and right of this node are null, return null\n\nNow let's continue with the case when the node has one child:\n\n- if the right node is null, return left node\n- if the left node is null, return right node\n\nHere, we're returning the non-null child so it can be assigned to the parent node.\n\nFinally, we have to handle the case where the node has two children.\n\nFirst, we need to find the node that will replace the deleted node. \nWe'll use the smallest node of the node to be deleted's right sub-tree:\n\n`private int findSmallestValue(Node n)`\n\n- if the left node is null, return n's object,\n- otherwise call findSmallestValue on n's left node\n\nThen, we assign the smallest value to the node to delete and after that, we'll delete it from the right subtree:\n\nint smallestValue = findSmallestValue(current.right);\ncurrent.value = smallestValue;\ncurrent.right = deleteRecursive(current.right, smallestValue);\nreturn current;\n\nFinally, let's create the public method that starts the deletion from the root:\n\n`public void delete(Integer value)`\n\nNow, let's check that the deletion works as expected:\n\n```\n@Test\npublic void givenABinaryTree_WhenDeletingElements_ThenTreeDoesNotContainThoseElements() {\n    BinaryTree bt = createBinaryTree();\n \n    assertTrue(bt.containsNode(9));\n    bt.delete(9);\n    assertFalse(bt.containsNode(9));\n}\n```\n\n### Traversing (Walking or Visiting) the Tree\n\nIn this section, we'll see different ways of traversing a tree, covering in detail the depth-first and breadth-first searches.\n\nWe'll use the same tree that we used before and we'll show the traversal order for each case.\n\n#### Depth-First Search\n\nDepth-first search is a type of traversal that goes deep as much as possible in every child before exploring the next sibling.\n\nThere are several ways to perform a depth-first search: in-order, pre-order and post-order.\n\nThe in-order traversal consists of first visiting the left sub-tree, then the root node, and finally the right sub-tree:\n\n`public void traverseInOrder(Node node)`\n\nIt would do something like:\n- if node is not null\n  - traverseInOrder(node.left)\n  - print(\" \" + node.value)\n  - traverseInOrder(node.right)\n    \n\nIf we call this method, the console output will show the in-order traversal:\n\n3 4 5 6 7 8 9\n\nPre-order traversal visits first the root node, then the left subtree, and finally the right subtree:\n\n`public void traversePreOrder(Node node)`\n\nLook carefully how this is different from **in order**.\n\n- if (node != null) \n  - print(\" \" + node.value)\n  - traversePreOrder(node.left)\n  - traversePreOrder(node.right)\n\nAnd let's check the pre-order traversal in the console output:\n\n6 4 3 5 8 7 9\n\nPost-order traversal visits the left subtree, the right subtree, and the root node at the end:\n\n`public void traversePostOrder(Node node)`\n\n- if (node != null) \n  - traversePostOrder(node.left)\n  - traversePostOrder(node.right)\n  - print(\" \" + node.value)\n\nHere are the nodes in post-order:\n\n3 5 4 7 9 8 6\n\n#### Breadth-First Search\n\nThis is another common type of traversal that visits all the nodes of a level before going to the next level.\n\nThis kind of traversal is also called level-order and visits all the levels of the tree starting from the root, and from left to right.\n\nFor the implementation, we'll use a Queue to hold the nodes from each level in order. We'll extract each node from the list, print its values, then add its children to the queue:\n\nThis is the most complex of the routines, and it's a gift :-)\n\n```java\npublic void traverseLevelOrder() {\n    if (root == null) {\n        return;\n    }\n \n    Queue\u003cNode\u003e nodes = new LinkedList\u003c\u003e();\n    nodes.add(root);\n \n    while (!nodes.isEmpty()) {\n \n        Node node = nodes.remove();\n \n        System.out.print(\" \" + node.value);\n \n        if (node.left != null) {\n            nodes.add(node.left);\n        }\n \n        if (node.right != null) {\n            nodes.add(node.right);\n        }\n    }\n}\n```\n\nIn this case, the order of the nodes will be:\n\n6 4 8 3 5 7 9\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzipcodecore%2Ftreesgrowdownward","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzipcodecore%2Ftreesgrowdownward","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzipcodecore%2Ftreesgrowdownward/lists"}