Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- public class Tree {
- class Node
- {
- int key;
- Node left;
- Node right;
- public Node(int item)
- {
- key = item;
- right = null;
- left = null;
- }
- }
- Node root;
- Tree()
- {
- root = null;
- }
- void insert(int key)
- {
- root = insertRec(key, root);
- }
- Node insertRec(int key, Node root)
- {
- if (root == null)
- {
- root = new Node(key);
- return root;
- }
- if (key < root.key)
- {
- root.left = insertRec(key, root.left);
- }
- else
- {
- root.right = insertRec(key, root.right);
- }
- return root;
- }
- void inorder()
- {
- inorderRec(root);
- }
- void inorderRec(Node root)
- {
- if (root != null)
- {
- inorderRec(root.left);
- System.out.print(" " + root.key + " ");
- inorderRec(root.right);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement