Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PE_TREE {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- BTree bTree = new BTree();
- while(in.hasNextLine())
- {
- String s = in.nextLine();
- if (s.length() < 1) break;
- bTree.add(Integer.parseInt(s));
- }
- bTree.postOrder(bTree.root);
- }
- }
- class BTree {
- TNode root = null;
- public void add(int value)
- {
- if (root == null)
- root = new TNode(value);
- else
- {
- TNode current = this.root;
- while(true)
- {
- if (value < current.data)
- {
- if (current._lft == null)
- {
- current._lft = new TNode(value);
- return;
- }
- else
- current = current._lft;
- }
- else
- {
- if (current._rft == null)
- {
- current._rft = new TNode(value);
- return;
- }
- else
- current = current._rft;
- }
- }
- }
- }
- public void postOrder(TNode node)
- {
- if (node != null)
- {
- postOrder(node._lft);
- postOrder(node._rft);
- System.out.println(node.data);
- }
- }
- }
- class TNode {
- int data;
- TNode _lft;
- TNode _rft;
- TNode (int data) {
- this.data = data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement