Implement Insert and Delete of Tri-nay Tree
问题
Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but with three child nodes for each parent instead of two -- with the left node being values less than the parent, the right node values greater than the parent, and the middle nodes values equal to the parent.
For example, suppose I added the following nodes to the tree in this order: 5, 4, 9, 5, 7, 2, 2. The resulting tree would look like this:
/*
* Author: Min Li
* This code can implement insert and delete in a tri-nary tree.
*/ #include<iostream> using namespace std; // Definition for Tree Node
struct TreeNode {
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode *middle;
TreeNode(int x) : val(x), left(NULL), right(NULL), middle(NULL) {}
}; // Class: trinaryTree
class trinaryTree {
public:
TreeNode* insert(TreeNode *root, int value); // Insert a node
TreeNode* deleteNode(TreeNode *root, int value); // Delete a node
TreeNode* findSuccessor(TreeNode *root); // Find a node's successor
bool test(); // Test my code
}; // Method: Insert a node into tri-nary tree
// return the root of new tri-nary tree
TreeNode* trinaryTree:: insert(TreeNode *root, int value) {
TreeNode *Node = new TreeNode(value);
if(root==NULL) // tree is empty
root = Node;
else {
TreeNode *parent;
TreeNode *tmpNode = root;
// Find the parent of "Node"
while(tmpNode!=NULL) {
parent = tmpNode;
if(tmpNode->val < Node->val) // Node is in the right subtree
tmpNode = tmpNode->right;
else if(tmpNode->val > Node->val) // Node is in the left subtree
tmpNode = tmpNode->left;
else // Node is in the middle subtree
tmpNode = tmpNode->middle;
}
// Insert the Node under parent
if(Node->val == parent->val)
parent->middle = Node;
else if(Node->val > parent->val)
parent->right = Node;
else
parent->left = Node;
}
return root;
} // Method: Delete a node from tri-nary tree
// Return the root of new tree
TreeNode* trinaryTree:: deleteNode(TreeNode *root, int value) { if(root==NULL)
return NULL;
if(root->val == value) {
if(root->left==NULL && root->middle==NULL && root->right==NULL) { // Delete a leaf
delete root;
return NULL;
}
if(root->middle!=NULL) { // Middle child is not empty
root->middle = deleteNode(root->middle,value);
}
else {
if(root->left==NULL) { // Left child is empty, but right child is not
TreeNode* rightChild = root->right;
delete root;
return rightChild; }
else if(root->right==NULL) { // Right child is empty, but left child is not
TreeNode* leftChild = root->left;
delete root;
return leftChild;
}
else { // Both left and right child exists
TreeNode *successor = findSuccessor(root->right);
root->val = successor->val;
root->right = deleteNode(root->right,successor->val);
}
}
}
else if(root->val > value) // Recursive left subtree
root->left = deleteNode(root->left,value);
else // Recursive right subtree
root->right = deleteNode(root->right,value); return root;
} // Method: Find the successor
TreeNode* trinaryTree:: findSuccessor(TreeNode *root) {
if(root->left==NULL)
return root;
else
return findSuccessor(root->left);
} // Method: Test
bool trinaryTree:: test() {
trinaryTree test;
TreeNode *root = NULL;
TreeNode *node; // Test tree insert
int val[] = {,,,,,,};
int i;
for(i=;i<sizeof(val)/sizeof(int);i++) {
root = test.insert(root,val[i]); } // Test tree delete
// Case1: delete a leaf
test.deleteNode(root,);
// Case2: delete root
test.deleteNode(root,);
// Case3: delete a node with only left child
test.deleteNode(root,); return true; }
Implement Insert and Delete of Tri-nay Tree的更多相关文章
- 关于MyBatis mapper的insert, update, delete返回值
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
原文: PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...
- mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干
1.mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干 2.一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性).Con ...
- [Hive - LanguageManual] DML: Load, Insert, Update, Delete
LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...
- Store update, insert, or delete statement affected an unexpected number of rows ({0}).
问题描述 Store update, insert, or delete statement affected an unexpected number of rows ({0}). Entities ...
- The use of function Merge (update、insert、delete)
1.The use of function merge(update.insert.delete) Example: #1.Initialize the data create table #test ...
- 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法
在开发的过程中,我们为了节约时间,往往会将大量重复机械的代码封装,考虑代码的复用性,这样我们可以节约很多时间来做别的事情.最近跳槽到一节webform开发的公司,主要是开发自己公司用的ERP.开始因为 ...
- 懒人小工具:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法
之前写了篇文章,懒人小工具:[自动生成Model,Insert,Select,Delete以及导出Excel的方法](http://www.jianshu.com/p/d5b11589174a),但是 ...
- 数据操纵:SELECT, INSERT, UPDATE, DELETE
SELECT 句法 SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE ...
随机推荐
- 淘宝接口实现ip归属地查询
<?php header('content-type:text/html;charset=utf-8'); /*获取当前ip归属地 调用淘宝接口 */ function get_ip_place ...
- Python自动化运维之2、运算符与数据类型
python对象的相关术语: python程序中保存的所有数据都是围绕对象这个概念展开的: 程序中存储的所有数据都是对象 每个对象都有一个身份.一个类型和一个值 例如,school='MaGe Lin ...
- 11061160_11061151_Pair Project: Elevator Scheduler软件工程结对编程作业总结
软件工程结对编程作业总结 11061160 顾泽鹏 11061151 庞梦劼 一.关于结对编程 这次的软工任务既不是单打独斗的个人任务,也不是集思广益的团队项目,而是人数为两人的结对编程.两个人合 ...
- web UI
Semantic不错的UI,代码非常详细
- android中如何处理cookie
Managing Cookies HttpClient provides cookie management features that can be particularly useful to t ...
- [LeetCode 119] - 杨辉三角形II(Pascal's Triangle II)
问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O(k)的额外空间吗? 初始思路 首先来复习复习杨辉三角形的性质 ...
- 强大疯狂的qttools
就是有点疑惑,为什么不整合到QT主项目中呢? 有空好好看看: https://github.com/qtproject/qttools/tree/dev/src ------------------- ...
- Python模块如何安装 并确认模块已经安装好?
看自己有没有安装好,最简单的办法在可以再控制台下: C:\Users\sony>python Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC ...
- LINK : fatal error LNK1000: Internal error during IncrBuildImage
一.下微软的补丁:KB948127补丁来解决,http://code.msdn.microsoft.com/KB948127.貌似安装了也不起作用二.如果下载的补丁没安装成功或下载失败,可以用下面的方 ...
- maven下载及配置
Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object model (POM)概念来管理项目,所有的项目配置信息都被定义在一个叫做POM.xm ...