use of java.util.Stack in project Algorithms by pedrovgs.
the class BinaryTreePostOrder method getIterative.
public List<BinaryNode> getIterative(BinaryNode root) {
validateTree(root);
List<BinaryNode> result = new LinkedList<BinaryNode>();
Stack<BinaryNode> stack = new Stack<BinaryNode>();
stack.push(root);
BinaryNode prev = null;
while (!stack.empty()) {
BinaryNode current = stack.peek();
//keep going down
if (prev == null || prev.getLeft() == current || prev.getRight() == current) {
//prev == null is the situation for the root node
if (current.getLeft() != null) {
stack.push(current.getLeft());
} else if (current.getRight() != null) {
stack.push(current.getRight());
} else {
stack.pop();
result.add(current);
}
//Go up the tree from left node need to check if there is a right child
//if yes, push it to stack otherwise, process parent and pop stack
} else if (current.getLeft() == prev) {
if (current.getRight() != null) {
stack.push(current.getRight());
} else {
stack.pop();
result.add(current);
}
//Go up the tree from right node after coming back from right node, process parent node
//and pop stack.
} else if (current.getRight() == prev) {
stack.pop();
result.add(current);
}
prev = current;
}
return result;
}
use of java.util.Stack in project graphdb by neo4j-attic.
the class TreeGraphTest method testBreadthFirst.
@Test
public void testBreadthFirst() throws Exception {
Traverser traverser = Traversal.description().breadthFirst().traverse(referenceNode());
Stack<Set<String>> levels = new Stack<Set<String>>();
levels.push(new HashSet<String>(Arrays.asList("5", "6", "7", "8", "9", "A", "B", "C", "D")));
levels.push(new HashSet<String>(Arrays.asList("2", "3", "4")));
levels.push(new HashSet<String>(Arrays.asList("1")));
assertLevels(traverser, levels);
}
use of java.util.Stack in project jaggery by wso2.
the class LogHostObject method jsConstructor.
/**
* Creates a new log object for the requested resource.
* logger name will be the resource name separated by a . (dot)
* i.e if resource is /foo/bar/mar.jag
* the loger name will be
* JAGGERY.foo.bar.mar
* <p/>
* by default the log level is set to debug
*
* @param cx
* @param args
* @param ctorObj
* @param inNewExpr
* @return
* @throws ScriptException
*/
public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
int argsCount = args.length;
if (argsCount > 1) {
HostObjectUtil.invalidNumberOfArgs(HOSTOBJECT_NAME, HOSTOBJECT_NAME, argsCount, true);
}
String loggerName;
JaggeryContext context = (JaggeryContext) RhinoEngine.getContextProperty(EngineConstants.JAGGERY_CONTEXT);
if (argsCount == 1 && (args[0] instanceof String)) {
loggerName = (String) args[0];
} else {
String requestString = ((Stack<String>) context.getProperty(JAGGERY_INCLUDES_CALLSTACK)).peek();
loggerName = ROOT_LOGGER + requestString.replace(".jag", ":jag").replace(".js", ":js").replace("/", ".");
}
LogHostObject logObj = new LogHostObject();
Logger currentLogger = Logger.getLogger(loggerName);
String appLogLevel = (String) context.getProperty(LOG_LEVEL);
if (currentLogger.getLevel() == null) {
currentLogger.setLevel(Level.toLevel(appLogLevel));
}
logObj.logger = currentLogger;
return logObj;
}
use of java.util.Stack in project android_frameworks_base by ResurrectionRemix.
the class NotificationHeaderViewWrapper method updateCropToPaddingForImageViews.
/**
* Since we are deactivating the clipping when transforming the ImageViews don't get clipped
* anymore during these transitions. We can avoid that by using
* {@link ImageView#setCropToPadding(boolean)} on all ImageViews.
*/
private void updateCropToPaddingForImageViews() {
Stack<View> stack = new Stack<>();
stack.push(mView);
while (!stack.isEmpty()) {
View child = stack.pop();
if (child instanceof ImageView) {
((ImageView) child).setCropToPadding(true);
} else if (child instanceof ViewGroup) {
ViewGroup group = (ViewGroup) child;
for (int i = 0; i < group.getChildCount(); i++) {
stack.push(group.getChildAt(i));
}
}
}
}
use of java.util.Stack in project OpenAM by OpenRock.
the class EntityManager method execute.
/**
* This method will use methods provided by ConfigManager to get "Structure
* Template" information from the DIT. It will use this information to
* create the "Structural Entity".
*
* @param principal
* The Principal.
* @param pObject
* The persistent object for which the entities apply.
* @param pGUID
* The guid of the parent object.
* @throws UMSException
* if an exception occurs.
*/
public void execute(java.security.Principal principal, PersistentObject pObject, Guid pGUID) throws UMSException {
String className;
HashMap hm = null;
Set set = null;
Iterator iter = null;
Attr attr = null;
AttrSet attrSet = null;
String[] attrValues;
if (pObject == null) {
String msg = i18n.getString(IUMSConstants.PERSISTENT_OBJECT_PARAM_NULL);
throw new UMSException(msg);
}
_principal = principal;
_pObject = pObject;
_stack = new Stack();
className = _pObject.getClass().getName();
_parentObject = _pObject;
if (debug.messageEnabled()) {
debug.message("GETTING ENTITY FOR:CLASS:" + className + ",PARENT:" + pGUID.getDn());
}
try {
set = _configManager.getEntity(pGUID, className);
if (!set.isEmpty()) {
if (set.size() > 1) {
attrSet = findEntity(_pObject, set);
} else {
Iterator it = set.iterator();
if (it.hasNext())
attrSet = (AttrSet) it.next();
}
} else
return;
if (attrSet == null) {
String[] args = new String[1];
args[0] = className;
String msg = i18n.getString(IUMSConstants.STRUCTURE_TEMPLATE_ATTRSET_NULL, args);
throw new UMSException(msg);
}
} catch (ConfigManagerException cme) {
String[] args = new String[1];
args[0] = cme.getMessage();
String msg = i18n.getString(IUMSConstants.CONFIG_MGR_ERROR, args);
throw new UMSException(msg);
}
if (debug.messageEnabled()) {
debug.message("ENTITY ATTRSET:" + attrSet);
}
attr = attrSet.getAttribute(ENTITY_CHILDNODE);
if (attr == null) {
return;
}
attrValues = attr.getStringValues();
for (int i = 0; i < attrValues.length; i++) {
hm = new HashMap();
hm.put(attrValues[i], _parentObject.getGuid());
_stack.push(hm);
}
while (!_stack.empty()) {
hm = (HashMap) _stack.pop();
set = hm.keySet();
iter = set.iterator();
String childNodeName = (String) iter.next();
Guid parentGuid = (Guid) hm.get(childNodeName);
try {
Set childSet = _configManager.getEntity(pGUID, childNodeName);
if (!childSet.isEmpty()) {
iter = childSet.iterator();
if (iter.hasNext())
attrSet = (AttrSet) iter.next();
}
if (childSet.isEmpty() | attrSet == null)
return;
} catch (ConfigManagerException cme) {
String[] args = new String[1];
args[0] = cme.getMessage();
String msg = i18n.getString(IUMSConstants.CONFIG_MGR_ERROR, args);
throw new UMSException(msg);
}
// Create Object
//
PersistentObject pObj = createObject(attrSet, parentGuid, pGUID);
attr = attrSet.getAttribute(ENTITY_CHILDNODE);
if (attr != null) {
attrValues = attr.getStringValues();
for (int j = 0; j < attrValues.length; j++) {
hm = new HashMap();
hm.put(attrValues[j], pObj.getGuid());
_stack.push(hm);
}
}
}
}
Aggregations