二叉查詢樹

2021-06-16 03:58:08 字數 2937 閱讀 6602

剛把資料結構c語言描述的二叉查詢樹這一部分看完,書上的給出了函式的定義,我都實現了一遍,有遞迴的和非遞迴的兩種,源**貼上來,留作紀念了。原始檔有比較詳細的注釋,但是在live writer裡面顯示是亂碼,希望在網頁上不是亂碼

tree.h標頭檔案內容:

#ifndef _tree_h

#define

_tree_h

typedef

intelementtype;

struct

treenode;

typedef

struct

treenode

*position;

typedef

struct

treenode

*searchtree;

searchtree makeempty(searchtree t);

position find(elementtype x,searchtree t);

position find_no(elementtype x,searchtree t);

position findmin(searchtree t);

position findmin_no(searchtree t);

position findmax(searchtree t);

position findmax_no(searchtree t);

searchtree insert(elementtype x,searchtree t);

searchtree insert_no(elementtype x,searchtree t);

searchtree delete(elementtype x,searchtree t);

searchtree delete_no(elementtype x,searchtree t);

elementtype retrieve(position p);

elementtype retrieve_no(position p);

position find_prev(elementtype x,searchtree t);

#endif

struct

treenode

;

tree.c原始檔的內容:

#include "tree.h"

#include

#include

//????????

searchtree makeempty(searchtree t)

return null;

}//????x??????,??

position find(elementtype x,searchtree t)

//????x??????,???

position find_no(elementtype x,searchtree t)

else if(x > t->element)

return ret;

}//??????????,???

position findmin_no(searchtree t)

return cur;

}//??????????,??

position findmin(searchtree t)

//??????????,???

position findmax_no(searchtree t)

return cur;

}//??????????,??

position findmax(searchtree t)

//??

searchtree insert(elementtype x,searchtree t)

else

return null;

} else

else if(x > p1->element)

else

return p1;

}if(x > cur->element)

cur->right = node;

else

cur->left = node;

} else if(x > t->element)

else if(x > p2->element)

else

return p2;

}if(x > cur->element)

cur->right = node;

else

cur->left = node;

}

} return t;

}//????x?????????,???

position find_prev(elementtype x,searchtree t)

else if(x > t->element)

else

return t;

}//??????

searchtree delete(elementtype x,searchtree t)

else if(x_pos->left == null || x_pos->right == null)//???????

else if(prev->left == x_pos)

free(x_pos);//?????????,?x_pos??null

x_pos = null;

} else if(x_pos->left != null && x_pos->right != null)//???????

else if(ptr_min_prev->left == ptr_min)

free(x_pos);

x_pos = null;

} return t;

}void output(searchtree t)

}int main()

二叉樹 二叉查詢樹

構建二叉樹,判斷是否為二叉查詢樹,遞迴先序遍歷,非遞迴中序遍歷 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 ...