Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class BNode<E>{
- public E info;
- public BNode<E> left;
- public BNode<E> right;
- static int LEFT = 1;
- static int RIGHT = 2;
- public BNode(E info){ // konstruktor so eden argument, samo info poleto
- this.info = info;
- this.left = null;
- this.right = null;
- }
- public BNode(E info, BNode<E> left, BNode<E> right){
- this.info = info;
- this.left = left;
- this.right = right;
- }
- }
- class BTree<E>{
- public BNode<E> root;
- public BTree(){
- root = null;
- }
- public BTree(E info){
- root = new BNode<E>(info);
- }
- public BNode<E> addChild(BNode<E> Node, int where, E info){
- BNode<E> temp = new BNode<E>(info);
- if(where == BNode.LEFT){
- if(Node.left != null){
- return null;
- }
- Node.left = temp;
- }else{
- if(Node.right !=null){
- return null;
- }
- Node.right = temp;
- }
- return temp;
- }
- public void InorderR(BNode<E> r){
- if(r!=null){
- InorderR(r.left);
- System.out.println(r.info.toString() + " ");
- InorderR(r.right);
- }
- }
- public void Inorder(){
- System.out.print("INORDER: ");
- InorderR(root);
- System.out.println();
- }
- public void PreorderR(BNode<E> r){
- if(r!=null) {
- System.out.println(r.info.toString() + " ");
- InorderR(r.left);
- InorderR(r.right);
- }
- }
- public void Preorder(){
- System.out.print("PREORDER: ");
- PreorderR(root);
- System.out.println();
- }
- public void PostorderR(BNode<E> r){
- if(r!=null) {
- InorderR(r.left);
- InorderR(r.right);
- System.out.println(r.info.toString() + " ");
- }
- }
- public void PostorderR(){
- System.out.print("POSTORDER: ");
- PostorderR(root);
- System.out.println();
- }
- int maximumHeightOfNode(BNode<E> node) {
- if(node == null) {
- return 0;
- }
- int leftSide = maximumHeightOfNode(node.left);
- int rightSide = maximumHeightOfNode(node.right);
- return Math.max(leftSide, rightSide) + 1;
- }
- int calculateDiameter(BNode<E> node) {
- if(node == null) {
- return 0;
- }
- int leftSide = maximumHeightOfNode(node.left);
- int rightSide = maximumHeightOfNode(node.right);
- int heightOfNode = leftSide + rightSide + 1; // eden od rezultatite
- int leftDiameter = calculateDiameter(node.left);
- int rightDiameter = calculateDiameter(node.right);
- int maxDiameter = Math.max(leftDiameter, rightDiameter);
- return Math.max(heightOfNode, maxDiameter);
- }
- }
- public class Main {
- public static void main(String[] args) {
- //BNode<Integer> root = new BNode<Integer>(10);
- BTree<Integer> drvo = new BTree<Integer>(10);
- drvo.addChild(drvo.root, drvo.root.LEFT, 5);
- drvo.addChild(drvo.root, drvo.root.RIGHT, 6);
- drvo.addChild(drvo.root.left, drvo.root.left.LEFT, 7);
- drvo.addChild(drvo.root.left, drvo.root.left.RIGHT, 9);
- drvo.addChild(drvo.root.right, drvo.root.right.RIGHT, 8);
- drvo.addChild(drvo.root.left.left, drvo.root.left.left.LEFT, 5);
- // drvo.InorderR(drvo.root);
- // int i= drvo.numLeaves1(drvo.root);
- System.out.println(drvo.calculateDiameter(drvo.root));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement