Binary Search Tree to ordered two-way linked list

Source: Internet
Author: User

[Cpp] view plaincopyprint? // A two-way linked list in which the binary search tree is converted to an ordered table. cpp: Defines the entry point for the console application.
//
// Question: Convert the binary search tree into a sorted two-way linked list
// Requirement: enter a binary search tree and convert it into a sorted two-way linked list.
// You cannot create any new node. You only need to adjust the pointer.
/* Convert the binary search tree below
10
/\
6 14
/\/\
4 8 12 16

4 = 6 = 8 = 10 = 12 = 14 = 16 this two-way linked list

*/
 
/*
What is a binary search tree?
Binary Search Tree: it is a binary tree first. On this basis, it is also an empty tree;
Or a binary tree with the following properties:
(1) If the left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(3) left and right subtree are also binary search trees.
How to construct a binary search tree?
Similar to the common binary tree construction method, the preceding conditions must be met.
Apply Recursion TO element insertion to find the proper insert position
Define Tree node Structure
Struct BSTreeNode
{
Int value;
BTreeNode * left;
BTreeNode * right;
}
Idea: through observation, we can find that the result of the Middle-order traversal of the Binary Search Tree is an ordered series,
In this way, you only need to traverse the tree in a central order and adjust the pointer to become a two-way linked list.
*/
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Struct BSTreeNode
{
Int value;
BSTreeNode * left;
BSTreeNode * right;
};
BSTreeNode * pHead = NULL; // point to the two-way linked list header Node
BSTreeNode * pIndex = NULL; // Save the previous node of the current Access Node
// For example, if the current access node is 6, pIndex points to 4.
Void AddBSTreeNode (BSTreeNode ** pRoot, int value)
{
If (NULL = * pRoot)
{
BSTreeNode * tmpNode = new BSTreeNode;
TmpNode-> value = value;
TmpNode-> left = NULL;
TmpNode-> right = NULL;
* PRoot = tmpNode;
}
Else if (* pRoot)-> value <value)
{
AddBSTreeNode (& (* pRoot)-> right, value );
}
Else if (* pRoot)-> value)
{
AddBSTreeNode (& (* pRoot)-> left, value );
}
}
// Traverse in the middle order and adjust the node pointer at the same time
Void InOrderAdjust (BSTreeNode * pBSTree)
{
If (NULL = pBSTree)
Return;
// Recursively access the left child
If (NULL! = PBSTree-> left)
InOrderAdjust (pBSTree-> left );
// Adjust the node pointer

PBSTree-> left = pIndex; // point the left pointer of the currently accessed node to the previous node.
If (NULL = pIndex) // if the previous node is empty, it indicates the first access.
PHead = pBSTree; // The node at this time should be the header node of the two-way linked list.
Else
PIndex-> right = pBSTree; // point the right pointer of the previous node to the current node, which forms a two-way linked list.
PIndex = pBSTree;
// Recursively access the right child
If (NULL! = PBSTree-> right)
InOrderAdjust (pBSTree-> right );
}
// Verify the output result
Void Print (BSTreeNode * pHead)
{
If (pHead = NULL)
Return;
BSTreeNode * pTmp;
Cout <"sequential traversal:" <endl;
While (pHead! = NULL)
{
PTmp = pHead;
Cout <pHead-> value <"";
PHead = pHead-> right;
}
Cout <endl;
Cout <"reverse traversal:" <endl;
While (pTmp! = NULL)
{
Cout <pTmp-> value <"";
PTmp = pTmp-> left;
}
Cout <endl;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
BSTreeNode * pRoot = NULL;
AddBSTreeNode (& pRoot, 10 );
AddBSTreeNode (& pRoot, 6 );
AddBSTreeNode (& pRoot, 14 );
AddBSTreeNode (& pRoot, 4 );
AddBSTreeNode (& pRoot, 8 );
AddBSTreeNode (& pRoot, 12 );
AddBSTreeNode (& pRoot, 16 );
InOrderAdjust (pRoot );
Print (pHead );
System ("pause ");
Return 0;
}

// A two-way linked list in which the binary search tree is converted to an ordered table. cpp: Defines the entry point for the console application.
//
// Question: Convert the binary search tree into a sorted two-way linked list
// Requirement: enter a binary search tree and convert it into a sorted two-way linked list.
// You cannot create any new node. You only need to adjust the pointer.
/* Convert the binary search tree below
10
/\
6 14
/\/\
4 8 12 16

4 = 6 = 8 = 10 = 12 = 14 = 16 this two-way linked list

*/

/*
What is a binary search tree?
Binary Search Tree: it is a binary tree first. On this basis, it is also an empty tree;
Or a binary tree with the following properties:
(1) If the left subtree is not empty, the value of all nodes on the left subtree is smaller than the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(3) left and right subtree are also binary search trees.
How to construct a binary search tree?
Similar to the common binary tree construction method, the preceding conditions must be met.
Apply Recursion TO element insertion to find the proper insert position
Define Tree node Structure
Struct BSTreeNode
{
Int value;
BTreeNode * left;
BTreeNode * right;
}
Idea: through observation, we can find that the result of the Middle-order traversal of the Binary Search Tree is an ordered series,
In this way, you only need to traverse the tree in a central order and adjust the pointer to become a two-way linked list.
*/
# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Struct BSTreeNode
{
Int value;
BSTreeNode * left;
BSTreeNode * right;
};
BSTreeNode * pHead = NULL; // point to the two-way linked list header Node
BSTreeNode * pIndex = NULL; // Save the previous node of the current Access Node
// For example, if the current access node is 6, pIndex points to 4.
Void AddBSTreeNode (BSTreeNode ** pRoot, int value)
{
If (NULL = * pRoot)
{
BSTreeNode * tmpNode = new BSTreeNode;
TmpNode-> value = value;
TmpNode-> left = NULL;
TmpNode-> right = NULL;
* PRoot = tmpNode;
}
Else if (* pRoot)-> value <value)
{
AddBSTreeNode (& (* pRoot)-> right, value );
}
Else if (* pRoot)-> value)
{
AddBSTreeNode (& (* pRoot)-> left, value );
}
}
// Traverse in the middle order and adjust the node pointer at the same time
Void InOrderAdjust (BSTreeNode * pBSTree)
{
If (NULL = pBSTree)
Return;
// Recursively access the left child
If (NULL! = PBSTree-> left)
InOrderAdjust (pBSTree-> left );
// Adjust the node pointer
 
PBSTree-> left = pIndex; // point the left pointer of the currently accessed node to the previous node.
If (NULL = pIndex) // if the previous node is empty, it indicates the first access.
PHead = pBSTree; // The node at this time should be the header node of the two-way linked list.
Else
PIndex-> right = pBSTree; // point the right pointer of the previous node to the current node, which forms a two-way linked list.
PIndex = pBSTree;
// Recursively access the right child
If (NULL! = PBSTree-> right)
InOrderAdjust (pBSTree-> right );
}
// Verify the output result
Void Print (BSTreeNode * pHead)
{
If (pHead = NULL)
Return;
BSTreeNode * pTmp;
Cout <"sequential traversal:" <endl;
While (pHead! = NULL)
{
PTmp = pHead;
Cout <pHead-> value <"";
PHead = pHead-> right;
}
Cout <endl;
Cout <"reverse traversal:" <endl;
While (pTmp! = NULL)
{
Cout <pTmp-> value <"";
PTmp = pTmp-> left;
}
Cout <endl;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
BSTreeNode * pRoot = NULL;
AddBSTreeNode (& pRoot, 10 );
AddBSTreeNode (& pRoot, 6 );
AddBSTreeNode (& pRoot, 14 );
AddBSTreeNode (& pRoot, 4 );
AddBSTreeNode (& pRoot, 8 );
AddBSTreeNode (& pRoot, 12 );
AddBSTreeNode (& pRoot, 16 );
InOrderAdjust (pRoot );
Print (pHead );
System ("pause ");
Return 0;
}
[Cpp] running result:

Running result:

Related Article

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.