Inplementation of Binary Search Tree using iteration-local version 2【1月23日学习笔记】
1.遍历链表,将节点接到末端 【1月16日学习笔记】2.Inserting a node at beginning,全局变量头指针【1月16日学习笔记】3.Inserting a node at beginning,局部变量头指针版本1【1月16日学习笔记】4.Inserting a node at beginning,局部变量头指针版本2【1月16日学习笔记】5.Inserting a node at nth position【1月17日学习笔记】6.Delete d node at nth position【1月17日学习笔记】7.Reverse a linked list【1月17日学习笔记·】8.Print linked list using recursion【1月17日学习笔记】9.Reverse a linked list using recursion【1月17日学习笔记】10.Doubly linked list【1月17日学习笔记】11.Stack-array based implementation【1月17日学习笔记】12.Stack-link list implementation【1月18日学习笔记】13.string reversal using stack【1月19日学习笔记】14.Linked list reversal using stack【1月19日学习笔记】15.Check for balanced parentheses using stack【1月20日学习笔记】16.Evaluation Of postfix Expression using stack【1月21日学习笔记】17.Infix to postfix conversion using stack【1月21日学习笔记】18.Queue-Linked List Implementation【1月22日学习笔记】19.Inplementation of Binary Search Tree using recursion-local version 1【1月22日学习笔记】
20.Inplementation of Binary Search Tree using iteration-local version 2【1月23日学习笔记】
21.Inplementation of Binary Search Tree using recursion-local version 3【1月23日学习笔记】22.Find min and max element in bst using iteration【1月23日学习笔记】23.Find min and max element in bst using recursion【1月23日学习笔记】24.Find height of a binary tree【1月23日学习笔记】25.Binary tree traversal-- level-order traversal using queue(no recursion)【1月23日学习笔记】26.Binary tree traversal-- beadth-first and depth-first【1月23日学习笔记】27.Check if a given binary tree is BST【2月14日学习笔记】28.Delete a node from bst【2月15日学习笔记】点击查看代码
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* left;
Node* right;
};
Node* newNode(int x)
{
Node* temp = new Node;
temp->data = x;
temp->left = temp->right = nullptr;
return temp;
}
/*************************wrong code*********8***************************
void insert(Node** headref, int data)
{
Node* travptr = *headref;
//travptr是局部变量,副本,修改副本指针的指向不会改变原指针
while (travptr != NULL)
{
travptr = (data > travptr->data) ? travptr->right : travptr->left;
}
travptr = newNode(data);
//只能通过指向原指针的另一个指针取修改原指针
}
***************************************************************************/
void insert(Node** headref, int data)//可能跟头指针操作相关
{
if (*headref == NULL)
{
*headref = newNode(data);
return;
}//跟头指针操作相关
//直接修改原指针
Node* travptr = *headref;
Node* run= NULL;//初始化为空
while (travptr != NULL)//当travptr指向NULL时结束,条件更简单
{
run = travptr;
travptr = (data > travptr->data) ? travptr->right : travptr->left;
}//关键是 run,结束时 run指向待插入节点
if (data > run->data)
run->right = newNode(data);
else
run->left = newNode(data);//利用 run修改待插入节点左右link
//只能通过指向原指针的另一个指针取修改原指针
}//时间复杂度:O(logn)in best case(balanced bst)
bool search(Node* travptr, int data)//没有与头指针相关操作,可以用形参(副本)
{
while (travptr != NULL && travptr->data != data)//关注结束时
travptr = (data > travptr->data) ? travptr->right : travptr->left;
//结束情况:travptr指向NULL,或找到目标值
if (travptr == NULL)
{
return false;
}
return true;//判定具体情况
}//时间复杂度:O(logn)in best case(balanced bst)
int main()
{
Node* head = NULL;
insert(&head, 1);
insert(&head, 2);
insert(&head, 3);
insert(&head, 4);
cout << search(head, 3) << "\t" << search(head, 10);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通