问题

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的更多相关文章

  1. 关于MyBatis mapper的insert, update, delete返回值

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...

  2. PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)

    原文: PHP5: mysqli 插入, 查询, 更新和删除  Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...

  3. mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干

    1.mysql 事务是专门用来管理insert,update,delete语句的,和select语句一点不相干 2.一般来说,事务是必须满足4个条件(ACID): Atomicity(原子性).Con ...

  4. [Hive - LanguageManual] DML: Load, Insert, Update, Delete

    LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...

  5. 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 ...

  6. 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 ...

  7. 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法

    在开发的过程中,我们为了节约时间,往往会将大量重复机械的代码封装,考虑代码的复用性,这样我们可以节约很多时间来做别的事情.最近跳槽到一节webform开发的公司,主要是开发自己公司用的ERP.开始因为 ...

  8. 懒人小工具:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法

    之前写了篇文章,懒人小工具:[自动生成Model,Insert,Select,Delete以及导出Excel的方法](http://www.jianshu.com/p/d5b11589174a),但是 ...

  9. 数据操纵:SELECT, INSERT, UPDATE, DELETE

    SELECT 句法 SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE ...

随机推荐

  1. mysql innerjoin left join right join 解析

    毕业半年多时间,一直都没有学习好join  之前一直是先从一个表里面取出数据然后,然后再从另外一个表里面取出数据,然后再写一个函数循环格式化数据. 还是先写一下学到的东西吧! 转载自w3school ...

  2. 关闭Centos的自动更新

    昨天跟老板汇报,提到我们的linux服务器每天自动更新,老板大发雷霆,说生产系统不能够这样,非常不安全,一个师兄也提到他原来在移动的时候,服务器更新也是很谨慎的事情.看来我的思维太技术了,不够全面,所 ...

  3. 用c++11打造类似于python的range

    python中的range函数表示一个连续的有序序列,range使用起来很方便,因为在定义时就隐含了初始化过程,因为只需要给begin()和end()或者仅仅一个end(),就能表示一个连续的序列.还 ...

  4. 转:Node.js软肋之CPU密集型任务

    文章来自于:http://www.infoq.com/cn/articles/nodejs-weakness-cpu-intensive-tasks Node.js在官网上是这样定义的:“一个搭建在C ...

  5. Altium Designer 6 快速进行差分对走线

    1: 在原理图中让一对网络前缀相同,后缀分别为_N 和_P,并且加上差分队对指示.在原理图中,让一对网络名称的前缀名相同,后缀分别为_N 和_P,左键点击Place DirectivesDiffere ...

  6. [面试题总结及扩展知识]HTTP协议返回状态码的问题

    经常在网页中看到一些错误的返回信息,见一个查一个已经累感不爱,在2014年腾讯笔试题中也见到一道这样的问题,所以现在来总结一下: 腾讯2014面试题: 答案选B 附带一些http协议的错误代码: 当服 ...

  7. memory_target not supported on this system

  8. 写两个线程,一个对n每次加一,一个对n每次减一

    public class A{     private static Integer n = 0; }     public class B extends A implements Runnable ...

  9. jQuery效果---隐藏与显示

    隐藏与显示 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

  10. Zookeeper为什么总是奇数个

    zookeeper有这样一个特性: [集群中只要有超过过半的机器是正常工作的,那么整个集群对外就是可用的] 也就是说如果有2个zookeeper,那么只要有1个死了zookeeper就不能用了,因为1 ...