日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构树4-二叉搜索树2

發(fā)布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构树4-二叉搜索树2 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

二叉搜索樹.h:
?

#ifndef _二叉查找樹_H #define _二叉查找樹_H #include<iostream> #include<string>enum Boolean{ FALSE,TRUE}; template<class Type> class Element { public:Type key;//方便添加更新數(shù)據(jù) }; // 錯誤我的寫法:template<class Type> BinaryTree;template<class Type> class BinaryTree; //前置聲明 template<class Type> class BinaryNode {friend class BinaryTree<Type>; public:Element<Type> data;BinaryNode* leftChild;BinaryNode* rightChild;void display(int i);};template<class Type> class BinaryTree { private:BinaryNode<Type> * root; public:BinaryTree(BinaryNode<Type> *init = 0){root = init;}Boolean Insert(const Element<Type>& x);BinaryNode<Type>* Search(const Element<Type>& x);BinaryNode<Type>* Search(BinaryNode<Type>*,const Element<Type>&);BinaryNode<Type>* IterSearch(const Element<Type>&);//增加刪除//增加遍歷void display(){cout << "\n";if (root)root->display(1);elsecout << "這是一棵空樹" << "\n";}}; //顯示當前結(jié)點的數(shù)據(jù)及左子樹右子樹中的數(shù)據(jù) template<class Type> void BinaryNode<Type>::display(int i) {std::cout << "Position:" << i << ",key:" << data.key << "\n";if (leftChild) leftChild->display(2 * i);if (rightChild) rightChild->display(2 * i + 1); }//我寫的 //template<class Type> //Boolean BinaryTree<Type>::Insert(const Element<Type>& x) //{ // BinaryNode<Type> *p = root; // BinaryNode<Type> *q =0; //q指向p的父結(jié)點 // if (x == p->data.key) return; // while (p) // { // q = p; // if (x > p->rightChild->data.key) // p = p->rightChild; // else // p = p->leftChild; // } // BinaryNode<Type>* newNode = new BinaryNode<Type>; // newNode->data.key = x; // newNode->rightChild = 0; // newNode->leftChild = 0; // if (x > q->data.key) // q->rightChild = newNode; // else // q->leftChild = newNode; //} template<class Type> Boolean BinaryTree<Type>::Insert(const Element<Type> &x) {BinaryNode<Type> *p = root;BinaryNode<Type> *q = 0; //q指向p的父結(jié)點//insert前先查找while (p){q = p;if (x.key == p->data.key) return FALSE; //發(fā)生重復,失敗返回FALSEif (x.key > p->data.key)p = p->rightChild;else p = p->leftChild;}//找到的位置就是qBinaryNode<Type> *newNode = new BinaryNode<Type>;newNode->data = x;newNode->rightChild = newNode->leftChild = 0;if (!root) root = newNode;else if (x.key > q->data.key)q->rightChild = newNode;elseq->leftChild = newNode;return TRUE; }//我寫的 //template<class Type> //BinaryNode<Type>* BinaryTree<Type>::IterSearch(const Element<Type>& x) //{ // BinaryNode<Type>* node, // node = root; // while (node) 用while錯誤???不知道為什么 // { // if (node->data.key == x) // return node; // if (node->data.key > x) // node = node->rightChild; // else // node = node->leftChild; // } // //} template<class Type> BinaryNode<Type>* BinaryTree<Type>::IterSearch(const Element<Type>& x) {for (BinaryNode<Type>* node = root; node;){if (x.key==node->data.key )return node;if (x.key>node->data.key)node = node->rightChild;elsenode = node->leftChild;}return 0;}//完全不會 template<class Type> BinaryNode<Type>* BinaryTree<Type>::Search(const Element<Type>&x) //查找某個數(shù)--》從root結(jié)點開始的樹中的數(shù) {return Search(root, x); } template<class Type> BinaryNode<Type>* BinaryTree<Type>::Search(BinaryNode<Type>* node, const Element<Type>&x) {if (!node)return 0;else if (x.key == node->data.key) return node;else if (x.key < node->data.key) return Search(node->leftChild, x); elsereturn Search(node->rightChild, x); }#endif

test.h:

#include<iostream> #include<string> #include"二叉查找樹.h"using namespace std; int main() {//我的錯誤寫法 Element<Type> a;BinaryTree<int> m;Element<int> a,b,c,d,e,f,g,h,i,j,k,l;a.key = 5;b.key = 3;c.key = 11;d.key = 3;e.key = 15;f.key = 2;g.key = 8;h.key = 22;i.key = 20;j.key = 9;cout<<m.Insert(a)<<endl;cout<<m.Insert(b)<<endl;cout << m.Insert(c) << endl;cout << m.Insert(d) << endl;cout << m.Insert(e) << endl;cout << m.Insert(f) << endl;cout << m.Insert(g) << endl;cout << m.Insert(h) << endl;cout << m.Insert(i) << endl;cout << m.Insert(j) << endl;m.display();BinaryNode<int> *node=m.IterSearch(f);cout << "node->data.key:" << node->data.key << endl;BinaryNode<int> *node1 = m.Search(e);cout << "node1->data.key:" << node1->data.key << endl;return 0; }

運行結(jié)果:

總結(jié)

以上是生活随笔為你收集整理的数据结构树4-二叉搜索树2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。