c 二叉搜尋樹的插入 查詢 非遞迴刪除

2021-08-07 11:32:21 字數 3935 閱讀 1424

尤其注意刪除單個根結點、刪除有乙個分支的根結點時要防止記憶體洩露(_root為私有成員時)

#include #include #include using namespace std;

//結點類

template class binarynode

;template binarynode::binarynode()

template binarynode::binarynode(t val)

//二叉樹類

template class binarytree

;//構造空樹

template binarytree::binarytree()

//先序構造二叉樹

template binarytree::binarytree(const t prelist, const int size, int index, const t end)

//析構

template binarytree::~binarytree()

//先序建立二叉樹

template binarynode* binarytree::createbitree(const t prelist,const int size, int &index,const t end) //特別注意:index必須用引用,否則函式的兩個++index將不會正常改變

return root;

}//銷毀二叉樹

template void binarytree::clearbitree(binarynode* root)

clearbitree(tmp->lchild);

clearbitree(tmp->rchild);

delete tmp;

tmp = null;

}//非遞迴遍歷

//1 前序遍歷

template void binarytree::previsitbitree()

while ((cur != null) || (!bitreestack.empty()))

binarynode*top = bitreestack.top();

bitreestack.pop();

cur = top->rchild;

cur->data;

} cout << endl;

}//2 中序遍歷

templatevoid binarytree::midvisitbitree()

binarynode*top = bitreestack.top();

cout << top->data << " ";

bitreestack.pop();

cur = top->rchild;

} cout << endl;

}//3 後序遍歷

templatevoid binarytree::postvisitbitree()

binarynode*top = bitreestack.top();

if((top->rchild == null) || (priview == top->rchild))

else

} cout << endl;

}//4 層序遍歷

templatevoid binarytree::levelvisitbitree()

if(top->rchild != null)

}cout << endl;

}//搜尋二叉樹的插入

templatevoid binarytree::inserttree(int val)

binarynode*cur = _root;

binarynode*parant = null;

binarynode*tmp = new binarynode();

while (cur != null)

else

}tmp->data = val;

if(val < parant->data)

else }

//刪除二叉樹

templatevoid binarytree::deletetree(int val)

binarynode*cur = _root;

binarynode*parent = _root;

int flag = 0;

while (cur != null)

else if(parent->lchild == cur) //左孩子為要刪除的點

else if(parent->rchild == cur) //右孩子為要刪除的點

delete cur;

cur = null;

return;

}else if((null == cur->lchild) || (null == cur->rchild)) //要刪除的點有乙個孩子

else if(cur->rchild != null)

}else if(parent->rchild == cur)

else if(cur->rchild != null)

}else if(parent == cur) //要刪除的點為根節點,且有乙個分支

else if(cur->rchild != null)

}delete cur;

cur = null;

return;

}else

cur->data = tmp->data;

if(tmpparent == cur)

else

delete tmp;

tmp = null;

return;

}} parent = cur;

if(val < cur->data)

else

}cout << "未找到要刪除的點" << endl;

}//搜尋二叉樹

templatebinarynode* binarytree::searchtree(int val)

binarynode*cur = _root;

while (cur != null)

else if(val > cur->data)

else

}cout << "none tree" << endl;

return null;

}int main()

; int prelist = ;

binarytree*tree = new binarytree(prelist, 12, 0, 0);

//tree->previsitbitree();

//tree->midvisitbitree();

//tree->postvisitbitree();

//tree->levelvisitbitree();

tree->inserttree(34);

tree->inserttree(32);

//tree->inserttree(33);

//tree->deletetree(8);

tree->deletetree(34);

//tree->deletetree(32);

//tree->deletetree(33);

//tree->deletetree(14);

//tree->deletetree(2);

//tree->deletetree(3);

//tree->deletetree(35);

cout << "insert a number" << endl;

tree->midvisitbitree();

cout << "search tree" << endl;

binarynode*tmp = tree->searchtree(98);

if(tmp != null)

delete tree;

return 0;

}

二叉搜尋樹(遞迴 非遞迴)

完整源 在此 1 二叉搜尋樹的概念 二叉搜尋樹又稱二叉排序樹,它或者是一棵空樹,或者是具有以下性質的二叉樹。若它的左子樹不為空,則左子樹上所有節點的值都小於根節點的值 若它的右子樹不為空,則右子樹上所有的節點的值都大於根節點的值 它的左右子樹也分為二叉搜尋樹 此二叉樹的中序遍歷結果為 0,1,2,3...

二叉搜尋樹 非遞迴)

二叉查詢樹 英語 binary search tree 也稱二叉搜尋樹 有序二叉樹 英語 ordered binary tree 排序二叉樹 英語 sorted binary tree 是指一棵空樹或者具有下列性質的二叉樹 任意節點的左子樹不空,則左子樹上所有結點的值均小於它的根結點的值 任意節點的...

二叉搜尋樹 插入 查詢

宣告 第一次寫二叉搜尋樹,可能會有bug 這棵二叉搜尋樹以中序遍歷的方式輸出,所以插入 儲存的規則也是按照中序遍歷的規則 include using namespace std struct node node root,nil 根節點和乙個空結點 void insert int key else ...