求 二叉树的查找节点算法 的 代码和流程图
要求用c#语言编写 希望能快速回答
代码和图如下,有帮助的话记得采纳哦!
class Node
{
public int Value { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public Node(int value)
{
Value = value;
Left = null;
Right = null;
}
}
class BinaryTree
{
public Node Root { get; set; }
public Node Find(int value)
{
return Find(Root, value);
}
private Node Find(Node current, int value)
{
if (current == null)
{
return null;
}
if (current.Value == value)
{
return current;
}
var left = Find(current.Left, value);
if (left != null)
{
return left;
}
return Find(current.Right, value);
}
}
上面的代码定义了一个 Node 类和一个 BinaryTree 类。 Node 类包含一个整型值和左右子节点的引用。 BinaryTree 类包含一个根节点的引用。 Find 方法在二叉树中查找给定值,并返回包含该值的节点。如果找不到该值,则返回 null。
图:
+-------------+
| Find(value) |
+------+-------+
|
v
+--------------+
| current == null|
+-------+--------+
|yes
|
v
+--------------+
| return null|
+--------------+
|no
|
v
+--------------+
| current.Value |
| == value |
+-------+--------+
|yes
|
v
+--------------+
| return current|
+--------------+
|no
|
v
+-------------+
| left = |
| Find(current|
| .Left, value)|
+-------------+
|
v
+--------------+
| left != null|
+-------+--------+
|yes
|
v
+--------------+
| return left|
+--------------+
|no
|
v
+-------------+
| return |
| Find(current|
| .Right, value)|
+-------------+
该流程图展示了在二叉树中查找特定值时,程序会从根节点开始遍历,依次比较当前节点的值和要查找的值。如果当前节点的值等于要查找的值,则返回该节点。如果当前节点的值不等于要查找的值,则继续在左子树或右子树中递归查找。如果遍历完整棵树都没有找到该值,则返回 null。
这里给一个测试案例:
class Program
{
static void Main(string[] args)
{
// 创建一个二叉树
var root = new Node(5);
root.Left = new Node(3);
root.Right = new Node(7);
root.Left.Left = new Node(2);
root.Left.Right = new Node(4);
root.Right.Left = new Node(6);
root.Right.Right = new Node(8);
// 创建一个二叉树对象
var tree = new BinaryTree();
tree.Root = root;
// 搜索特定值,这个给个8,我们查找试试
var value = 8;
var result = tree.Find(value);
if (result != null)
{
Console.WriteLine("Found value: {0}", result.Value);
}
else
{
Console.WriteLine("Value not found: {0}", value);
}
Console.ReadLine();
}
}
这棵二叉树的结构如下:
5
/ \
3 7
/ \ / \
2 4 6 8
程序会在二叉树中查找值为 8 的节点。在这个例子中,程序会在树的最右边找到一个节点,值为 8,并在控制台输出 "Found value: 8"。
如果你需要查找的值不存在于二叉树中,程序将在控制台输出 "Value not found: 8"。
希望有所帮助
#include<stdio.h>
#include<stdlib.h>
typedef struct Node//二叉树的二叉链表存储表示;
{
char data;//节点数据域;
struct Node*lchild;//左孩子指针;
struct Node*rchild;//右孩子指针;
} Node,*Tree;
void CreateTree(Tree*T)
//先序创建二叉树;
{
char ch;
scanf("%c",&ch);//输入字符;
if(ch=='#')
(*T)=NULL;//递归结束,为空树;
else
{
*T=(Tree)malloc(sizeof(Node));//创建二叉树空间;
(*T)->data=ch;//根节点数据域置为ch;
CreateTree(&(*T)->lchild);//递归创建左子树;
CreateTree(&(*T)->rchild);//递归创建右子树;
}
}
int NodeCount(Tree T)
//统计二叉树的节点个数;
{
if(T==NULL) return 0;//如果是空树,节点个数为0;
else return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
//节点个数=左子树节点+右子树节点+根节点;
}
int main( )
{
printf("创建一个带节点的二叉树链表: ");
Tree T=NULL;//此时未创建二叉树;
CreateTree(&T);//& 创立一颗二叉树;
printf("二叉树的节点总数为:%d ",NodeCount(T));
}
class Node
{
public int data;
public Node left;
public Node right;
public Node(int data)
{
this.data = data;
left = null;
right = null;
}
}
class BinaryTree
{
public Node root;
public BinaryTree()
{
root = null;
}
public Node Search(Node root, int key)
{
if (root == null || root.data == key)
return root;
if (root.data > key)
return Search(root.left, key);
return Search(root.right, key);
}
}