- using System;
- namespace BTree
- {
- class Node//二叉树结点数据类型
- {
- public int data;//数据
- public Node lchild;//左子树
- public Node rchild;//右子树
- public Node(int i, Node l, Node r)
- {
- data = i;
- lchild = l;
- rchild = r;
- }
- }
- class BTree
- {
- public Node ROOT;
- public BTree()
- {
- ROOT = null;
- }
- public void insert(int element)
- {
- Node tmp, parent = null, currentNode = null;
- find(element, ref parent, ref currentNode);
- if (currentNode != null)//如果currentNode不为空值说明currentNode.info==element,是重复元素
- {
- Console.WriteLine(“Duplicates words not allowed”);
- return;
- }
- else//如果currentNode为空值时插入新结点
- {
- tmp = new Node(element, null, null);//tmp为新节点,lchild和rchild指正均为空
- if (parent == null)//如果parent结点为空,说明树为空,则tmp作为根结点
- ROOT = tmp;
- else
- if (element < parent.data)//如果parent不为空,则比较parent.data和element的大小
- parent.lchild = tmp;
- else
- parent.rchild = tmp;
- }
- }
- public void find(int element, ref Node parent, ref Node currentNode)//寻找元素
- {
- currentNode = ROOT;
- parent = null;
- while ((currentNode != null) && (currentNode.data != element))
- {//当currentNode指针所指的当前结点不为空时,或者当前结点的info不为element时循环
- parent = currentNode;//将parent指针指向currentNode指向的当前结点
- if (element < currentNode.data)
- currentNode = currentNode.lchild;//element比current.data小时,将currentNode指向左子树
- else
- currentNode = currentNode.rchild;
- }
- }
- public void delete(int element)
- {
- Node parent = null, currentNode = null;
- find(element, ref parent, ref currentNode);
- if (currentNode != null)//找到目标结点
- {
- if (currentNode.lchild == null && currentNode.rchild == null)//如果目标结点的左子树和右子树均为空
- {
- if (parent == null)//如果目标结点为根结点
- ROOT = null;
- //Release currentNode
- else
- {
- if (parent.lchild == currentNode)
- parent.lchild = null;
- else
- parent.rchild = null;
- //Release currentNode
- }
- }
- else if ((currentNode.lchild == null && currentNode.rchild != null)
- || (currentNode.lchild != null && currentNode.rchild == null))//如果目标结点有一棵子树
- {
- if (parent == null)
- {
- if (currentNode.lchild != null)
- ROOT = currentNode.lchild;
- else
- ROOT = currentNode.rchild;
- //Release currentNode
- }
- else
- {
- if (parent.lchild == currentNode)
- if (currentNode.lchild != null)
- parent.lchild = currentNode.lchild;
- else
- parent.lchild = currentNode.rchild;
- else
- if (currentNode.lchild != null)
- parent.rchild = currentNode.lchild;
- else
- parent.rchild = currentNode.rchild;
- //Release currentNode
- }
- }
- else if (currentNode.lchild != null && currentNode.rchild != null)//如果目标结点有两颗子树
- {
- Node inorder_suc = currentNode.rchild;
- parent = currentNode;
- while (inorder_suc.lchild != null)//寻找currentNode右子树的最左子树
- {
- parent = inorder_suc;
- inorder_suc = inorder_suc.lchild;
- }
- currentNode.data = inorder_suc.data;
- if (inorder_suc.lchild == null && inorder_suc.rchild == null)//如果inorder_suc的左子树和右子树均为空
- {
- if (parent.lchild == inorder_suc)
- parent.lchild = null;
- else
- parent.rchild = null;
- //Release currentNode
- }
- if (inorder_suc.lchild == null || inorder_suc.rchild == null)//如果inorder_suc有一棵子树
- {
- if (parent.lchild == inorder_suc)
- if (inorder_suc.lchild != null)
- parent.lchild = inorder_suc.lchild;
- else
- parent.lchild = inorder_suc.rchild;
- else
- if (inorder_suc.lchild != null)
- parent.rchild = inorder_suc.lchild;
- else
- parent.rchild = inorder_suc.rchild;
- //Release currentNode
- }
- }
- }
- }
- public void balanceTree(ref Node pivot, ref int BF)
- {
- Node X, Y, parent = null, currentNode = null;
- if (BF < 0)//if tree is initially right heavy
- {
- X = pivot.rchild;
- if (getHeight(X.lchild, ref pivot, ref BF) < getHeight(X.rchild, ref pivot, ref BF))
- {//the new node is inserted in the right subtree of X
- find(pivot.data, ref parent, ref currentNode);
- if (pivot != ROOT)
- {
- if (parent.lchild == pivot)
- parent.lchild = X;
- else
- parent.rchild = X;
- }
- else
- ROOT = X;
- pivot.rchild = X.lchild;
- X.lchild = pivot;
- }
- else//the new node is inserted in the left subtree of x
- {
- Y = X.lchild;
- X.lchild = Y.rchild;
- Y.rchild = X;
- pivot.rchild = Y;
- //repeat once the code above when getHeight(X.lchild) < getHeight(X.rchild)
- X = pivot.rchild;
- find(pivot.data, ref parent, ref currentNode);
- if (pivot != ROOT)
- {
- if (parent.lchild == pivot)
- parent.lchild = X;
- else
- parent.rchild = X;
- }
- else
- ROOT = X;
- pivot.rchild = X.lchild;
- X.lchild = pivot;
- }
- }
- else//if the tree is initially left heavy
- {
- X = pivot.lchild;
- if (getHeight(X.lchild, ref pivot, ref BF) > getHeight(X.rchild, ref pivot, ref BF))
- {//the new node is inserted in the left subtree of X
- find(pivot.data, ref parent, ref currentNode);
- if (pivot != ROOT)
- {
- if (parent.lchild == pivot)
- parent.lchild = X;
- else
- parent.rchild = X;
- }
- else
- ROOT = X;
- pivot.lchild = X.rchild;
- X.rchild = pivot;
- }
- else//the new node is inserted in the right subtree of x
- {
- Y = X.rchild;
- X.rchild = Y.lchild;
- Y.lchild = X;
- pivot.lchild = Y;
- //repeat once the code above when getHeight(X.lchild) > getHeight(X.rchild)
- X = pivot.lchild;
- find(pivot.data, ref parent, ref currentNode);
- if (pivot != ROOT)
- {
- if (parent.lchild == pivot)
- parent.lchild = X;
- else
- parent.rchild = X;
- }
- else
- ROOT = X;
- pivot.lchild = X.rchild;
- X.rchild = pivot;
- }
- }
- }
- public int getHeight(Node ptr, ref Node pivot, ref int BF)
- {
- int lHeight = 0, rHeight = 0;
- if (ROOT == null && pivot != null)
- return 0;
- else
- {
- if (ptr!= null)
- {
- lHeight = getHeight(ptr.lchild, ref pivot, ref BF);
- rHeight = getHeight(ptr.rchild, ref pivot, ref BF);
- if (Math.Abs(lHeight – rHeight) > 1 && pivot == null)//notice here!! “pivot==null”,otherwise make a mistake
- {//here get pivot and balance factor
- pivot = ptr;
- BF = lHeight – rHeight;
- }
- return (lHeight > rHeight ? lHeight + 1 : rHeight + 1);
- }
- else
- return 0;
- }
- }
- public void printTree(Node ptr, int level)//输出二叉树
- {
- if (ptr == null)
- return;
- else
- {
- if (ptr.rchild != null)
- printTree(ptr.rchild, level + 1);
- for (int i = 0; i < level; i++)
- Console.Write(” “);
- Console.WriteLine(ptr.data);
- if (ptr.lchild != null)
- printTree(ptr.lchild, level + 1);
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- char ch;
- int data, BF;
- Node pivot;
- BTree b = new BTree();
- while (true)
- {
- Console.Clear();
- BF = 0;
- pivot = null;
- b.getHeight(b.ROOT, ref pivot, ref BF);
- if (pivot != null)
- b.balanceTree(ref pivot, ref BF);
- Console.WriteLine(“print B-Tree:”);
- b.printTree(b.ROOT, 1);
- Console.WriteLine(“——MENU——“);
- Console.WriteLine(“1. inseret data”);
- Console.WriteLine(“2. delete data”);
- Console.WriteLine(“3. Exit”);
- ch = char.Parse(Console.ReadLine());
- switch (ch)
- {
- case ‘1’:
- {
- Console.Write(“enter a data:”);
- data = int.Parse(Console.ReadLine());
- b.insert(data);
- break;
- }
- case ‘2’:
- {
- Console.WriteLine(“enter a data:”);
- data = int.Parse(Console.ReadLine());
- b.delete(data);
- break;
- }
- case ‘3’:
- return;
- default:
- {
- Console.WriteLine(“Invalid option”);
- break;
- }
- }
- }
- }
- }
- }
平衡二叉树(B-Tree) C#
原文作者:平衡二叉树
原文地址: https://blog.csdn.net/zen_chou/article/details/3016471
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/zen_chou/article/details/3016471
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。