#include<iostream> #include<cstdlib> using namespace std; typedef int ElemType; struct Node { ElemType data; Node *left, *right; bool rTag, lTag; }; void initThread(Node* &bt) { bt=NULL; } void insertThread(Node* &bt,const ElemType& item) { Node *child=bt,*parent=NULL; while(child!=NULL) { parent=child; if(item<child->data) if(child->lTag==false) child=child->left; else child=NULL; else if(item>child->data) if(child->rTag==false) child=child->right; else child=NULL; else return; } Node* temp=new Node; temp->data=item; temp->lTag=temp->rTag=true; if(parent==NULL) { temp->left=temp->right=NULL; bt=temp; } else if(item>parent->data) { temp->right=parent->right; parent->rTag=false; parent->right=temp; temp->left=parent; } else { temp->left=parent->left; parent->lTag=false; parent->left=temp; temp->right=parent; } } void printTree(Node* bt) { if (bt != NULL) { cout << bt->data << ' '; if (bt->lTag == false || bt->rTag == false) { cout << '('; if (bt->lTag == false) printTree(bt->left); if (bt->rTag == false) { cout << ','; printTree(bt->right); } cout << ')'; } } } Node* inorderNext(Node* bt) { if (bt->rTag == true) return bt->right; else { bt = bt->right; while (bt->lTag == false) bt = bt->left; return bt; } } void inorderThread(Node* bt) { if (bt != NULL) { while (bt->lTag == false) bt = bt->left; do { cout << bt->data << ' '; bt = inorderNext(bt); } while (bt != NULL); } } void display(Node* bt) { cout << "Tree: "; printTree(bt); cout << endl; cout << "inorder thread: "; inorderThread(bt); cout << endl; } bool deleteThread(Node* &bt,const ElemType& item) { if(bt==NULL) return false; else { if(item>bt->data) if(bt->rTag==false) return deleteThread(bt->right,item); else return false; else if(item<bt->data) if(bt->lTag==false) return deleteThread(bt->left,item); else return false; else { if(bt->lTag==true&&bt->rTag==true) { Node* temp=bt; if((bt->left!=NULL)&&(bt->left->right==temp)) { bt->left->rTag=true; bt->left->right=bt->right; } else if((bt->right!=NULL)&&(bt->right->left=temp)) { bt->right->lTag=true; bt->right->left=bt->left; } else { bt=NULL; } delete temp; return true; } else if(bt->lTag==true&&bt->rTag==false) { Node* temp=bt->right; while(temp->lTag!=true) { temp=temp->left; } bt->data=temp->data; return deleteThread(bt->right,temp->data); } else if(bt->lTag=false&&bt->rTag==true) { Node *temp=bt->left; while(temp->rTag!=true) { temp=temp->right; } bt->data=temp->data; return deleteThread(bt->left,temp->data); } else { if(bt->left->rTag==true) { bt->data=bt->left->data; return deleteThread(bt->left,bt->left->data); } else { Node *parent=bt,*child=bt->left; while(child->rTag!=true) { parent=child; child=child->right; } bt->data=child->data; return deleteThread(parent->right,child->data); } } } } } void clearThread(Node* &bt) { if(bt!=NULL) { if(bt->lTag==false) clearThread(bt->left); if(bt->rTag==false) clearThread(bt->right); delete bt; bt=NULL; } } int main() { ElemType array[] = { 36, 12, 75, 83, 54, 67, 60, 40, 92, 72 }; Node* bt; initThread(bt); for (int i = 0; i < 10; i++) insertThread(bt, array[i]); display(bt); for (int i = 0; i < 10; i++) { cout << "***************** " << array[i] << " ***************" << endl; deleteThread(bt, array[i]); display(bt); } clearThread(bt); }
相关推荐
线索二叉树是一种特殊形式的二叉树,它在二叉链表的基础上增加了线索,用于在树中进行更高效的前序、中序和后序遍历。这种数据结构在计算机科学,尤其是数据结构课程中占有重要地位,因为它能有效地解决非递归遍历的...
线索二叉树可以分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种。在本文中,我们将讨论中序线索二叉树的建立、删除、插入和恢复线索。 一、线索二叉树的建立 线索二叉树的建立可以通过遍历二叉树并将每个...
线索二叉树是一种特殊的二叉树结构,它在二叉链表的基础上增加了线索,以便于在非递归情况下进行前序、中序和后序遍历。在C++中实现线索二叉树需要理解二叉树的基本概念以及链式存储结构。本文将深入探讨线索二叉树...
线索二叉树是一种特殊的二叉树数据结构,它在二叉链表的基础上增加了线索,用于在非递归情况下实现二叉树的前序、中序和后序遍历。这种数据结构在解决某些特定问题时非常有用,比如快速查找、遍历等。接下来我们将...
中序线索二叉树(建立二叉树,线索化,输出二叉树)
线索二叉树作为一种高效的数据结构,其概念由著名计算机科学家严蔚敏教授提出,它对传统的二叉树进行了改良,通过增加线索来提升遍历效率,尤其在需要频繁遍历操作的场景下表现突出。 线索二叉树是在二叉树的基础上...
线索二叉树是一种特殊的二叉树,它是在普通的二叉链表基础上进行改造,以便在二叉树中方便地进行前序、中序和后序遍历。在标准的二叉链表中,每个节点仅包含指向左孩子和右孩子的指针,但没有直接指向其前驱和后继...
线索二叉树是一种特殊的二叉树数据结构,它通过在二叉链表的空指针域中存储指向结点在特定遍历顺序下的前驱和后继结点的指针,从而方便快速的遍历。在本课程设计中,重点在于实现中序线索二叉树的建立、插入、删除...
线索二叉树是一种特殊形式的二叉树,它在二叉链表的基础上增加了线索,用于在不使用额外栈或递归的情况下实现前序、中序和后序遍历。这种数据结构在处理大规模数据时非常有用,因为它可以提高查找效率。下面我们将...
数据结构(C++)上课笔记——线索二叉树的概念与C++实现 线索二叉树是一种特殊的二叉树结构,它在二叉树的基础上添加了线索的概念,使得每个结点除了有左右孩子外,还有指向前驱和后继的指针。这种结构可以提高...
线索二叉树是一种特殊的二叉树,它在二叉链表的基础上增加了线索,使得在非递归情况下也能方便地进行前序、中序和后序遍历。这种数据结构在实际编程中,尤其是在数据结构课程设计中,是学生学习和掌握的重点之一。...
在中序线索二叉树中,我们可以快速地从前一个节点找到后一个节点,而无需通过父节点进行跳转。这一过程涉及到对二叉树节点的修改,添加两个额外的指针,称为“前向线索”(in-order predecessor pointer)和“后向...
线索二叉树是一种特殊形式的二叉树,它在二叉树的基础上增加了线索,以便于在不使用递归的情况下进行二叉树的遍历。在本实验中,我们使用C语言实现了线索二叉树,主要涉及到以下几个核心知识点: 1. **线索二叉树的...
在众多的数据结构类型中,线索二叉树是一种特殊形式的二叉搜索树,主要用于提高对二叉树的遍历效率,特别是对于查找前驱和后继节点。这个主题与严蔚敏教授的教材《数据结构》紧密相关,这是一本在中国计算机科学教育...
线索二叉树是一种在二叉树中添加线索(thread)以方便进行遍历的数据结构,主要应用于不支持随机访问的存储系统中。这种数据结构在实际应用中,尤其是在需要高效遍历二叉树(如前序、中序、后序遍历)时,能提供便利...
线索二叉树是一种在二叉树中添加线索以方便遍历的数据结构,它主要用于实现二叉树的前序、中序和后序遍历。在C语言中,线索二叉树的实现通常涉及到指针的高级操作和结构体的设计。下面我们将详细探讨线索二叉树的...
**线索二叉树**是一种特殊的二叉树结构,它的主要目的是为了方便在二叉树中进行遍历。在普通的二叉树中,我们通常通过递归或者栈来实现前序、中序、后序等遍历方式。但在线索二叉树中,通过在节点的空指针域中存储...