问题

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. Array and its point.

    a is the array name. &a is the ponit of 2-D array which contains a[5]. the type of &a should ...

  2. MyEclipse 点击 部署 按钮 无效的解决办法

    问题描述 1 通常情况下,当我们点击MyEclipse任务栏上的部署按钮时,会弹出项目部署框,如下图:   2 但我们有时也会遇到点击部署按钮怎么也弹不出项目部署框的问题. END 解决方法一: 1 ...

  3. 自定义cell 自适应高度

    #pragma mark - 动态计算cell高度 //计算 返回 文本高度 + (CGFloat)calsLabelHeightWithContact:(Contacts *)contact { / ...

  4. swift代码排版-参考

    代码排版包括: 空行.空格.断行和缩进等内容.代码排版内容比较多工作量很多,但是非常重要. 空行 空行将逻辑相关的代码段分隔开,以提高可读性.下列情况应该总是添加空行: 类型声明之前. import语 ...

  5. Filter Conditions 过滤条件

    <pre name="code" class="html">Filter Conditions 过滤条件: Rsyslog 提供4种不同类型的&qu ...

  6. 【转】s3c2440 按键驱动 — 字符设备

    原文网址:http://www.xuebuyuan.com/632893.html 主机:VM - redhat 9.0 开发板:FL2440,linux-2.6.12 arm-linux-gcc:3 ...

  7. jquery easyui tree绑定静态数据的方法

    若是动态,返回的是json格式,这个比较常见,就不列举说明了,如果要绑定的数据为静态,很简单,只需将ajax的url改为data 如 //鼠标单击树事件 $("#tree").tr ...

  8. Scala-变量、常量和懒加载

    package com.mengyao.scala.function /** * Scala的变量声明和使用(可变类型和值类型) *  * @author mengyao */object Test0 ...

  9. hadoop2.2.0 MapReduce分区

    package com.my.hadoop.mapreduce.partition; import java.util.HashMap;import java.util.Map; import org ...

  10. Oracle 表分析

    ANALYZE TABLE SeikyuTbl COMPUTE Statistics FOR TABLE FOR ALL COLUMNS FOR ALL INDEXES ; 一.优化器的优化方式 Or ...