Search in sources :

Example 56 with TreeNode

use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.

the class JulyWeek4 method recurseTreeAnother.

public TreeNode recurseTreeAnother(TreeNode currentNode, TreeNode p, TreeNode q) {
    if (currentNode == null)
        return null;
    if (currentNode.val == p.val || currentNode.val == q.val)
        return currentNode;
    TreeNode right = recurseTreeAnother(currentNode.left, p, q);
    TreeNode left = recurseTreeAnother(currentNode.right, p, q);
    if (right != null && left != null) {
        return currentNode;
    }
    if (right == null && left != null) {
        return left;
    }
    if (right != null && left == null) {
        return right;
    }
    return null;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 57 with TreeNode

use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.

the class DecemberW1 method buildFromPostOrder.

public TreeNode buildFromPostOrder(int left, int right, int[] postorder, HashMap<Integer, Integer> mapInorder) {
    if (left > right)
        return null;
    TreeNode root = new TreeNode(postorder[postIdx--]);
    int ri = mapInorder.get(root.val);
    root.right = buildFromPostOrder(ri + 1, right, postorder, mapInorder);
    root.left = buildFromPostOrder(left, ri - 1, postorder, mapInorder);
    return root;
}
Also used : TreeNode(com.pkumar7.TreeNode)

Example 58 with TreeNode

use of com.pkumar7.TreeNode in project Data-Structures-Algorithms by pankajgangwar.

the class JulyW1 method isCompleteTree.

/*
    LC : 958
    https://leetcode.com/problems/check-completeness-of-a-binary-tree/
    */
public boolean isCompleteTree(TreeNode root) {
    if (root == null)
        return true;
    Queue<TreeNode> q = new LinkedList<>();
    q.offer(root);
    int level = 0;
    Integer lastLevel = null;
    while (!q.isEmpty()) {
        int size = q.size();
        boolean isLastLevel = false;
        while (size-- > 0) {
            TreeNode curr = q.poll();
            if (curr.left == null) {
                if (curr.right != null) {
                    return false;
                }
            }
            if (isLastLevel && curr.left != null) {
                // This node should not have any child as node with left child already encountered
                return false;
            }
            if (curr.right == null && lastLevel == null) {
                // We are at last level
                isLastLevel = true;
                lastLevel = level + 2;
            }
            if (curr.left != null) {
                q.offer(curr.left);
            }
            if (curr.right != null) {
                q.offer(curr.right);
            }
            if (lastLevel != null) {
                if (lastLevel == level) {
                    return false;
                }
            }
        }
        level++;
    }
    return true;
}
Also used : TreeNode(com.pkumar7.TreeNode) LinkedList(java.util.LinkedList)

Example 59 with TreeNode

use of com.pkumar7.TreeNode in project TNTConcept by autentia.

the class MenuBean method getMenu.

/**
 * Get menu tree
 * @return menu tree
 */
public TreeNode getMenu() {
    // Create menu only the first time
    if (menu == null) {
        Principal creds = SpringUtils.getPrincipal();
        Stack<TreeNode> path = new Stack<TreeNode>();
        menu = new TreeNodeBase("menu", "Menu", false);
        path.push(menu);
        if (openNode(path, creds, null, "admin")) {
            addLeaf(path, creds, Permission.Entity_Menu(User.class), "users");
            addLeaf(path, creds, Permission.Entity_Menu(UserCategory.class), "userCategorys");
            addLeaf(path, creds, null, "changePassword");
            addLeaf(path, creds, Permission.Entity_Menu(Department.class), "departments");
            // addLeaf( path, creds, Permission.Entity_Menu(Setting.class),      "settings" );
            closeNode(path);
        }
        if (openNode(path, creds, null, "masterTables")) {
            addLeaf(path, creds, Permission.Entity_Menu(AccountEntryType.class), "accountEntryTypes");
            addLeaf(path, creds, Permission.Entity_Menu(OrganizationType.class), "organizationTypes");
            addLeaf(path, creds, Permission.Entity_Menu(InteractionType.class), "interactionTypes");
            addLeaf(path, creds, Permission.Entity_Menu(OrganizationISOCategory.class), "organizationISOCategorys");
            addLeaf(path, creds, Permission.Entity_Menu(ContractType.class), "contractTypes");
            addLeaf(path, creds, Permission.Entity_Menu(Magazine.class), "magazines");
            addLeaf(path, creds, Permission.Entity_Menu(OfferRejectReason.class), "offerRejectReasons");
            closeNode(path);
        }
        if (openNode(path, creds, null, "billing")) {
            addLeaf(path, creds, Permission.Entity_Menu(Bill.class), "bills");
            addLeaf(path, creds, Permission.Entity_Menu(Account.class), "accounts");
            addLeaf(path, creds, Permission.Entity_Menu(AccountEntry.class), "accountEntrys");
            addLeaf(path, creds, Permission.Entity_Menu(PeriodicalAccountEntry.class), "periodicalAccountEntrys");
            addLeaf(path, creds, Permission.Action_NOF, "nof");
            addLeaf(path, creds, Permission.Entity_Menu(FinancialRatio.class), "financialRatios");
            closeNode(path);
        }
        if (openNode(path, creds, null, "contacts")) {
            addLeaf(path, creds, Permission.Entity_Menu(Organization.class), "organizations");
            addLeaf(path, creds, Permission.Entity_Menu(Interaction.class), "interactions");
            addLeaf(path, creds, Permission.Entity_Menu(Contact.class), "contacts");
            addLeaf(path, creds, Permission.Entity_Menu(Offer.class), "offers");
            addLeaf(path, creds, Permission.Entity_Menu(Project.class), "projects");
            closeNode(path);
        }
        if (openNode(path, creds, null, "quality")) {
            addLeaf(path, creds, Permission.Action_ListQualityDocuments, "qualityDocuments");
            closeNode(path);
        }
        if (openNode(path, creds, null, "bulletin")) {
            addLeaf(path, creds, Permission.Entity_Menu(BulletinBoard.class), "bulletinBoards");
            addLeaf(path, creds, Permission.Entity_Menu(CompanyState.class), "companyStates");
            addLeaf(path, creds, Permission.Entity_Menu(BulletinBoardCategory.class), "bulletinBoardCategorys");
            addLeaf(path, creds, Permission.Entity_Menu(Idea.class), "ideas");
            closeNode(path);
        }
        if (openNode(path, creds, null, "activity")) {
            addLeaf(path, creds, Permission.Entity_Menu(Activity.class), "activitys");
            addLeaf(path, creds, Permission.Entity_Menu(Objective.class), "objectives");
            closeNode(path);
        }
        if (openNode(path, creds, null, "reports")) {
            addLeaf(path, creds, Permission.Action_GeneralReports, "generalReports");
            addLeaf(path, creds, Permission.Action_BitacoreReports, "bitacoreReports");
            addLeaf(path, creds, Permission.Action_BillReports, "billReports");
            addLeaf(path, creds, Permission.Action_ProjectReports, "projectReports");
            addLeaf(path, creds, Permission.Action_InteractionReports, "interactionReports");
            addLeaf(path, creds, Permission.Action_OrganizationReports, "organizationReports");
            addLeaf(path, creds, Permission.Action_OfferReports, "offerReports");
            addLeaf(path, creds, Permission.Action_OwnReports, "ownReports");
            addLeaf(path, creds, Permission.Action_PersonalReports, "personalReports");
            closeNode(path);
        }
        if (openNode(path, creds, null, "publish")) {
            addLeaf(path, creds, Permission.Entity_Menu(Tutorial.class), "tutorials");
            addLeaf(path, creds, Permission.Entity_Menu(Publication.class), "publications");
            closeNode(path);
        }
        if (openNode(path, creds, null, "holiday")) {
            addLeaf(path, creds, Permission.Entity_Menu(Holiday.class), "holidays");
            addLeaf(path, creds, Permission.Entity_Menu(RequestHoliday.class), "requestHolidays");
            addLeaf(path, creds, Permission.Entity_Menu(AdminHoliday.class), "adminHolidays");
            closeNode(path);
        }
        if (openNode(path, creds, null, "utils")) {
            addLeaf(path, creds, Permission.Entity_Menu(Book.class), "books");
            addLeaf(path, creds, Permission.Entity_Menu(Inventary.class), "inventarys");
            closeNode(path);
        }
    }
    return menu;
}
Also used : InteractionType(com.autentia.tnt.businessobject.InteractionType) UserCategory(com.autentia.tnt.businessobject.UserCategory) Account(com.autentia.tnt.businessobject.Account) User(com.autentia.tnt.businessobject.User) Organization(com.autentia.tnt.businessobject.Organization) BulletinBoardCategory(com.autentia.tnt.businessobject.BulletinBoardCategory) Activity(com.autentia.tnt.businessobject.Activity) PeriodicalAccountEntry(com.autentia.tnt.businessobject.PeriodicalAccountEntry) AccountEntry(com.autentia.tnt.businessobject.AccountEntry) TreeNodeBase(org.apache.myfaces.custom.tree2.TreeNodeBase) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) Department(com.autentia.tnt.businessobject.Department) PeriodicalAccountEntry(com.autentia.tnt.businessobject.PeriodicalAccountEntry) TreeNode(org.apache.myfaces.custom.tree2.TreeNode) Book(com.autentia.tnt.businessobject.Book) OrganizationISOCategory(com.autentia.tnt.businessobject.OrganizationISOCategory) FinancialRatio(com.autentia.tnt.businessobject.FinancialRatio) Tutorial(com.autentia.tnt.businessobject.Tutorial) Interaction(com.autentia.tnt.businessobject.Interaction) Publication(com.autentia.tnt.businessobject.Publication) AccountEntryType(com.autentia.tnt.businessobject.AccountEntryType) CompanyState(com.autentia.tnt.businessobject.CompanyState) OrganizationType(com.autentia.tnt.businessobject.OrganizationType) Inventary(com.autentia.tnt.businessobject.Inventary) Stack(java.util.Stack) Contact(com.autentia.tnt.businessobject.Contact) BulletinBoard(com.autentia.tnt.businessobject.BulletinBoard) Project(com.autentia.tnt.businessobject.Project) Objective(com.autentia.tnt.businessobject.Objective) OfferRejectReason(com.autentia.tnt.businessobject.OfferRejectReason) Offer(com.autentia.tnt.businessobject.Offer) Idea(com.autentia.tnt.businessobject.Idea) Holiday(com.autentia.tnt.businessobject.Holiday) AdminHoliday(com.autentia.tnt.businessobject.AdminHoliday) RequestHoliday(com.autentia.tnt.businessobject.RequestHoliday) Bill(com.autentia.tnt.businessobject.Bill) ContractType(com.autentia.tnt.businessobject.ContractType) Principal(com.autentia.tnt.manager.security.Principal) Magazine(com.autentia.tnt.businessobject.Magazine) AdminHoliday(com.autentia.tnt.businessobject.AdminHoliday)

Example 60 with TreeNode

use of com.pkumar7.TreeNode in project TNTConcept by autentia.

the class MenuBean method closeNode.

/**
 * Finalize an opened node and return its parent. If the given node has no
 * childs it is removed from the tree.
 * @param path path of current node
 * @return false if the node was removed
 */
private boolean closeNode(Stack<TreeNode> path) {
    boolean closed = true;
    TreeNode node = path.pop();
    if (node.getChildCount() == 0) {
        path.peek().getChildren().remove(node);
        closed = false;
    }
    log.debug("addLeaf - " + (closed ? "CLOSE " : "REMOVE") + ": " + node.getIdentifier());
    return closed;
}
Also used : TreeNode(org.apache.myfaces.custom.tree2.TreeNode)

Aggregations

TreeNode (com.pkumar7.TreeNode)47 LinkedList (java.util.LinkedList)16 ArrayList (java.util.ArrayList)13 TreeNode (hatecode._0001_0999.TreeNode)11 Stack (java.util.Stack)7 TreeNode (org.apache.myfaces.custom.tree2.TreeNode)6 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)3 List (java.util.List)3 TreeNodeBase (org.apache.myfaces.custom.tree2.TreeNodeBase)3 Account (com.autentia.tnt.businessobject.Account)1 AccountEntry (com.autentia.tnt.businessobject.AccountEntry)1 AccountEntryType (com.autentia.tnt.businessobject.AccountEntryType)1 Activity (com.autentia.tnt.businessobject.Activity)1 AdminHoliday (com.autentia.tnt.businessobject.AdminHoliday)1 Bill (com.autentia.tnt.businessobject.Bill)1 Book (com.autentia.tnt.businessobject.Book)1 BulletinBoard (com.autentia.tnt.businessobject.BulletinBoard)1 BulletinBoardCategory (com.autentia.tnt.businessobject.BulletinBoardCategory)1 CompanyState (com.autentia.tnt.businessobject.CompanyState)1