# c语言二叉树中序非递归遍历算法,二叉树中序遍历非递归算法（不用栈）

struct AvlTreeNode* AvlTreeIterateHead(struct AvlTree* tree)

{

struct AvlTreeNode* currentNode = tree->m_rootNode;

if(currentNode != tree->m_nullNode)

{

// find the smallest node

while(currentNode->m_child1 != tree->m_nullNode)

currentNode = currentNode->m_child1;

return currentNode;

}

return NULL;

}

struct AvlTreeNode* AvlTreeIterateTail(struct AvlTree* tree)

{

struct AvlTreeNode* currentNode = tree->m_rootNode;

if(currentNode != tree->m_nullNode)

{

// find the greatest node

while(currentNode->m_child2 != tree->m_nullNode)

currentNode = currentNode->m_child2;

return currentNode;

}

return NULL;

}

struct AvlTreeNode* AvlTreeIterateNext(

struct AvlTree* tree,

struct AvlTreeNode* node)

{

struct AvlTreeNode* currentNode;

if(node->m_child2 != tree->m_nullNode)

{

// look for right child's smallest node

currentNode = node->m_child2;

while(currentNode->m_child1 != tree->m_nullNode)

currentNode = currentNode->m_child1;

return currentNode;

}

else if(node->m_parent != tree->m_nullNode)

{

currentNode = node;

// the node has no right child, look back

while(currentNode != tree->m_rootNode)

if(currentNode->m_parent->m_child2 != currentNode)

return currentNode->m_parent;

else currentNode = currentNode->m_parent;

}

return NULL;

}

struct AvlTreeNode* AvlTreeIteratePrev(

struct AvlTree* tree,

struct AvlTreeNode* node)

{

struct AvlTreeNode* currentNode;

if(node->m_child1 != tree->m_nullNode)

{

currentNode = node->m_child1;

// look for left child's greatest node

while(currentNode->m_child2 != tree->m_nullNode)

currentNode = currentNode->m_child2;

return currentNode;

}

else if(node->m_parent != tree->m_nullNode)

{

currentNode = node;

// the node has no left child, look back

while(currentNode != tree->m_rootNode)

if(currentNode->m_parent->m_child1 != currentNode)

return currentNode->m_parent;

else currentNode = currentNode->m_parent;

}

return NULL;

}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
10-06 6941
10-23 5607
02-03 796
11-30
12-14 1万+
12-30 257

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。