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个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值