(三)資料結構 二叉樹的查詢

2021-06-08 19:10:20 字數 2431 閱讀 2167

二叉查詢樹的一些實現,一些心得都嵌入其中的注釋

#include "iostream"

using namespace std;

//因為查詢樹左小右大

template class binarysearchtree

//結點複製構造時使用

}; binarynode *root;

void insert(const t &x, binarynode * &root) const; //指向引用的指標

void remove(const t &x, binarynode * &root) const; //當要進行記憶體分配的時候,利用new 或者malloc 時用 "* &t"

binarynode *findmin(binarynode *root) const;

binarynode *findmax(binarynode *root) const;

bool contains(const t &x, binarynode *root) const;

void makeempty(binarynode * &root);

void printtree(binarynode * root) const;

binarynode *clone(binarynode *root) const; //複製返回的根節點

};template binarysearchtree::binarysearchtree()

template binarysearchtree::binarysearchtree(const binarysearchtree &rhs)

template binarysearchtree::~binarysearchtree()

template const binarysearchtree&binarysearchtree::operator=(const binarysearchtree &rhs)

template bool binarysearchtree::isempty() const

template const t &binarysearchtree::findmin()const

template const t &binarysearchtree::findmax()const

//宣告這個是型別typename

template typename binarysearchtree::binarynode *binarysearchtree::findmin(binarynode *root) const

else

return root;

} else

return root;

}template typename binarysearchtree::binarynode *binarysearchtree::findmax(binarynode *root) const

else

return root;

} else

return root;

}template bool binarysearchtree::contains(const t &x)

template bool binarysearchtree::contains(const t &x, binarynode *root) const

template void binarysearchtree::insert(const t &x)

template void binarysearchtree::insert(const t &x, binarynode * &root) const //指向引用的指標

if(root && x !=root->element)

else

return;

}template void binarysearchtree::remove(const t &x)

//刪除,要的那個結點,若左存在,從他的作結點中找出中序遍歷的最後乙個結點,替換這個;若左不存在,則用右替換

template void binarysearchtree::remove(const t &x, binarynode * &root) const //當要進行記憶體分配的時候,利用new 或者malloc 時用 "* &t"

else if(x < root->element)

remove(x, root->leftchild);

else if(null != root->leftchild && null != root->rightchild)

else

}}template void binarysearchtree::makeempty()

template void binarysearchtree::makeempty(binarynode * &root)

}int main()

資料結構 二叉樹 反轉二叉樹

include using namespace std define maxsize 1000 struct binary tree node class queue queue queue void queue push binary tree node btn binary tree node ...

資料結構之二叉樹的查詢

二叉樹的查詢方式有三種,分別是先序中序和後序。先序思路 判斷當前的節點中的no和傳過來要查詢的是否一致 相等返回 不相等判斷當前節點左子節點是否為空 如果不為空 繼續比較 查詢返回 如果 為空 或者沒有找到 檢視右子節點 查到返回否則返回null 中序思路 判斷當前的左子節點是否為空,不為空遞迴查詢...

《資料結構》 二叉樹

二叉樹 是 n個結點的有限集,它或為空集,或由乙個根結點及兩棵互不相交的 分別稱為該根的左子樹和右子樹的二叉樹組成。二叉樹不是樹的特殊情況,這是兩種不同的資料結構 它與無序樹和度為 2的有序樹不同。二叉樹的性質 1 二叉樹第 i層上的結點數最多為 2 i 1 2 深度為 k的二叉樹至多有 2 k 1...