Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lol;
- import DS.*;
- public class idkmain {
- public static void LenPrint(BinNode<Integer> root){
- Queue<BinNode<Integer>> q = new Queue<BinNode<Integer>>();
- q.insert(root);
- while(!q.isEmpty()){
- if(q.head().hasLeft()){
- q.insert(q.head().getLeft());
- }
- if(q.head().hasRight()){
- q.insert(q.head().getRight());
- }
- System.out.print(q.remove().getValue());
- }
- }
- public static void printBinaryTree(BinNode root, int level){
- if(root==null)
- return;
- printBinaryTree(root.getRight(), level+1);
- if(level!=0){
- for(int i=0;i<level-1;i++)
- System.out.print("|\t");
- System.out.println("|-------"+root.getValue());
- }
- else
- System.out.println(root.getValue());
- printBinaryTree(root.getLeft(), level+1);
- }
- public static boolean sortedByLevel(BinNode<Integer> root){
- //does not work
- Queue<BinNode<Integer>> temp = new Queue<BinNode<Integer>>();
- Queue<BinNode<Integer>> q = new Queue<BinNode<Integer>>();
- q.insert(root);
- while(!temp.isEmpty()){
- if(q.head().hasLeft()){
- q.insert(q.head().getLeft());
- }
- if(q.head().hasRight()){
- q.insert(q.head().getRight());
- }
- q.insert(temp.remove());
- }
- while(!q.isEmpty()){
- System.out.println(q.remove());
- }
- while(!q.isEmpty()){
- BinNode<Integer> tempB = q.remove();
- System.out.println(tempB.getValue());
- if(!q.isEmpty()){
- System.out.println(tempB.getValue()+","+q.head().getValue());
- if(!(tempB.getValue()<q.head().getValue())){
- return false;
- }
- }
- }
- return true;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- BinNode<Integer> root = new BinNode<Integer>(1);
- BinNode<Integer> t2 = new BinNode<Integer>(2);
- BinNode<Integer> t3 = new BinNode<Integer>(3);
- BinNode<Integer> t4 = new BinNode<Integer>(4);
- BinNode<Integer> t5 = new BinNode<Integer>(5);
- BinNode<Integer> t6 = new BinNode<Integer>(6);
- BinNode<Integer> t7 = new BinNode<Integer>(7);
- BinNode<Integer> t8 = new BinNode<Integer>(8);
- BinNode<Integer> t9 = new BinNode<Integer>(9);
- BinNode<Integer> t10 = new BinNode<Integer>(10);
- root.setLeft(t3);
- root.setRight(t2);
- t2.setRight(t4);
- t4.setLeft(t7);
- t3.setLeft(t6);
- t3.setRight(t5);
- t5.setLeft(t9);
- t5.setRight(t8);
- t8.setLeft(t10);
- LenPrint(root);
- System.out.println();
- printBinaryTree(root, 1);
- System.out.println(sortedByLevel(root));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement