- 浏览: 17212 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
二叉树,单链表,反向链表,stack栈 二叉查找树
// 二叉查找树节点 Binary search tree node
public class BinarySearchTreeNode
{ public int key;// 二叉查找树节点的值
public BinarySearchTreeNode left;// 二叉查找树节点的左子节点
public BinarySearchTreeNode right;// 二叉查找树节点的右子节点
/// 二叉查找树节点构造函数
public BinarySearchTreeNode(int nodeValue)
{ key = nodeValue;//nodeValue 节点的值
left = null; right = null;
}
/// 插入节点
public void InsertNode(BinarySearchTreeNode node)
{ if(node.key > this.key)
{ if(this.right == null)
{ this.right = node;//node插入的节点
return;
}
else
this.right.InsertNode(node);
}
else
{ if(this.left == null)
{ this.left = node; return; }
else
this.left.InsertNode(node);
}
}
/// 二叉查找树查询
public bool SearchKey(int searchValue)
{ if(this.key == searchValue)//searchValue需要查询的值
return true;// 是否找到查询的值
if(searchValue > this.key)
{ if(this.right == null) return false;
else
return this.right.SearchKey(searchValue);
}
else
{ if(this.left == null) return false;
else
return this.left.SearchKey(searchValue);
}
}
// 中序遍历
public void MiddleDisplay()
{ if(this.left != null)
this.left.MiddleDisplay();
Console.WriteLine(this.key);
if(this.right != null)
this.right.MiddleDisplay();
}
// 前序遍历
public void FrontDisplay()
{ Console.WriteLine(this.key);
if(this.left != null)
this.left.FrontDisplay();
if(this.right != null)
this.right.FrontDisplay();
}
// 后序遍历
public void BehindDisplay()
{ if(this.left != null)
this.left.BehindDisplay();
if(this.right != null)
this.right.BehindDisplay();
Console.WriteLine(this.key);
}
}
/// 二叉查找树
public class BinarySearchTree
{ private BinarySearchTreeNode root;
/// 生成一个二叉查找树
public BinarySearchTree()
{ root = nul; }
/// 生成一个二叉查找树
/// <param name="nodeValue">二叉查找树根节点的值</param>
public BinarySearchTree(int nodeValue)
{ root = new BinarySearchTreeNode(nodeValue); }
/// 在二叉查找树上插入一个节点
/// <param name="nodeValue">插入节点的值</param>
public void InsertBinarySearchTreeNode(int nodeValue)
{ BinarySearchTreeNode insertNode = new BinarySearchTreeNode(nodeValue);
if(root == null)
{ root = insertNode;
return;
}
else
root.InsertNode(insertNode);
return;
}
/// 在二叉查找树上查询一个数
/// <param name="searchValue">需要查询的值</param>
/// <returns>是否找到查询的值</returns>
public bool SearchKey(int searchValue)
{ if(root.key == searchValue) return true;
else
return root.SearchKey(searchValue);
}
/// 二叉查找树中序遍历
public void MiddleDisplay()
{ root.MiddeleDisplay(); return; }
/// 二叉查找树前序遍历
public void FrontDisplay()
{ root.FrontDisplay(); return; }
/// 二叉查找树后序遍历
public void BehindDisplay()
{ root.BehindDisplay(); return; }
/// 二叉查找树排序
/// <param name="a">需要排序的数组</param>
public static void BinarySearchTreeSort(int [] a)
{ BinarySearchTree t = new BinarySearchTree();
for(int i = 0; i < a.Length; i ++)
t.InsertBinarySearchTreeNode(a[i]);
t.MiddleDisplay();return;
}/// 二叉查找树查找
/// <param name="a">进行查找的数组</param>
/// <param name="searchKey">需要查找的树</param>
public static bool BinarySearchTreeSearch(int [] a, int searchKey)
{ BinarySearchTree t = new BinarySearchTree();
for(int i = 0; i < a.Length; i ++)
t.InsertBinarySearchTreeNode(a[i]);
return t.SearchKey(searchKey);
}
}
namespace 二叉树
{ class Node
{ int n;
public Node(int x)
{ n=x; }
public Node Left;
public Node Right;
public void Insert(Node node)
{ if(node.n > this.n)
{ if(this.Right == null)
this.Right = node;
else
this.Right.Insert(node); }
else
{ if(this.Left == null)
{ this.Left = node; }
else
{ this.Left.Insert(node); } } } //递归
public void Show()
{ Console.WriteLine(n); } }
class BinaryTree
{ Node root;
public void GenerateTree(Node node) //高内聚,低耦合
{ if(root == null)
{ root = node; return; }//如果树是空,第一次加节点
root.Insert(node);
}
public void ShowInOrder(Node node) //中序遍历(in order):左中右。先(前)序遍历(pre order):中左右。后序遍历(post order):左右中。
{ if(node == null) return;//递归必须有个终止条件,递归方法中一定要接受参数
ShowInOrder(node.Left);
node.Show();
ShowInOrder(node.Right);
}
public void Show()
{ ShowInOrder(root); }
}
class A
{ static void Main()
{ BinaryTree b = new BinaryTree();
Node node = new Node(5);
b.GenerateTree(node);
node = new Node(13);
b.GenerateTree(node);
node = new Node(6);
b.GenerateTree(node);
node = new Node(26);
b.GenerateTree(node);
node = new Node(7);
b.GenerateTree(node);
b.Show(); } } } 结果:5,6,7,13,26
单链表
class Node
{ int a;
public Node(int a)
{ this.a=a; }
public int A
{get{return a;} set{a=value;} }
public Node next;
}
class LinkedList
{ Node header;
public void Generate(int x)
{ if(header==null)
header=new Node(x);
else
{ Node n = new Node(x);
if(n.A < header.A)
{ n.next = header;
header=n;
return;
}
Node tmp=header;
Node t=header;
while(tmp.A < n.A)
{ t=tmp; //为了下一次循环
tmp=tmp.next;
if(tmp==null)
break;
}
t.next=n;
n.next=tmp;
}
}
public void Out()
{ Node tmp=header;
while(tmp!=null)
{ Console.WriteLine(tmp.A);
tmp = tmp.next;
} } }
class Test
{ static void Main()
{ LinkedList ll = new LinkedList();
ll.Generate(6);
ll.Generate(36);
ll.Generate(26);
ll.Generate(16);
ll.Out();
} } }
反向链表
class Link //this class reverse the LinkedList
{ public int a;
public Link next;
}
class createLink //the class create the LinkedList
{
Link header=null;
Link p = null;
Link temp = null;
Link l=null; //Link k=null;
Link g=null;
public void create()
{ string str;
int i;
Console.WriteLine("Please enter number:");
str=Console.ReadLine();
while(str!="y")
{ i=Convert.ToInt32(str);
temp=new Link();
temp.a=i;
temp.next=null;
if(g==null)
g=temp;
if(header==null)
header=temp;
if(p==null)
p=temp;
else
{ p.next=temp;
p=p.next;
}
Console.WriteLine("please enter number:");
str=Console.ReadLine();
}
}
public void display()
{ while(header!=null)
{ Console.WriteLine(header.a);
header=header.next;
}
}
public void reversed() // the mothod reversed the LinkedList
{ Link k=null;
Link tmp=null;
Link com =null;
if(tmp==null)
tmp=header.next;
while(tmp!=null)
{ // if(com==null)
// com=header;
l=tmp;
if(k==null)
{ header.next=null;
k=header;
}
com=header;
header=l;
tmp=l.next;
l.next=com;
}
}
public void show()
{ while(l!=null)
{ Console.WriteLine(l.a);
l=l.next;
} } }
class Tester
{ static void Main()
{ createLink cl=new createLink();
cl.create();
//cl.display();
cl.reversed();
cl.show();
} } }
Stack 栈
class Node
{ int a;
public Node(int a)
{ this.a=a; }
public int A
{ get{return a;} set{a=value;} }
public Node next;
}
class LinkedList
{ protected Node header;
public void Generate(int x)
{ if(header==null)
header=new Node(x);
else
{ Node n = new Node(x);
n.next=header;
header=n;
}
}
public void Out()
{ Node tmp=header;
while(tmp!=null)
{ Console.WriteLine(tmp.A);
tmp = tmp.next;
} } }
class Stack : LinkedList
{ public void Push(int x)
{ this.Generate(x); }
public int Pop()
{ if(this.header == null)
return -1; // empty stack
int n = header.A;
header = header.next;
return n;
}
}
class Test
{ static void Main()
{ Stack ss = new Stack();
ss.Push(7);
ss.Push(78);
ss.Push(9);
ss.Push(2);
int i = ss.Pop();
while(i != -1)
{ Console.WriteLine(i);
i = ss.Pop();
} } } }
相关推荐
二叉树的二叉链表源码
在这个主题中,我们将深入探讨几个关键概念:单链表、二叉树遍历、折半查找、二叉排序树以及内部排序。 首先,单链表是一种基本的数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。这种线性...
在这个项目中,我们关注的是五种重要的数据结构及其应用:单链表、表达式求值、二叉树、二叉排序树和Huffman编码。这些概念在算法设计、编译器构建、文件压缩等领域都有广泛应用。 首先,单链表是一种线性数据结构...
最后,二叉查找树(Binary Search Tree, BST)是一种自平衡的二叉树,每个节点的左子树只包含小于当前节点的元素,右子树只包含大于当前节点的元素。这种特性使得二叉查找树在搜索、插入和删除操作上具有较高的效率...
用C++实现的最优二叉查找树,简单,明了,是数据结构里经典必学算法,初学者适用
"第六章.ppt" 文件可能包含了关于二叉树的深入理论、示例和练习,可能涵盖了二叉树的性质、查找算法、排序算法(如二叉搜索树)以及与二叉链表相关的复杂操作。例如,可能讲解了如何判断一棵二叉树是否平衡,或者...
根据给定的文件信息,我们将深入探讨如何通过不同的方法来判断一棵二叉树是否为二叉排序树(Binary Search Tree, BST)。二叉排序树是一种特殊的二叉树,它满足以下条件: 1. 若左子树不为空,则左子树上所有节点的...
在这个实验中,我们专注于四个关键的算法和数据结构:单链表的基本操作、二叉树的遍历、折半查找以及二叉排序树,还有内部排序的实现。 首先,单链表是一种线性数据结构,其中每个元素(节点)包含数据和一个指向下...
数据结构课程设计之二叉树采用二叉链表作为存储结构 本课程设计的主要任务是设计并实现一个二叉树的存储结构,使用二叉链表作为存储结构,并实现按层次顺序遍历二叉树的算法。下面是本设计的详细解释和实现过程: ...
二叉查找树(Binary Search Tree,BST),也称为二叉排序树,是一种特殊的二叉树数据结构。它的每个节点都包含一个键(key)、一个关联的值、一个指向左子节点的指针和一个指向右子节点的指针。在二叉查找树中,对于...
"二叉树的二叉链表存储表示" 在计算机科学中,二叉树是一种重要的数据结构,它广泛应用于许多领域,如数据库、操作系统、编译器等。二叉树的存储表示是指将二叉树存储在计算机中的一种方式,其中二叉链表存储表示是...
本项目提供了在VC++6.0环境下运行的二叉树和二叉查找树(BST)的实现,这对于学习数据结构的初学者来说是一份宝贵的资源。 首先,我们来深入了解一下二叉树的基本概念。二叉树是由节点构成的层次结构,每个节点包含...
设一棵二叉树以二叉链表表示,试编写有关二叉树的递归算法
"用顺序和二叉链表作存储结构实现二叉排序树" 本课程设计旨在使用顺序和二叉链表作为存储结构来实现二叉排序树。二叉排序树是一种重要的数据结构,它广泛应用于计算机科学和技术领域。通过本课程设计,我们将学习...
二叉查找树(Binary Search Tree,BST),也称为二叉排序树,是一种特殊的二叉树数据结构,它的每个节点都包含一个键值,且满足以下性质:对于任意节点,其左子树中的所有节点的键值都小于该节点的键值,而右子树中...
二叉查找树,又称为二叉搜索树或二叉排序树,是一种特殊类型的二叉树,它具有以下特性: 1. **定义**: - 每个节点包含一个关键字(key),所有节点的关键字都是唯一的。 - 对于任何非空节点,其左子树中的所有...
二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree)。其定义为:二叉排序树或者是空树,或者是满足如下性质的二叉树:1.若它的左子树非空,则左子树上所有结点的值均小于根结点的值;2.若它的右...
二叉查找树(Binary Search Tree,BST)是一种特殊的二叉树结构,它的每个节点都包含一个键值(key)、一个关联的数据或值、一个指向左子树的引用和一个指向右子树的引用。二叉查找树的主要特性是:对于任意节点,其...
【二叉排序树】,全称为...总结来说,二叉排序树是一种重要的数据结构,它结合了数组的高效查找和链表的灵活插入与删除。在C语言中,通过结构体和指针可以方便地实现二叉排序树的各种操作,为数据处理提供了强大工具。
判别给定二叉树是否为二叉排序树