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:

  1 /*
  2  * Author: Min Li
  3  * This code can implement insert and delete in a tri-nary tree.
  4  */
  5
  6 #include<iostream>
  7
  8 using namespace std;
  9
 10
 11 // Definition for Tree Node
 12 struct TreeNode {
 13 public:
 14         int val;
 15         TreeNode *left;
 16         TreeNode *right;
 17         TreeNode *middle;
 18         TreeNode(int x) : val(x), left(NULL), right(NULL), middle(NULL) {}
 19 };
 20
 21
 22 // Class: trinaryTree
 23 class trinaryTree {
 24 public:
 25     TreeNode* insert(TreeNode *root, int value);        // Insert a node
 26     TreeNode* deleteNode(TreeNode *root, int value);    // Delete a node
 27     TreeNode* findSuccessor(TreeNode *root);            // Find a node‘s successor
 28     bool test();                                        // Test my code
 29 };
 30
 31
 32 // Method: Insert a node into tri-nary tree
 33 // return the root of new tri-nary tree
 34 TreeNode* trinaryTree:: insert(TreeNode *root, int value) {
 35     TreeNode *Node = new TreeNode(value);
 36     if(root==NULL)                    // tree is empty
 37       root = Node;
 38     else {
 39       TreeNode *parent;
 40       TreeNode *tmpNode = root;
 41       // Find the parent of "Node"
 42       while(tmpNode!=NULL) {
 43         parent = tmpNode;
 44         if(tmpNode->val < Node->val)        // Node is in the right subtree
 45           tmpNode = tmpNode->right;
 46         else if(tmpNode->val > Node->val)    // Node is in the left subtree
 47           tmpNode = tmpNode->left;
 48         else                                // Node is in the middle subtree
 49           tmpNode = tmpNode->middle;
 50       }
 51       // Insert the Node under parent
 52       if(Node->val == parent->val)
 53         parent->middle = Node;
 54       else if(Node->val > parent->val)
 55         parent->right = Node;
 56       else
 57         parent->left = Node;
 58     }
 59     return root;
 60 }
 61
 62 // Method: Delete a node from tri-nary tree
 63 // Return the root of new tree
 64 TreeNode* trinaryTree:: deleteNode(TreeNode *root, int value) {
 65
 66     if(root==NULL)
 67       return NULL;
 68     if(root->val == value) {
 69         if(root->left==NULL && root->middle==NULL && root->right==NULL) { // Delete a leaf
 70             delete root;
 71             return NULL;
 72         }
 73         if(root->middle!=NULL) {            // Middle child is not empty
 74             root->middle = deleteNode(root->middle,value);
 75         }
 76         else {
 77             if(root->left==NULL) {            // Left child is empty, but right child is not
 78                 TreeNode* rightChild = root->right;
 79                 delete root;
 80                 return rightChild;
 81
 82             }
 83             else if(root->right==NULL) {    // Right child is empty, but left child is not
 84                 TreeNode* leftChild = root->left;
 85                 delete root;
 86                 return leftChild;
 87             }
 88             else {    // Both left and right child exists
 89                 TreeNode *successor = findSuccessor(root->right);
 90                 root->val = successor->val;
 91                 root->right = deleteNode(root->right,successor->val);
 92             }
 93         }
 94     }
 95     else if(root->val > value)  // Recursive left subtree
 96       root->left = deleteNode(root->left,value);
 97     else                        // Recursive right subtree
 98       root->right = deleteNode(root->right,value);
 99
100     return root;
101 }
102
103 // Method: Find the successor
104 TreeNode* trinaryTree:: findSuccessor(TreeNode *root) {
105     if(root->left==NULL)
106       return root;
107     else
108       return findSuccessor(root->left);
109 }
110
111
112 // Method: Test
113 bool trinaryTree:: test() {
114     trinaryTree test;
115     TreeNode *root = NULL;
116     TreeNode *node;
117
118     // Test tree insert
119     int val[] = {5,4,9,5,7,2,2};
120     int i;
121     for(i=0;i<sizeof(val)/sizeof(int);i++) {
122         root = test.insert(root,val[i]);
123
124     }
125
126     // Test tree delete
127     // Case1: delete a leaf
128     test.deleteNode(root,5);
129     // Case2: delete root
130     test.deleteNode(root,5);
131     // Case3: delete a node with only left child
132     test.deleteNode(root,4);
133
134     return true;
135
136
137 }

时间: 2024-10-13 05:05:13

Implement Insert and Delete of Tri-nay Tree的相关文章

数据库编程3 Oracle 子查询 insert update delete 事务 回收站 字段操作 企业级项目案例

[本文谢绝转载原文来自http://990487026.blog.51cto.com] <大纲> 数据库编程3 Oracle 子查询 insert update delete 事务 回收站 字段操作 企业级项目案例 实验所用数据表 子查询,解决一步不能求解 查询工资比scott高的员工信息: 子查询知识体系搭建: 解释3,查询部门是sales的员工信息: 方法1:子查询 [方法2]:多表: 优化考虑: 解释4[select],只能放单行子查询 解释4[from] 考题:显示员工姓名,薪水 解释

LINQ体验(9)——LINQ to SQL语句之Insert/Update/Delete操作

我们继续讲解LINQ to SQL语句,这篇我们来讨论Insert/Update/Delete操作.这个在我们的程序中最为常用了.我们直接看例子. Insert/Update/Delete操作 插入(Insert) 1.简单形式 说明:new一个对象,使用InsertOnSubmit方法将其加入到对应的集合中,使用SubmitChanges()提交到数据库. NorthwindDataContext db = new NorthwindDataContext(); var newCustomer

sqlserver 大数据量的insert、delete操作优化

http://blog.csdn.net/lanyuzhen/article/details/7547476 --大批量导出orders表:insert DBCC DROPCLEANBUFFERS  DBCC FREEPROCCACHE goSET NOCOUNT ON BEGIN TRANSACTION  INSERT INTO test.dbo.orders with(tablock) SELECT * FROM bak.dbo.OrdersWHERE ordertime BETWEEN '

[Lintcode] Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree. Example Given binary search tree as follow, after Insert node 6, the tree

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod

linux下mysql Insert update delete 事务 用户管理

linux下mysql Insert update delete  事务 用户管理 1.INSERT插入语句格式: INSERT INTO tb_name (字段1, 字段2, ...) VALUES (值1,值2, ...)[,(值1, 值2, ...),...]; INSERT INTO 表名 SET 字段1=值1,字段2=值2,...; INSERT INTO 表名 (字段1,字段2,...) SELECT (字段1,字段2,...) FROM 表名 [WHERE 条件]; 2.REPLA

mybatis之@Select、@Insert、@Delete、@Param

之前学习的时候,看到别人在使用mybatis时,用到@Select.@Insert.@Delete.@Param这几个注解,故楼主研究了一下,在这里与大家分享 当使用这几个注解的时候,可以省去写Mapper.xml等一系列配置文件 首先来看个例子: import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotation

mybatis insert update delete返回都是整型 0,1,增,删,改要提交事物

mybatis insert update delete返回都是整型 0,1, 没有扔 增,删,改要提交事物 原文地址:https://www.cnblogs.com/gzhbk/p/9499293.html

Oracle 增删改(INSERT、DELETE、UPDATE)语句

?  简介 本文介绍 Oracle 中的增删改语句,即 INSERT.DELETE.UPDATE 语句的使用.是时候展现真正的技术了,快上车: 1.   插入数据(INSERT) 2.   修改数据(UPDATE) 3.   删除数据(DELETE) 4.   注意事项 1.   插入数据(INSERT) u  语法: INSERT INTO TABLE_NAME [(column1[, column2-]] VALUES(value1[, value2-]); 说明: 1)   INSERT