一、二叉排序樹介紹:
二叉排序樹又稱“二叉查找樹”、“二叉搜索樹”。二叉排序樹:或者是一棵空樹,或者是具有下列性質的二叉樹:
1. 若它的左子樹不空,則左子樹上所有結點的值均小於它的根結點的值;
2. 若它的右子樹不空,則右子樹上所有結點的值均大於它的根結點的值;
3. 它的左、右子樹也分別為二叉排序樹。
二叉排序樹通常采用二叉鏈表作為存儲結構。中序遍歷二叉排序樹可得到一個依據關鍵字的有序序列,一個無序序列可以通過構造一棵二叉排序樹變成一個有序序列,構造樹的過程即是對無序序列進行排序的過程。每次插入的新的結點都是二叉排序樹上新的葉子結點,在進行插入操作時,不必移動其它結點,只需改動某個結點的指針,由空變為非空即可。搜索、插入、刪除的時間復雜度等於樹高,期望O(logn),最壞O(n)(數列有序,樹退化成線性表,如右斜樹)
二、代碼:
// Sort.cpp : 定義控制台應用程序的入口點。 // #include "stdafx.h" #include<windows.h> #include<assert.h> #if 1 typedef int KEY_VALUE; struct bstree_node { KEY_VALUE data; struct bstree_node *left; struct bstree_node *right; }; struct bstree { struct bstree_node *root; }; //創建節點並給key賦值 struct bstree_node *bstree_create_node(KEY_VALUE key) { struct bstree_node *node = (struct bstree_node*)malloc(sizeof(struct bstree_node)); if (node == NULL) { assert(0); } node->data = key; node->left = node->right = NULL; return node; } //把節點插入到二叉順序樹中 int bstree_insert(struct bstree *T, int key) { assert(T != NULL); if (T->root == NULL) { T->root = bstree_create_node(key); return 0; } struct bstree_node *node = T->root; struct bstree_node *tmp = T->root; //node的父節點為tmp, while (node != NULL) { tmp = node; if (key < node->data) { node = node->left; } else { node = node->right; } } if (key < tmp->data) { tmp->left = bstree_create_node(key); } else { tmp->right = bstree_create_node(key); } return 0; } //遍歷順序節點,並輸出 int bstree_traversal(struct bstree_node *node) { if (node == NULL) return 0; bstree_traversal(node->left); printf("%4d ", node->data); bstree_traversal(node->right); } #define ARRAY_LENGTH 20 int main() { int keyArray[ARRAY_LENGTH] = { 24, 25, 13, 35, 23, 26, 67, 47, 38, 98, 20, 13, 17, 49, 12, 21, 9, 18, 14, 15 }; struct bstree T = { 0 }; int i = 0; for (i = 0; i < ARRAY_LENGTH; i++) { bstree_insert(&T, keyArray[i]); } bstree_traversal(T.root); system("pause"); printf("\n"); } #else typedef int KEY_VALUE; #define BSTREE_ENTRY(name, type) \ struct name { \ struct type *left; \ struct type *right; \ } struct bstree_node { KEY_VALUE data; BSTREE_ENTRY(, bstree_node) bst; }; struct bstree { struct bstree_node *root; }; struct bstree_node *bstree_create_node(KEY_VALUE key) { struct bstree_node *node = (struct bstree_node*)malloc(sizeof(struct bstree_node)); if (node == NULL) { assert(0); } node->data = key; node->bst.left = node->bst.right = NULL; return node; } int bstree_insert(struct bstree *T, int key) { assert(T != NULL); if (T->root == NULL) { T->root = bstree_create_node(key); return 0; } struct bstree_node *node = T->root; struct bstree_node *tmp = T->root; while (node != NULL) { tmp = node; if (key < node->data) { node = node->bst.left; } else { node = node->bst.right; } } if (key < tmp->data) { tmp->bst.left = bstree_create_node(key); } else { tmp->bst.right = bstree_create_node(key); } return 0; } int bstree_traversal(struct bstree_node *node) { if (node == NULL) return 0; bstree_traversal(node->bst.left); printf("%4d ", node->data); bstree_traversal(node->bst.right); } #define ARRAY_LENGTH 20 int _tmain(int argc, _TCHAR* argv[]) { int keyArray[ARRAY_LENGTH] = { 24, 25, 13, 35, 23, 26, 67, 47, 38, 98, 20, 13, 17, 49, 12, 21, 9, 18, 14, 15 }; struct bstree T = { 0 }; int i = 0; for (i = 0; i < ARRAY_LENGTH; i++) { bstree_insert(&T, keyArray[i]); } bstree_traversal(T.root); printf("\n"); system("pause"); return 0; } #endif