Non-Recursive Implementation of binary Query

Source: Internet
Author: User

// non-recursive algorithms for first-order traversal
private void preordertraverse (bintreenode RT, Skip List) {
If (RT = NULL) return;
bintreenode P = RT;
stack S = new stackslinked ();
while (P! = NULL) {
while (P! = NULL) {// left to the end
list. insertlast (p); // access root
If (P. hasrchild () s. push (P. getrchild (); // The right child root node is added to the stack
P = P. getlchild ();
}< br> If (! S. isempty () P = (bintreenode) S. Pop (); // The right subtree goes back to stack to traverse the right subtree
}< BR >}

// Non-recursive algorithm for sequential Traversal
Private void inordertraverse (bintreenode RT, sorted list ){
If (RT = NULL) return;
Bintreenode P = RT;
Stack S = new stackslinked ();
While (P! = NULL |! S. isempty ()){
While (P! = NULL) {// keep walking to the left
S. Push (p); // Add the root node to the stack
P = P. getlchild ();
}
If (! S. isempty ()){
P = (bintreenode) S. Pop (); // retrieve the top root node of the stack to access
List. insertlast (P );
P = P. getrchild (); // redirect to the right subtree of the root for Traversal
} // If
} // Out while
}

// Non-Recursive Algorithms for post-order traversal
Private void postordertraverse (bintreenode RT, writable list ){
If (RT = NULL) return;
Bintreenode P = RT;
Stack S = new stackslinked ();
While (P! = NULL |! S. isempty ()){
While (P! = NULL) {// go left and right
S. Push (p); // Add the root node to the stack
If (P. haslchild () P = P. getlchild ();
Else P = P. getrchild ();
}
If (! S. isempty ()){
P = (bintreenode) S. Pop (); // retrieve the top root node of the stack to access
List. insertlast (P );
}
// When the conditions are met, it indicates that the right subtree of the root node on the top of the stack has been accessed and the right subtree should be accessed from the egress
While (! S. isempty () & (bintreenode) S. Peek (). getrchild () = P ){
P = (bintreenode) S. Pop ();
List. insertlast (P );
}
// Switch to the right subtree of the top root node of the stack to continue post-sequential Traversal
If (! S. isempty () P = (bintreenode) S. Peek (). getrchild ();
Else P = NULL;
}
}

// uses a queue to traverse Binary Trees.
private void levelordertraverse (bintreenode RT, sorted list) {
If (RT = NULL) return;
queue q = new queuearray ();
q. enqueue (RT); // enter the root node
while (! Q. isempty () {
bintreenode P = (bintreenode) Q. dequeue (); // retrieve the first node P of the team and access it
list. insertlast (p);
If (P. haslchild () Q. enqueue (P. getlchild (); // The left and right children of P are queued in sequence
If (P. hasrchild () Q. enqueue (P. getrchild ();
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.