Data Structure interview 6-common Binary Tree operations 2 (non-recursive traversal & Binary sorting tree)

Source: Internet
Author: User
Data Structure interview 6-common operations of binary tree 2 (Non-recursive Traversal & Binary sorting tree)

Note: The interview book has related exercises, but the ideas are relatively unclear and the layout is incorrect. The author has rewritten the related books and opinions for your reference.

6. Basic Binary Tree operations (non-recursive traversal) & Binary Tree operations

Next, we will analyze the non-recursive traversal of Binary Trees and the operations of binary tree.

1. Non-recursive sequential Traversal

// 1. Add the left subtree of the root node to the stack in sequence until lchild = NULL. Execute 2

// 2. Stack elements are routed out of the stack and accessed; the current pointer is directed to the node's rchild and traversed cyclically. Until the stack is empty!

Template <typenameelemtype> voidbinarytreetype <elemtype>: norecursioninordertraversal () // non-recursive sequential traversal {cout <"norecursioninordertraversal traversal>" <Endl; revoke stacktype <nodetype <elemtype> *> stack; nodetype <elemtype> * Current = root; while (current! = NULL |! Stack. isemptystack () // or | {If (current! = NULL) {stack. push (current); current = Current-> llink;} else {stack. pop (current); cout <Current-> info <"\ t"; // access the node current = Current-> rlink ;}} cout <Endl; cout <"<---------------------- norecursioninordertraversal" <Endl ;}

2. Non-recursive first-order traversal

// The access order changes based on the middle-order traversal;

// First traverse the root node one by one, and then process the left and right child nodes in sequence.

Template <typenameelemtype> voidbinarytreetype <elemtype>: norecursionpreordertraversal () // non-recursive pre-order traversal {cout <"norecursionpreordertraversal traversal>" <Endl; revoke stacktype <nodetype <elemtype> *> stack; nodetype <elemtype> * Current = root; while (current! = NULL |! Stack. isemptystack () // or | {If (current! = NULL) {cout <Current-> info <"\ t"; // access the node first and then go to the stack. push (current); current = Current-> llink;} else {stack. pop (current); current = Current-> rlink ;}} cout <Endl; cout <"<---------------------- norecursionpreordertraversal" <Endl ;}

3. Non-recursive post-order traversal

Because the access order is first the left subtree, then the right subtree, And the last root node. The preceding operations are performed on each node. Therefore, for traversal, You need to identify that the current node type is root (relative), left child node, and right child node. Therefore, we set the flag Flag variable, flag = 0 initial flag, the node has not been written into the stack; before accessing the left child, set the flag to 1; set the flag to 2 before accessing the right child, and set the flag to 0 after accessing the right child.

// Non-recursive post-order traversal is complicated ..

Template <typenameelemtype> voidbinarytreetype <elemtype>: norecursionpostordertraversal () // non-recursive post-order traversal {cout <"norecursionpostordertraversal iterator>" <Endl; linkedstacktype <nodetype <elemtype> *> stack; linkedstacktype <int> intstack; // tag the synchronization stack. nodetype <elemtype> * Current = root; intnflag = 0; // The initial flag is 0. if (current = NULL) {cout <"the stack is empty! "<Endl;} else {// 1. add the first node to the stack. push (current); intstack. push (1); current = Current-> llink; // note that you need to adjust the point to ***** while (! Stack. isemptystack ()&&! Intstack. isemptystack () {If (current! = NULL & nflag = 0) {stack. push (current); intstack. push (1); // The flag bit is 1. [set the value to 1 before accessing the left child.] Current = Current-> llink;} else {stack. pop (current); intstack. pop (nflag); // the flag at this time is the returned value. You need to determine if (nflag = 1) Based on the returned value. // The next step is to stack the right child. {stack. push (current); // continue to stack the node, intstack. push (2); // But [set it to 2 before accessing the right child]. Current = Current-> rlink; // access the right node, nflag = 0; // set the flag to 0} else {cout <Current-> info <""; // wait until the left and right subtree are empty before accessing the node. }}} Cout <Endl; cout <"<------------------------ norecursionpostordertraversal" <Endl ;}}

 

4. Search operations on the binary sorting tree

Clear concepts. The next three concepts mentioned in domestic and foreign books are equivalent., Binary search tree = Binary Search Tree = binary sorting tree.

// The binary sorting tree can be searched in the following situations:

// 1. The linked list is empty, prompting and returning;

// 2. The linked list is not empty and needs to be searched cyclically until the pointer is null. If so, bfound = true; otherwise, bfound = default false is found.

Template <class elemtype> boolbsearchtreetype <elemtype>: Search (const elemtype & searchitem) {nodetype <elemtype> * Current = new nodetype <elemtype>; boolbfound = false; if (root = NULL) {cout <"The bsearchtree is null \ n"; // case1: The linked list is empty! Returnfalse;} else {current = root; while (current! = NULL &&! Bfound) // case2: searches in the linked list and locks the left and right subtree based on its size. {If (current-> info = searchitem) {bfound = true;} elseif (current-> info> searchitem) {current = Current-> llink; // left subtree} elseif (current-> info <searchitem) {current = Current-> rlink; // right subtree }}returnbfound ;}

5. The insertion of the binary sorting tree has the following situations:

// 1. The linked list is empty, and the inserted element is the root node;

// 2. The linked list is not empty. You need to find the insert position and insert it.

// 2.1 If the inserted element already exists, an error is prompted.

// 2.2 always finds a location greater than or less than a node and records trailcurrent to complete the insert operation.

Template <class elemtype> voidbsearchtreetype <elemtype>: insert (const elemtype & insertitem) {nodetype <elemtype> * newnode = new nodetype <elemtype>; nodetype <elemtype> * current; nodetype <elemtype> * trailcurrent; newnode-> info = insertitem; newnode-> llink = NULL; newnode-> rlink = NULL; If (root = NULL) {root = newnode; // case1: the tree is empty .} else {current = root; while (current! = NULL) // case2, search for 3 or 4! {Trailcurrent = current; If (current-> info = insertitem) {cout <"The ELEM is already exist! \ N "; // case2: the element already has return;} else {If (current-> info> insertitem) {current = Current-> llink; // case3: lock the left position ...} else {current = Current-> rlink; // case4: Lock the right position ...}}} // endwhile // case3, 4 link based on size if (trailcurrent-> info <insertitem) {trailcurrent-> rlink = newnode ;} else {trailcurrent-> llink = newnode;} // end else}

6.The deletion of the binary sorting tree may be complicated in the following situations ]:

// To delete a node, you must first determine whether the element value exists in the binary sorting tree,

// If not, return;

// If it exists, you need to lock the corresponding location as one node, two leaf nodes, and three other nodes.

// Based on whether or not the node to be deleted contains the left and right subtree, it can be divided into four situations,

// See the deletefromtree () function.

Template <class elemtype> voidbsearchtreetype <elemtype>: deletenode (const elemtype & deleteitem) {// 1. no node found/2.1 found, does not exist; // 2.2 found, delete, call the nodetype <elemtype> * Current; nodetype <elemtype> * trailcurrent; boolbfound = false; if (root = NULL) {cout <"can't delete an empty BST" <Endl; return;} else {current = root; trailcurrent = root; while (current! = NULL &&! Bfound) {If (current-> info = deleteitem) {bfound = true;} elseif (current-> info> deleteitem) {trailcurrent = current; current = Current-> llink; // left} else {trailcurrent = current; current = Current-> rlink; // right} // endwhile if (current = NULL) {cout <deleteitem <"is not exist in the BST! \ N "<Endl;} elseif (bfound) {If (current = root) {deletefromtree (Root ); // It may be the root node} elseif (trailcurrent-> info> deleteitem) {deletefromtree (trailcurrent-> llink); // The left half branch, adjust the direction of trailcurrent} elseif (trailcurrent-> info <deleteitem) {deletefromtree (trailcurrent-> rlink); // right branch, adjust the trailcurrent direction} // endif bfound} // end else}

// [Principle]: The frontend of a node is the rightmost node of the Left subtree of the node (result of Middle-order traversal)

Template <class elemtype> voidbsearchtreetype <elemtype>: deletefromtree (nodetype <elemtype> * & P) {nodetype <elemtype> * temp; nodetype <elemtype> * current; nodetype <elemtype> * trailcurrent; If (P = NULL) {cout <"the BST is null! "<Endl; return;} If (p-> llink = NULL & P-> rlink = NULL) // Case 1, left and right nodes are empty (leaf nodes) {temp = P; P = NULL; deletetemp;} elseif (p-> rlink = NULL) // Case 2, the right subtree is empty, left non-empty {temp = P; P = temp-> llink; deletetemp;} elseif (p-> llink = NULL) // case 3, left subtree is empty, right non-empty {temp = P; P = temp-> rlink; deletetemp;} else // case 4, left and right are not empty [Replace with the previous node in the middle order traversal] {current = p-> llink; trailcurrent = NULL; while (current-> rlink! = NULL) {trailcurrent = current; // trailcurrent finally points to the previous node to be deleted, current = Current-> rlink;} p-> info = Current-> Info; // information value assignment if (trailcurrent = NULL) // only one left child node {P-> rlink = Current-> llink ;} else {trailcurrent-> rlink = Current-> llink; // adjust the pointer to} deletecurrent ;}}

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.