二叉排序树的实现(C#)

原创 2007年10月09日 13:31:00

 

 

搞了一天终于把这个homework搞定了。开心    ,下个星期有新的任务了。如果所有工作在星期五都可以告于一个段落,对所有职场人来说应该是一件很舒心的事吧。周末可以专注自己的其他事情,不用背着包袱过日子。Wish happy weekend!

曾经在网上搜到很多关于一些数据结构的代码,对过往的我的学业带来很大帮助,对现在工作中的我也很有启发。所以现在也把代码共享出来。希望也可以help someone in someday!

 

 
*****************************************************start****************************************************************************************using System;
using System.Collections;
using System.Globalization;
using System.Text;

namespace BTree
...{
    
    
public class TreeNode
    
...{
        
public object data;
        
public TreeNode leftNode;
        
public TreeNode rightNode;

        
public TreeNode(object item)
        
...{
            data 
= item;
            leftNode 
= rightNode = null;
        }


        
public void PrintNode()
        
...{
            
string result = "";
            
if (this == null)
                
return;
            result 
= "node.data is :" + this.data.ToString();
            Console.WriteLine(result);
        }

    }
 // end of TreeNode class

    
public class BTree
    
...{
        
public TreeNode root;

        
// create an empty BTree
        public BTree()
        
...{
            root 
= null;
        }


        
public void Destroy(TreeNode node)
        
...{
            
if (root != null)
            
...{
                Destroy(root.leftNode);
                Destroy(root.rightNode);
                root 
= null;
            }

        }


        
public void CreateBTree(int n)
        
...{
            
int i = 0;
            
for (; i < n;i++ )
            
...{
                
object temp = Console.ReadLine();
                insert(temp);
            }

        }


        
public void Inorder(TreeNode root)
        
...{
            
if (root == null)
                
return;
            Inorder(root.leftNode);
            root.PrintNode();
            Inorder(root.rightNode);
        }


        
public void Firstorder(TreeNode root)
        
...{
            
if (root == null)
                
return;
            root.PrintNode();
            Firstorder(root.leftNode);
            Firstorder(root.rightNode);
        }


        
public void Backorder(TreeNode root)
        
...{
            
if (root == null)
                
return;
            Backorder(root.leftNode);
            Backorder(root.rightNode);
            root.PrintNode();
        }



        
public TreeNode search(object item)
        
...{
            TreeNode temp
= null;
            
if (root.data.Equals(item))
                temp 
= root;
            
else
                temp 
= search(root, item);
            
return temp;
        }


        
public TreeNode search(TreeNode root, object item)
        
...{
            TreeNode tmp 
= null;
            
if (root.data.Equals(item))
            
...{
                tmp
= root;
                
return tmp;
            }

            
int result = Comparer.Default.Compare(root.data,item);
            
if (result > 0)
            
...{
                
if (root.leftNode != null)
                    tmp 
= search(root.leftNode, item);
            }

            
else
            
...{
                
if (root.rightNode != null)
                    tmp 
= search(root.rightNode, item);
            }

            
return tmp;
        }


        
public void insert(object item)
        
...{
            
if (root == null)
            
...{
                root 
= new TreeNode(item);
            }

            
else
                insert(root, item);
        }


        
public void insert(TreeNode node, object item)
        
...{
            
int result = Comparer.Default.Compare(node.data, item);
            
if ( result >= 0)
            
...{
                
if (node.leftNode == null)
                
...{
                    TreeNode temp 
= new TreeNode(item);
                    node.leftNode 
= temp;
                }

                
else
                    insert(node.leftNode, item);
            }

            
else
            
...{
                
if (node.rightNode == null)
                
...{
                    TreeNode temp 
= new TreeNode(item);
                    node.rightNode 
= temp;
                }

                
else
                    insert(node.rightNode, item);
            }

        }
 //end of insert


        
public TreeNode MinRight(TreeNode node)
        
...{
            
if (node.leftNode == null)
                
return node;
            
else
                
return MinRight(node.leftNode);
        }


        
public TreeNode searchParent(TreeNode head,TreeNode p)
        
...{
            
if(head.leftNode == p || head.rightNode == p || head == null)
                
return head;
            
int result = Comparer.Default.Compare(p.data, head.data);
            
if (result <= 0)
                
return searchParent(head.leftNode, p);
            
else
                
return searchParent(head.rightNode, p);
        }


        
public void delete(object item)
        
...{
            TreeNode p 
= null;

            p 
= search(root, item);

            
if (p == null// p is not the item of the BTree
            ...{
                Console.WriteLine(
"don't find the item");
                
return;
            }


            
if (p == root) // p is the root node
            ...{
                
if (root.leftNode != null || root.rightNode != null)
                
...{
                    Console.WriteLine(
"BTree root node can't be delete yet");
                    
return;
                }

                
else
                
...{
                    root 
= null;
                    Console.WriteLine(
"BTree root node has been deleted");
                    
return;
                }

            }


            
else // p is not the root node
            ...{
                TreeNode parent 
= null;
                parent 
= searchParent(root, p);
                
if (p.leftNode == null && p.rightNode == null// p is leaf node
                ...{
                    
if (parent.leftNode == p)
                        parent.leftNode 
= null;
                    
else
                        parent.rightNode 
= null;
                }

                
else //p is not leaf node
                ...{
                    
if (p.rightNode == null// p has no right child node
                    ...{
                        
if (parent.leftNode == p)
                            parent.leftNode 
= p.leftNode;
                        
else
                            parent.rightNode 
= p.leftNode;
                    }

                    
if (p.leftNode == null// p has no left child node
                    ...{
                        
if (p.leftNode == p)
                            parent.leftNode 
= p.rightNode;
                        
else
                            parent.rightNode 
= p.rightNode;
                    }

                    
else if (p.leftNode !=null && p.rightNode !=null// p has both left node and right node
                    ...{
                        TreeNode rightMinSon, rightMinParent;
                        rightMinSon 
= MinRight(p.rightNode);
                        rightMinParent 
= searchParent(p.rightNode, rightMinSon);

                        rightMinParent.leftNode 
= rightMinSon.rightNode;

                        
if (p.rightNode == rightMinSon)
                        
...{
                            p.rightNode 
= rightMinSon.rightNode;
                        }

                        p.data 
= rightMinSon.data;

                    }


                }

            }

        }
 // end of delete

    }
 // end of BTree class

 

    
public class MainEntry
    
...{
        
public static void Main()
        
...{
            BTree tree 
= new BTree();

            Console.Write(
" *********************************************** ");
            Console.WriteLine(
"Create BTree");
            Console.WriteLine(
"please input the numbers of the BTree which you want to create");
            
int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(
"please input the items:");
            tree.CreateBTree(n);
            Console.WriteLine(
"***********************************************");

            Console.Write(
" *********************************************** ");
            Console.WriteLine(
"BTree first search");
            tree.Firstorder(tree.root);
            Console.WriteLine(
"***********************************************");

            Console.Write(
" *********************************************** ");
            Console.WriteLine(
"BTree inorder search");
            tree.Inorder(tree.root);
            Console.WriteLine(
"***********************************************");

            Console.Write(
" *********************************************** ");
            Console.WriteLine(
"BTree backorder search");
            tree.Backorder(tree.root);
            Console.WriteLine(
"***********************************************");

            Console.Write(
" *********************************************** ");
            Console.WriteLine(
"search item in BTree");
            Console.WriteLine(
"please input the item you want to search");
            
            
object obj = Console.ReadLine();
            TreeNode result 
= tree.search(obj);
            
if (result != null)
                Console.WriteLine(
"has find the item {0}", result.data);
            
else
                Console.WriteLine(
"has not find the item {0}", obj);
             
#if DEBUG
            
for (int i = 1; i < n+1; i++)
            
...{
               
object item = null;
               TreeNode temp
= null;
               item 
= Console.ReadLine();
               temp 
= tree.search(item);
                
if (temp != null)
                    Console.WriteLine(
"has find the item {0}", temp.data);
                
else
                    Console.WriteLine(
"has not find the item {0}", item);
            }

#endif          
            Console.WriteLine(
"***********************************************");

            Console.Write(
" *********************************************** ");
            Console.WriteLine(
"insert item in BTree");
            Console.WriteLine(
"please input the item you want to insert");
            obj 
= Console.ReadLine();
            tree.insert(obj);
            tree.Firstorder(tree.root);
            Console.WriteLine(
"***********************************************");

       
            Console.Write(
" *********************************************** ");
            Console.WriteLine(
"delete item in BTree");
            Console.WriteLine(
"please input the item you want to delete");
            
object obj1 = Console.ReadLine();
            tree.delete(obj1);
            
if (tree.root != null)
                tree.Firstorder(tree.root);
            
else
                Console.WriteLine(
"the BTree is empty");
#if DEBUG
            
object test = null;
            
for (int i = 1; i <= n+2; i++)
            
...{
                test 
= Console.ReadLine();
                tree.delete(test);
                
if (tree.root != null)
                        tree.Firstorder(tree.root);
                
else
                        Console.WriteLine(
"the BTree is empty");
            }

#endif

           Console.WriteLine(
"***********************************************");
           Console.ReadKey();         

        }

    }
    
}
 // end of namespace BTree

*************************************************************end***************************************************************************

 

上面#if DEBUG是编译开关,而且里面的循环纯粹是为了测试上面写的BTree的相关操作是否正确。如果大家要用的话,只需把BTreeClass拷贝过去就可以直接利用了。^_^

c#二叉排序树实现

主要是二叉排序树的构建和显示 using System; using System.Collections; using System.Text; public class Node { p...
  • jialeheyeshu
  • jialeheyeshu
  • 2016-12-02 10:13:50
  • 783

c#实现二叉树+二叉树遍历彻底理解

本来只是一个复习的,但是为了能系统的理解性复习所以在此花了一段时间来写这个博文,同时为了贡献自己的知识,让那些初学者彻底理解递归调用,写了写自我的理解。一直受那句的影响:只有对一个知识和技术有足够的理...
  • jialeheyeshu
  • jialeheyeshu
  • 2016-08-28 09:50:20
  • 4484

如何使用非递归算法实现二叉排序树的建立

1.如何使用非递归算法实现二叉排序树的建立?
  • liu16659
  • liu16659
  • 2017-04-28 16:58:13
  • 314

二叉排序树的创建(结点的插入,删除等操作)

二叉排序树的理论看课本 代码如下: BSTree.h struct BSTreeNode; typedef struct BSTreeNode *ptrtreenode; void Make...
  • jw903
  • jw903
  • 2014-05-05 22:49:48
  • 1347

无限级树形结构(增、删、改、排序)

  • 2014年03月05日 20:18
  • 463KB
  • 下载

C# 二叉树算法排序

  • 2010年12月24日 16:40
  • 32KB
  • 下载

八大排序算法 之 堆排序(二叉树排序)

例如数组 a={19,3,60,7,1,15,33,24,45,32,79,85}; 排序思想: 1,堆排序也是选择排序的一种,根据堆得特性,每次把最大或最小值(本次以最大值为例)拿出来,按序排列; ...
  • yxb_yingu
  • yxb_yingu
  • 2016-05-07 10:03:43
  • 12185

排序二叉树C#实现

        很久以前写的,现在贴出来交流一下。先看排序二叉树的接口public interface ISorttedBinaryTree {  void InsertElement(ICompar...
  • zhuweisky
  • zhuweisky
  • 2005-07-02 20:33:00
  • 2379

利用二叉树的排序算法c#实现

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac...
  • fengzheng22
  • fengzheng22
  • 2013-12-07 16:30:42
  • 1209

二叉排序树C实现(含完整源码)

分类: 数据结构与算法 2014-02-25 00:02 1738人阅读 评论(18) 收藏 举报 二叉排序树二叉查找树BST 目录(?)[+] 转载请注明出处:htt...
  • hongya6088
  • hongya6088
  • 2014-03-10 17:25:44
  • 481
收藏助手
不良信息举报
您举报文章:二叉排序树的实现(C#)
举报原因:
原因补充:

(最多只允许输入30个字)