前面,我們介紹了樹的基本操作,這幾篇都是資料結構小白必會的東西,今天再分享一篇
#include #include using namespace std;
typedef int elemtype;
struct tree
;struct node
;struct queue
;tree *creatbinarytree();
void printbinarytree(tree *t);
queue creatqueue();
bool isempty(queue *q);
void addqueue(queue *q,tree *n);
tree *deletequeue(queue *q);
//二叉搜尋樹
tree *find(tree *bst,elemtype elem);
tree *findmax(tree *bst);
tree *findmin(tree *bst);
tree *insert(tree *bst,elemtype elem);
tree *delete(tree *bst,elemtype elem);
int main()
case 5:
} }
return 0;
}tree *creatbinarytree()//先序建立
else
return t;
}void printbinarytree(tree *t)//層次遍歷
}queue creatqueue()
bool isempty(queue *q)
void addqueue(queue *q,tree *elem)
}tree *deletequeue(queue *q)
else
return null;
}tree *find(tree *bst,int elem)
else if(elem>bst->elem)
else
*/ while(bst)
else if(elem>bst->elem)
else
}return null;
}tree *findmax(tree *bst)
else return null;
*/ if(bst)
while(bst->rchild)
return bst;
}tree *findmin(tree *bst)
/* if(bst)
while(bst->lchild)
return bst;*/}
tree *insert(tree *bst,elemtype elem)
if(elemelem)//左
if(elem>bst->elem)//右
return bst;
}tree *delete(tree *bst,elemtype elem)
else if(elem>bst->elem)
else
else
}} return bst;
}
二叉搜尋樹的查詢,新增和刪除
main.cpp niukewang created by on 2019 8 17.include include using namespace std 二叉搜尋樹的查詢,新增和刪除 struct treenode 二叉搜尋樹的新增 treenode insert treenode bst,in...
二叉搜尋樹的插入,查詢,刪除
include using namespace std template class node 預設析構函式 templatenode node template node node t value template class bstree 預設析構 void buildbstree 建立二叉樹 ...
二叉搜尋樹的查詢 插入 刪除
coding utf 8 time 2020 9 23 15 56 author julyli file bst.py class bitreenode def init self,data self.data data self.lchild none self.rchild none self....