Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node {
- public int val;
- public Node left;
- public Node right;
- public Node(int val) {
- this.val = val;
- }
- }
- class BinTree {
- Node root;
- public BinTree() {
- root = null;
- }
- public void insert(int val) {
- if (root == null) {
- root = new Node(val);
- return;
- }
- insert(val, root);
- }
- private void insert(int val, Node node) {
- if (val < node.val) {
- if (node.left == null) {
- node.left = new Node(val);
- return;
- }
- insert(val, node.left);
- }
- else {
- if(node.right == null) {
- node.right = new Node(val);
- return;
- }
- insert(val, node.right);
- }
- }
- public void print() {
- print(root);
- }
- private void print(Node node) {
- if (node != null) {
- print(node.left);
- Console.WriteLine("{0}", node.val);
- print(node.right);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement