java二叉树前序方法增加一个新的节点,然后把另一个节点的数据插入到这个新节点

怎么在二叉树中插入一个新的节点~

二叉树节点的查找、插入、删除.用C语言做的,不懂的地方可以给我留言。,

#include
#include

typedef int elemtype;
typedef struct Node
{
elemtype data;
struct Node *Lchild;
struct Node *Rchild;
}TreeNode;
typedef TreeNode *bt;

int
Search_data(TreeNode *t,TreeNode **p,TreeNode **q, elemtype x) //查找函数
{
int flag=0;
*p=NULL;
*q=t;

while(*q)
{
if (x>(*q)->data)
{
*p=*q;
*q=(*q)->Rchild;
}
else{
if (xdata)
{
*p=*q;
*q=(*q)->Lchild;
}
else{
flag=1;
break;
}
}
}
return flag;
}

int
InsertNode(TreeNode **t,elemtype x) //插入函数
{
int flag =0;
TreeNode *q,*s;
TreeNode *p=*t;

if (,Search_data(*t,&p,&q,x))
{
s=(TreeNode *)malloc(sizeof(TreeNode));
s->data=x;
s->Lchild=NULL;
s->Rchild=NULL;
flag=1;
if(,p) t=s;
else{
if(x>p->data)
p->Rchild=s;
else
p->Lchild=s;
}
}
return flag;
}

int
DeleteNode(TreeNode **t,elemtype x) //删除函数
{
int flag=0;
TreeNode *q,*s,**f;
TreeNode *p=*t;

if (Search_data(*t,&p,&q,x))
{
flag=1;
if(p==q) f=&(*t);
else
{
f=&(p->Lchild);
if(x>p->data)
f=&(p->Rchild);
}

if(,q->Rchild)
*f=q->Lchild;
else{
if(,q->Lchild)
*f=q->Rchild;
else{
p=q->Rchild;
s=p;
while(p->Lchild){
s=p;
p=q->Lchild;
}
*f=p;
p->Lchild=q->Lchild;

if (s,=p)
{
s->Lchild=p->Rchild;
p->Rchild=q->Rchild;
}
}
}
free(q);
}
return flag;
}


void
visit(bt t)
{
printf("%c",t->data);
}

TreeNode *
creat_Tree()
{
char ch;
bt t;
ch=getchar();
if(ch==' ') return (NULL);
else{
t=(TreeNode *)malloc(sizeof(TreeNode));
t->data=ch;
t->Lchild=creat_Tree();
t->Rchild=creat_Tree();
printf("%x
",&t);
return (t);
}
}

void
mid_oderTree(bt t)
{
if (t,=NULL)
{
mid_oderTree(t->Lchild);
visit(t);
mid_oderTree(t->Rchild);
}
}

int
count_leafTree(bt t)
{
int i;
if(t==NULL) return 0;
else
if(t->Lchild==NULL&&t->Rchild==NULL)
i=1;
else i=count_leafTree(t->Lchild)+
count_leafTree(t->Rchild);
return i;
}

main()
{
TreeNode *t,*p,*q;
elemtype x;
x='M';
printf("creat Tree:
");
//二叉树在遍历最后一个节点之后,遇到结束符结束建立树。
t=creat_Tree();
printf("中根遍历树:
");
mid_oderTree(t);

printf("
中根序插入%c成功输出(是1否0):%d
",x,InsertNode(&t,x));

printf("插入%c后的查找成功输出(是1否0):%d
",x,Search_data(t,&p,&q, x));
printf("插入后的中根遍历树:
");
mid_oderTree(t);

printf("
删除%c成功输出(是1否0):%d
",x,DeleteNode(&t,x));
printf("删除后的中根遍历树:
");
mid_oderTree(t);

printf("
求树的叶子数:%d
",count_leafTree(t));。

$(function(){ var setting = { view:{showLine:true, showIcon: false}, //有没有连线和显不示显示图标 //下面语句可以让pid相同的分层 data: { simpleData: { enable: true //自动成圣树结构 } }, callback: { onClick: zTreeOnClick //回调点击函数 } }; //动态获取数据自动生成树 $.ajax({ type: "post", url: "../tools/admin_ajax.ashx?action=GetCallContent&time=" + Math.random(),//链接地址 dataType: "html", success: function (data) { //转换一下json var myNodes = JSON.parse(data); var zNodes = []; // alert(myNodes); for (var i = 0; i 判断树节点与点击的节点对应的id是否相等 // alert(item); var cont = ''; cont+=item.answer; $('#tab_content_ztree_explain').html(cont); //赋值给div // $('treeNode.name').html(answ); } } } }); }; });

叶子节点:没有孩子节点的节点也就是说,当我们明白了叶子节点的定义后,只需要遍历一遍二叉树,把符合这种条件(左孩子节点和右孩子节点都为NULL的节点)的节点统计出来就可以了。于是,实际上这个问题也就转化成了如何遍历二叉树?很显然,遍历二叉树是可以有多种方式的,如:前序遍历(递归/非递归)、中序遍历(递归/非递归)、后序遍历(递归/非递归)、层次遍历等等。下面我将给出使用递归前序遍历以及层次遍历两种思路实现的求解叶子节点的示例代码吧,仅供参考。其他几种实现方式思路类似,可自行尝试。示例代码如下:package cn.zifangsky.tree.questions;import org.junit.Test;import cn.zifangsky.queue.LinkQueue;import cn.zifangsky.tree.BinaryTreeNode;/** * 求二叉树中叶子节点的个数 * @author Administrator * */public class Question2 {/** * 通过递归前序遍历获取叶子节点个数 * @param root * @return */public int getNumberOfLeavesByPreOrder(BinaryTreeNode root){if(root == null){return 0;}else{if(root.getLeft() == null && root.getRight() == null){ //叶子节点return 1;}else{return getNumberOfLeavesByPreOrder(root.getLeft()) + getNumberOfLeavesByPreOrder(root.getRight());}}}/** * 使用层次遍历获取二叉树叶子节点个数 * * @时间复杂度 O(n) * @param root */public int getNumberOfLeavesByQueue(BinaryTreeNode root){int count = 0; //叶子节点总数LinkQueue queue = new LinkQueue();if(root != null){queue.enQueue(root);}while (!queue.isEmpty()) {BinaryTreeNode temp = (BinaryTreeNode) queue.deQueue();//叶子节点:左孩子节点和右孩子节点都为NULL的节点if(temp.getLeft() == null && temp.getRight() == null){count++;}else{if(temp.getLeft() != null){queue.enQueue(temp.getLeft());}if(temp.getRight() != null){queue.enQueue(temp.getRight());}}}return count;}/** * 测试用例 */@Testpublic void testMethods(){/** * 使用队列构造一个供测试使用的二叉树 * 1 * 2 3 * 4 5 6 7 * 8 9 */LinkQueue queue = new LinkQueue();BinaryTreeNode root = new BinaryTreeNode(1); //根节点queue.enQueue(root);BinaryTreeNode temp = null;for(int i=2;i */public class LinkQueue{private SinglyNode frontNode; //队首节点private SinglyNode rearNode; //队尾节点public LinkQueue() {frontNode = null;rearNode = null;}/** * 返回队列是否为空 * @时间复杂度 O(1) * @return */public boolean isEmpty(){return (frontNode == null);}/** * 返回存储在队列的元素个数 * @时间复杂度 O(n) * @return */public int size(){int length = 0;SinglyNode currentNode = frontNode;while (currentNode != null) {length++;currentNode = currentNode.getNext();}return length;}/** * 入队:在链表表尾插入数据 * @时间复杂度 O(1) * @param data */public void enQueue(K data){SinglyNode newNode = new SinglyNode(data);if(rearNode != null){rearNode.setNext(newNode);}rearNode = newNode;if(frontNode == null){frontNode = rearNode;}}/** * 出队:删除表头节点 * @时间复杂度 O(1) * @return */public Object deQueue(){if(isEmpty()){throw new RuntimeException("Queue Empty!");}else{Object result = frontNode.getData();if(frontNode == rearNode){frontNode = null;rearNode = null;}else{frontNode = frontNode.getNext();}return result;}}}单链表节点SinglyNode的定义:package cn.zifangsky.linkedlist;/** * 单链表的定义 * @author zifangsky * @param */public class SinglyNode {private K data; // 数据private SinglyNode next; // 该节点的下个节点public SinglyNode(K data) {this.data = data;}public SinglyNode(K data, SinglyNode next) {this.data = data;this.next = next;}public K getData() {return data;}public void setData(K data) {this.data = data;}public SinglyNode getNext() {return next;}public void setNext(SinglyNode next) {this.next = next;}@Overridepublic String toString() {return "SinglyNode [data=" + data + "]";}}

怎么写二叉树的先序遍历、中序遍历、后序遍历?
答:一、先序遍历 :1、访问根节点 2、前序遍历 左 子树 3、前序遍历 右子 树 二、中序遍历 :1、中序遍历左子树 2、访问根节点 3、中序遍历右子树 三、后序 遍历:1、后序遍历 左子树 2、后序遍历右子树 3、访问根节点 下面介绍一下例子与方法:1、画树求法:第一步,根据前序遍历的特点...

假如我已经用java实现了二叉树的插入。 我把一个没有拍学的整数数列里面...
答:public int[] createArray (Node root, int[] a){ if (root.left != null)a = createArray(root.left,a);a.add[root.value];if (root.right !=null)a = createArray(root.right,a);return a;}

利用JAVA 先序建立二叉树 #表示空树。例如输入ABC##DE#G##F### 先...
答:static Node CreateTree()// 先序建立二叉树 { Node node = null;if (ch[i] == '#') { node = null;i++;}else { node = new Node();node.data = ch[i];i++;node.left = CreateTree();node.Right = CreateTree();} return node;} static public void preorder(Node node)/...

什么是二叉树的先序、中序和后续遍历?
答:二叉树前序中序后序口诀:前序遍历:根节点—-左子树—-右子树,中序遍历:左子树—-根节点—-右子树,后序遍历:左子树—-右子树—-根节点 先序:是二叉树遍历中的一种,即先访问根结点,然后遍历左子树,后遍历右子树。遍历左、右子树时,先访问根结点,后遍历左子树,后遍历右子树,如果二叉树...

java构建二叉树算法
答:}//前序遍历二叉树 public static void preOrder(BinTree parent){ if(parent == null)return;System.out.print(parent.data+" ");preOrder(parent.left);preOrder(parent.right);}//中序遍历二叉树 public void inOrder(BinTree parent){ if(parent == null)return;inOrder(parent.left);Sys...

用java实现二叉树
答:那么,碰巧要找的数字位于99999那个地方,那查找的速度将很慢,因为要从第1个依次往 后取,取出来后进行比较。平衡二叉树(构建平衡二叉树需要先排序,我们这里就不作考虑 了)可以很好地解决这个问题,但二叉树的遍历(前序,中序,后序)效率要比数组低很多,public class Node { public int value;...

为什么二叉树中的前序中序后序的顺序?
答:二叉树前序中序后序是访问排列的主要方式。二叉树是一种树形结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的遍历方式有三种:前序遍历、中序遍历和后序遍历。前序遍历的方式是首先访问根节点,然后访问左子树,最后访问右子树。中序遍历的方式是首先访问左子树,接着访问根结点...

设计算法求二叉树所包含的度为1的结点的数目。(给出设计思想,再用代码...
答:擅长:JAVA相关 C/C++ 编程语言 数据结构及算法 软件开发 向TA提问 私信TA 展开全部 1、先序遍历的方式创建一棵二叉树 2、先序遍历的方式查找该二叉树,如果结点的度为1,则counts++。 3、代码如下: #include<stdio.h>#include<malloc.h> #define Max_size 1000 struct Tree{//存节点信息 char ch; Tree...

用递归算法先序中序后序遍历二叉树
答:1、先序 void PreOrderTraversal(BinTree BT){ if( BT ){ printf(“%d\n”, BT->Data); //对节点做些访问比如打印 PreOrderTraversal(BT->Left); //访问左儿子 PreOrderTraversal(BT->Right); //访问右儿子 } } 2、中序 void InOrderTraversal(BinTree BT){ if(BT){ InOrde...

二叉树的中序、前序、后序的递归、非递归遍历算法,层次序的非递归遍历...
答://二叉树前序遍历递归实现 void preorder(bintree t)//t是指针变量,而不是结点结构体变量 {if(t){ cout<<t->data<<" ";preorder(t->lchild);preorder(t->rchild);} } //二叉树前序遍历非递归实现 void preorder1(bintree t){ seqstack s;s.top=-1;//top 的初始值为-1;while...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网