├── BinrayTree.iml ├── LICENSE.md ├── README.md ├── out └── production │ └── BinrayTree │ ├── BinaryTree.class │ ├── Main.class │ └── Node.class └── src ├── BinaryTree.java ├── Main.java └── Node.java /BinrayTree.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2021 Moeidheidari 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Binary tree implementation in Java 2 | -------------------------------------------------------------------------------- /out/production/BinrayTree/BinaryTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MoeidHeidari/Binary-tree-implementation-in-Java/79a54a2bc4c091b89d6105e0d092c56a42771cb7/out/production/BinrayTree/BinaryTree.class -------------------------------------------------------------------------------- /out/production/BinrayTree/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MoeidHeidari/Binary-tree-implementation-in-Java/79a54a2bc4c091b89d6105e0d092c56a42771cb7/out/production/BinrayTree/Main.class -------------------------------------------------------------------------------- /out/production/BinrayTree/Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MoeidHeidari/Binary-tree-implementation-in-Java/79a54a2bc4c091b89d6105e0d092c56a42771cb7/out/production/BinrayTree/Node.class -------------------------------------------------------------------------------- /src/BinaryTree.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class BinaryTree 3 | { 4 | private Node root; 5 | 6 | 7 | 8 | public BinaryTree(Node root) 9 | { 10 | this.setRoot(root); 11 | } 12 | 13 | public Node getRoot() { 14 | return root; 15 | } 16 | 17 | public void setRoot(Node root) { 18 | this.root = root; 19 | } 20 | 21 | public BinaryTree() 22 | { 23 | this.setRoot(null); 24 | } 25 | 26 | public void insert(Node newNode) 27 | { 28 | if(null == this.root) 29 | { 30 | this.root=newNode; 31 | } 32 | else if(this.root.getLeft() == null) 33 | { 34 | this.root.setLeft(newNode); 35 | } 36 | else if(this.root.getRight() == null) 37 | { 38 | this.root.setRight(newNode); 39 | } 40 | else 41 | { 42 | List siblingNodes=new LinkedList(); 43 | siblingNodes.add(this.root.getLeft()); 44 | siblingNodes.add(this.root.getRight()); 45 | insert2(siblingNodes,newNode); 46 | } 47 | } 48 | 49 | public void insert2(List siblingNodes,Node newNode) 50 | { 51 | 52 | List nextSiblingNodes=new LinkedList(); 53 | for(Node currentNode: siblingNodes) 54 | { 55 | if(currentNode.getLeft()==null) 56 | { 57 | currentNode.setLeft(newNode); 58 | return; 59 | } 60 | else if(currentNode.getRight()==null) 61 | { 62 | currentNode.setRight(newNode); 63 | return; 64 | } 65 | nextSiblingNodes.add(currentNode.getLeft()); 66 | nextSiblingNodes.add(currentNode.getRight()); 67 | } 68 | insert2(nextSiblingNodes,newNode); 69 | } 70 | 71 | public List bfsTraverse() 72 | { 73 | List bfsList=new LinkedList(); 74 | Queue nodeQueue=new LinkedList(); 75 | nodeQueue.add(this.root); 76 | 77 | while(nodeQueue.size() != 0) 78 | { 79 | Node n=nodeQueue.poll(); 80 | bfsList.add(n); 81 | if(n.getLeft() !=null) nodeQueue.add(n.getLeft()); 82 | if(n.getRight() != null) nodeQueue.add(n.getRight()); 83 | } 84 | return bfsList; 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main 2 | { 3 | public static void main(String[] args) 4 | { 5 | Node root=new Node(6); 6 | BinaryTree bnt=new BinaryTree(root); 7 | Node n1=new Node(3); 8 | Node n2=new Node(9); 9 | Node n3=new Node(1); 10 | Node n4=new Node(5); 11 | Node n5=new Node(7); 12 | Node n6=new Node(11); 13 | bnt.insert(n1); 14 | bnt.insert(n2); 15 | bnt.insert(n3); 16 | bnt.insert(n4); 17 | bnt.insert(n5); 18 | bnt.insert(n6); 19 | for(Node item: bnt.bfsTraverse()) 20 | { 21 | System.out.print(item.getValue()+"---"); 22 | } 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Node.java: -------------------------------------------------------------------------------- 1 | public class Node 2 | { 3 | //======================================Attributes============================================================= 4 | private Object value; 5 | private Node left; 6 | private Node right; 7 | //============================================================================================================== 8 | 9 | public Node() 10 | { 11 | 12 | } 13 | public Node(Object value) 14 | { 15 | this.value=value; 16 | } 17 | 18 | public void setValue(Object value) 19 | { 20 | this.value=value; 21 | } 22 | public Object getValue() 23 | { 24 | return this.value; 25 | } 26 | public Node getLeft() { 27 | return left; 28 | } 29 | 30 | public void setLeft(Node left) { 31 | this.left = left; 32 | } 33 | 34 | public Node getRight() { 35 | return right; 36 | } 37 | 38 | public void setRight(Node right) { 39 | this.right = right; 40 | } 41 | } 42 | --------------------------------------------------------------------------------