二叉查詢樹

2021-06-09 23:58:39 字數 3363 閱讀 4660

#ifndef binarysearchtree_h

#define binarysearchtree_h

#include using namespace std;

templateclass treenode

treenode(t value) // constructor

};template < typename t >

class binarysearchtree

;template < typename t >

binarysearchtree::binarysearchtree()

template < typename t >

binarysearchtree::binarysearchtree(t values,int arraysize)

}template int binarysearchtree::insert(t value)

else

else if (value > current->value)

else

else if(value > former->value)

} treesize++;

return 0;

}template treenode* binarysearchtree::search(t searchvalue)

else if(current->value > searchvalue)else

} if (find == 1)else

}template int binarysearchtree::deletenode(t value)

this->deletenode(delnode);

cout << "node "<< value <<" has been deleted."<< endl;

return 0;

}template int binarysearchtree::deletenode(treenode*delnode)else if(delnode->left !=null)else if(delnode->right !=null)

treenode*deletetargetchild = null;

if (deletetarget->left != null)else if (deletetarget->right != null)

if (deletetargetchild != null)

if (deletetarget->parent == null)else if ( deletetarget->parent->left == deletetarget)else

if (deletetarget != delnode)

treesize--;

return 0;

}template int binarysearchtree::successor(t value)

treenode*successornode = this->successor(position);

if ( successornode != null)

cout << value << " \'s successor is:" << successornode->value << endl;

else

cout << value << " has no successor" << endl;

return 0;

}template treenode* binarysearchtree::successor(treenode*target)

treenode* parentnode =target->parent;

while ( parentnode != null && parentnode->right == target)

return parentnode;

}template int binarysearchtree::predecessor(t value)

treenode*predecessornode = this->predecessor(position);

if ( predecessornode != null)

cout << value << " \'s predecessor is:" << predecessornode->value << endl;

else

cout << value << " has no predecessor" << endl;

return 0;

}template treenode* binarysearchtree::predecessor(treenode*target)

treenode* parentnode =target->parent;

while ( parentnode != null && parentnode->left == target)

return parentnode;

}template void binarysearchtree::maxvalue()

template treenode* binarysearchtree::maxvalue(treenode*target)

return target;

}template void binarysearchtree::minvalue()

template treenode* binarysearchtree::minvalue(treenode*target)

return target;

}template int binarysearchtree::getsize(t value)

template int binarysearchtree::getsize(treenode*target)

if (target->left == null && target->left == null)else

}template void binarysearchtree::inorder()

template void binarysearchtree::inorder(treenode*target)

template void binarysearchtree::inordernorrec()

template void binarysearchtree::inordernorrec(treenode*target)

else

}}template void binarysearchtree::output()

template void binarysearchtree::output(treenode*target,int totalspaces)

else

output(target->left,totalspaces+4);

}};#endif

二叉樹 二叉查詢樹

構建二叉樹,判斷是否為二叉查詢樹,遞迴先序遍歷,非遞迴中序遍歷 include include include include using namespace std 二叉樹結點 struct treenode 鍊錶結點 struct listnode struct tempnodetempnode...

二叉樹 二叉查詢樹

二叉樹 binary tree 一種樹型結構,每個節點最多擁有兩個節點。如下圖 幾種型別的二叉樹 1.full binary tree 每個節點的孩子數 是 0 或者 2.對高度沒有要求。如下圖 2.perfect binary tree 這個就是最完美的樹,顧名思義,所有葉子節點都有相同的深度,並...

樹(樹,二叉樹,二叉查詢樹)

1.定義 n n 0 個結點構成的有限集合。當n 0時,稱為空樹 2.對於任一棵非空樹 n 0 它具備以下性質 1 樹中有乙個稱為 根 root 的特殊結點,用 r 表示 2 其餘結點可分為m m 0 個互不相交的有限集t1,t2,其中每個集合本身又是一棵樹,稱為原來樹的子樹。3.樹的一些性質 1 ...