C++ Implement binary search Tree(BST)
2014-07-19 20:35
375 查看
我们已经知道了关于BST的一些基本知识。 下面使用C++ 去实现一个BST。 以一个BST of integers, 我们的第一步是创建节点(在内存heap 区域), 然后link 起来, 一个节点有三个fields, 一个指向左孩子节点的指针, 一个指向data, 一个指向右孩子节点的指针。 如果没有孩子, 将对应的指针设定为NULL, 我们需要创建的时候就将两个指针变量设为NULL, 以便更好地创建BST。 注意, 在doubly linked list中的节点也有三个fields,, 其中有两个指针,
一个指向前一个节点, 一个指向下一个节点。 但是doubly linked list 是线性的data structures:
假如我们创建节点的地址如标注的, 最终我们创建的BST如下:
注意根节点指针是确定这棵BST树的位置的。 没有根节点, 我们就无法找到这课BST树(注意区分pointer to root 和 the root itself)。
下面给出程序:
运行结果为:
程序分析说明:
在主程序中:
(1)BstNode* root = NULL;
(2)root = Insert(root,15); //即调用的是Insert(0, 15),
效果如下:
NOTE: main 函数中有个root, Insert定义的时候也有一个root, 但是这两个变量的function scope 不同。
由于在Insert函数的scope中, 传递的参数为0(赋给了root指针), 会执行第一个条件语句, getNewNode 函数创建如上的节点, 返回新的节点的地址(为200), 赋值给root, 返回这个root(function scope 为Insert)。
返回到主程序中, 语句root = Inser(0, 15), 相当于root = 200(这个root 的function scope为main 函数), 这样的赋值, 相当于将root 和新节点的link 链接起来。 于是又如下效果, 这样就创建了一个只有一个BST的二叉树。
(3)执行 root = Insert(root,10);
由于在main 函数中, 此时root 为200, 所以相当于调用 Insert(200, 10)。
进入函数Insert内, 此时由于我们的BST不是empty tree(即root != NULL), 所以会进入下面的两个条件语句判断(2 cases, 要么应该插入在左子树, 要么右子树)。 经判断, 10 < 15(data < root -> data) , 所以应该应该插在坐子树中。 此时就是对Insert函数的递归调用。
接下来, 运行指令: root->left = Insert(root->left,data);
也就是说, 赋值左边, Insert(root->, data), 相当于运行 Insert(0, 10),
上述的Insert(0, 10)运行完之后, 就返回新节点的地址(150), 对当前的节点(即root -> left 进行赋值), 建立 link。
具体的, 如下:
于是返回到了Insert(200, 10)stack 中沿着未运行玩的程序继续运行, 最后return 会root(即200):
于是实现了插入节点10.
同理如果数据比节点的数据大, 我们需要将其插入在右子树中。 以此类推。
(3)主函数中, 执行:root = Insert(root,20); // 即root = Insert(200, 20).
接下来:
最后, 回到主程序中, 返回树的根节点:
(4) 主程序中执行: root = Insert(root,25);// root = Insert(200, 25):
执行过程为:
一个指向前一个节点, 一个指向下一个节点。 但是doubly linked list 是线性的data structures:
假如我们创建节点的地址如标注的, 最终我们创建的BST如下:
注意根节点指针是确定这棵BST树的位置的。 没有根节点, 我们就无法找到这课BST树(注意区分pointer to root 和 the root itself)。
下面给出程序:
// Binary Search Tree - Implemenation in C++ // Simple program to create a BST of integers and search an element in it #include<iostream> using namespace std; //Definition of Node for Binary search tree struct BstNode { int data; BstNode* left; BstNode* right; }; // Function to create a new Node in heap BstNode* GetNewNode(int data) { BstNode* newNode = new BstNode(); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } // To insert data in BST, returns address of root node BstNode* Insert(BstNode* root,int data) { if(root == NULL) { // empty tree root = GetNewNode(data); } // if data to be inserted is lesser, insert in left subtree. else if(data <= root->data) { root->left = Insert(root->left,data); } // else, insert in right subtree. else { root->right = Insert(root->right,data); } return root; // 若无, 则回传为void, 由于root的scope 我local variable, 所以。。。 } //To search an element in BST, returns true if element is found bool Search(BstNode* root,int data) { if(root == NULL) { return false; } else if(root->data == data) { return true; } else if(data <= root->data) { return Search(root->left,data); } else { return Search(root->right,data); } } int main() { BstNode* root = NULL; // Creating an empty tree, root is to store the address of the root node /*Code to test the logic*/ root = Insert(root,15); root = Insert(root,10); root = Insert(root,20); root = Insert(root,25); root = Insert(root,8); root = Insert(root,12); // Ask user to enter a number. int number; cout<<"Enter number be searched\n"; cin>>number; //If number is found, print "FOUND" if(Search(root,number) == true) cout<<"Found\n"; else cout<<"Not Found\n"; return 0; }
运行结果为:
程序分析说明:
在主程序中:
(1)BstNode* root = NULL;
(2)root = Insert(root,15); //即调用的是Insert(0, 15),
效果如下:
NOTE: main 函数中有个root, Insert定义的时候也有一个root, 但是这两个变量的function scope 不同。
由于在Insert函数的scope中, 传递的参数为0(赋给了root指针), 会执行第一个条件语句, getNewNode 函数创建如上的节点, 返回新的节点的地址(为200), 赋值给root, 返回这个root(function scope 为Insert)。
返回到主程序中, 语句root = Inser(0, 15), 相当于root = 200(这个root 的function scope为main 函数), 这样的赋值, 相当于将root 和新节点的link 链接起来。 于是又如下效果, 这样就创建了一个只有一个BST的二叉树。
(3)执行 root = Insert(root,10);
由于在main 函数中, 此时root 为200, 所以相当于调用 Insert(200, 10)。
进入函数Insert内, 此时由于我们的BST不是empty tree(即root != NULL), 所以会进入下面的两个条件语句判断(2 cases, 要么应该插入在左子树, 要么右子树)。 经判断, 10 < 15(data < root -> data) , 所以应该应该插在坐子树中。 此时就是对Insert函数的递归调用。
接下来, 运行指令: root->left = Insert(root->left,data);
也就是说, 赋值左边, Insert(root->, data), 相当于运行 Insert(0, 10),
上述的Insert(0, 10)运行完之后, 就返回新节点的地址(150), 对当前的节点(即root -> left 进行赋值), 建立 link。
具体的, 如下:
于是返回到了Insert(200, 10)stack 中沿着未运行玩的程序继续运行, 最后return 会root(即200):
于是实现了插入节点10.
同理如果数据比节点的数据大, 我们需要将其插入在右子树中。 以此类推。
(3)主函数中, 执行:root = Insert(root,20); // 即root = Insert(200, 20).
接下来:
最后, 回到主程序中, 返回树的根节点:
(4) 主程序中执行: root = Insert(root,25);// root = Insert(200, 25):
执行过程为:
相关文章推荐
- Binary Search Tree (BST) Implementation C++
- [C++]LeetCode: 122 Validate Binary Search Tree (是否是合法BST树)
- 二分查找树的C++实现 Binary Search Tree(BST)
- zoj 3170 7 Levels of Binary Search Tree(BST = =)
- 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree
- binary search tree in C++
- 二叉查找树(binary search tree (BST))--算法导论示例
- Binary Search Tree/ Binary Sorted Tree —— BST
- Recover Binary Search Tree BST找乱序的两个元素 @LeetCode
- Convert Sorted Array to Binary Search Tree 把一个有序数组转换成BST @LeetCode
- BST(Binary Search Tree,二叉查找树,二叉排序树)c的实现(部分函数不知如何调用)
- C++ young 程序库——y_binary_search_tree_base.hpp、y_red_black_tree_base.hpp 和 y_red_black_tree.hpp
- Implement Binary Search Tree
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- Convert Binary Search Tree (BST) to Sorted Doubly-Linked List
- Convert Sorted List to Binary Search Tree 把有序链表转为BST@LeetCode
- Binary Search Tree Insertion in C++
- BST(Binary Search Tree 二叉查找树模版)
- 二叉查找树(binary search tree (BST))--算法导论示例
- 【题解】【BST】【Leetcode】Validate Binary Search Tree