// ###################################### Straight Iteration // this is a left first straight iteration otherwise known as a level order traversal public dblLinked straightItr() { // check that the trunk exists before doing anything if (trunk != null) { dblLinked straightList = new dblLinked(trunk); // if I dont add a least one more node to the list // the loop will end at the start /* * if(trunk.left!= null) * { * straightList.addToEnd(trunk.left); * } * if (trunk.right != null) * { * straightList.addToEnd(trunk.right); * } * /* it looks like a do while works instead of a while loop*/ do { // ok so to make the line of code readablely short lets pull the current // node from the list with a name TreeNode tempTreeNode = (TreeNode)straightList.getData(); // if it has a left node add it to list if (tempTreeNode.left != null) { straightList.addToEnd(tempTreeNode.left); } // ditto for right if (tempTreeNode.right != null) { straightList.addToEnd(tempTreeNode.right); } // go to the next node in the list //straightList.goRight(); } while (straightList.goRight()); // well thats it for the loop // it should should get though them all as it will stopp adding more // at the leaf nodes return(straightList); } else { return(null); } }
// getting just the indexes in order - for reasons // left side iteration or list from lowest to highest private dblLinked indexLeftSideItr(TreeNode checkIt, bool fromChild, dblLinked results) { if (fromChild) { if (checkIt.right == null) { results.addToEnd(checkIt.sortingNum); } if (checkIt.parent != null) { if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum) { results.addToEnd(checkIt.parent.sortingNum); return(indexLeftSideItr(checkIt.parent.right, false, results)); } else { return(indexLeftSideItr(checkIt.parent, true, results)); } } else { return(results); } } // ok so that is if its coming from a kid now if it coming from anywhere else else { if (checkIt.left != null) { return(indexLeftSideItr(checkIt.left, false, results)); } else { results.addToEnd(checkIt.sortingNum); if (checkIt.right != null) { return(indexLeftSideItr(checkIt.right, false, results)); } else { if (checkIt.parent != null) { // I forgot to check for a sister if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum) { results.addToEnd(checkIt.parent.sortingNum); return(indexLeftSideItr(checkIt.parent.right, false, results)); } else { return(indexLeftSideItr(checkIt.parent, true, results)); } } else { return(results); } } } } //return results; }
// left side iteration or list from lowest to highest recording any time we dont go up the tree otherwise known as pre order traversal public dblLinked preOrderItr(TreeNode checkIt, bool fromChild, dblLinked results) { if (fromChild) { /* * if (checkIt.right == null) * { * results.addToEnd(checkIt.ToString()); * }*/ // parent null means we are back at the trunk if (checkIt.parent != null) { // if its coming from the left go parent right(sister) if possible otherwise go up if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum) { // results.addToEnd(checkIt.parent.ToString()); return(preOrderItr(checkIt.parent.right, false, results)); } else { return(preOrderItr(checkIt.parent, true, results)); } } else { // back to trunk so now its over return(results); } } // ok so that is if its coming from a kid now if it coming from anywhere else else { // just record as long as we are not going back up results.addToEnd(checkIt.ToString()); // first try to go down left(son) if not try right(daughter) if (checkIt.left != null) { return(preOrderItr(checkIt.left, false, results)); } else { // results.addToEnd(checkIt.ToString()); //well there was no son(left) so lets see if there was a daughter(right) if (checkIt.right != null) { return(preOrderItr(checkIt.right, false, results)); } else { // no left(son) or right(daughter) so if there is no parent right(sister) to to the parent if (checkIt.parent != null) { // I forgot to check for a sister if (checkIt.parent.right != null && checkIt.sortingNum < checkIt.parent.sortingNum) { // results.addToEnd(checkIt.parent.ToString()); return(preOrderItr(checkIt.parent.right, false, results)); } else { return(preOrderItr(checkIt.parent, true, results)); } } else { // no son, daughter, or parent so we are alone at the trunk and can end return(results); } } } } //return results; }
// ########################################################## // right side iteration or list from Highest to lowest public dblLinked rightSideItr(TreeNode checkIt, bool fromChild, dblLinked results) { if (fromChild) { if (checkIt.left == null) { // results += checkIt.ToString() + "\r\n"; results.addToEnd(checkIt); } if (checkIt.parent != null) { if (checkIt.parent.left != null && checkIt.sortingNum > checkIt.parent.sortingNum) { // results += checkIt.parent.ToString() + "\r\n"; results.addToEnd(checkIt.parent); return(rightSideItr(checkIt.parent.left, false, results)); } else { return(rightSideItr(checkIt.parent, true, results)); } } else { return(results); } } // ok so that is if its coming from a kid now if it coming from anywhere else else { if (checkIt.right != null) { return(rightSideItr(checkIt.right, false, results)); } else { // results += checkIt.ToString() + "\r\n"; results.addToEnd(checkIt); if (checkIt.left != null) { return(rightSideItr(checkIt.left, false, results)); } else { if (checkIt.parent != null) { // I forgot to check for a sister if (checkIt.parent.left != null && checkIt.sortingNum > checkIt.parent.sortingNum) { // results += checkIt.parent.ToString() + "\r\n"; results.addToEnd(checkIt.parent); return(rightSideItr(checkIt.parent.left, false, results)); } else { return(rightSideItr(checkIt.parent, true, results)); } } else { return(results); } } } } //return results; }