-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTreeWithPointer.cpp
55 lines (42 loc) · 1.07 KB
/
binaryTreeWithPointer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
*指针实现二叉树
*/
template<class T>
bool BinaryTree<T>::isEmpty() const{
return ((root)?true:false);
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::getParent(BinaryTreeNode<T>* root,
BinaryTreeNode<T>* current){
BinaryTreeNode<T>* temp;
if(root==NULL)
return null;
if((root->leftChild()==current ||root->rightChild())
return root;
//递归找父节点
if(temp=getParent(root->leftChild(), current)!=NULL
return temp;
else
return getParent(root->rightChild(), current);
}
/////////////////////////////////////////////
template<class T>
void BinaryTree<T>::deleteBinaryTree(BinaryTreeNode<T>* root)
{
if(root){
deleteBinaryTree(root->left);
deleteBinaryTree(root->right);
delete root;
}
}
template<class T>
void BinaryTree<T>::createTree(
const T& elem,
BinaryTree<T>& leftTree,
BinaryTree<T>& rightTree){
//elem root node
//leftTree: left child tree
//rightTree: right child tree
root=new BinaryTreeNode(elem, leftTree.root, rightTree.root);
leftTree.root=rightTree.root=NULL;
}