摘要: 1.非递归,不允许用栈,求前序遍历最后一个结点思想:前序遍历最后一个结点:若有右子树,则为右子树最右下结点,若有左子树,则为左子树最右下结点,否则为根结点。#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ int data; struct BTreeNode *lchild,*rchild;}BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}BTree *LastNode(BTree *b) 阅读全文
posted @ 2012-08-13 21:06 代码改变未来 阅读(1488) 评论(0) 推荐(0) 编辑
摘要: 1.打印值为x的结点的所有祖先栈从0开始,top初始值-1#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ int data; struct BTreeNode *lchild,*rchild;}BTree;typedef struct stacknode{ BTree *node; int tag;//tag为0表示结点左子女已经访问,为1表示右子女已经访问}stack;int _tmain(int argc, _TCHAR* argv[]){ 阅读全文
posted @ 2012-08-10 22:51 代码改变未来 阅读(293) 评论(0) 推荐(0) 编辑
摘要: 2.中序遍历 3.后序遍历 4.使用栈的非递归遍历 非递归实现 根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下: 阅读全文
posted @ 2012-08-08 13:50 代码改变未来 阅读(274) 评论(0) 推荐(0) 编辑
摘要: 算法1:若无左子女则不应该有右子女 2.求二叉树宽度 3.二叉树k层叶子结点 阅读全文
posted @ 2012-08-06 22:32 代码改变未来 阅读(3405) 评论(0) 推荐(1) 编辑
摘要: 3)分别求二叉树的叶结点,度数为1的结点,度数为2的结点。#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ float data; char optr; struct BTreeNode *lchild,*rchild;}BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}int n0,n1,n2;//全局变量,用于统计结点数void Count(BTree *t){ if(t)//前序遍历 阅读全文
posted @ 2012-08-03 23:37 代码改变未来 阅读(453) 评论(0) 推荐(0) 编辑
摘要: 1)二叉树仅含二元运算符,存储在二叉树BT中,写出计算该算术表达式的值的算法。#include "stdafx.h"#include<iostream>using namespace std;typedef struct BTreeNode{ float data; char optr; struct BTreeNode *lchild,*rchild;}BiNode,*BTree;int _tmain(int argc, _TCHAR* argv[]){ return 0;}float PostEval(BTree bt)//后序遍历{ float lv,rv 阅读全文
posted @ 2012-08-03 21:41 代码改变未来 阅读(476) 评论(0) 推荐(0) 编辑
摘要: 12)按递增次序输出链表结点#include "stdafx.h"#include<iostream>using namespace std;typedef struct node{ int data; struct node *next;}Listnode,*LinkList;int _tmain(int argc, _TCHAR* argv[]){ return 0;}void reverse(LinkList &La){ while(La->next) { LinkList pre=La; LinkList r=pre;//r为辅助指针记录前 阅读全文
posted @ 2012-08-02 23:52 代码改变未来 阅读(194) 评论(0) 推荐(0) 编辑
摘要: 9)【判断b链表是否a链表的子序列】假设带头结点错误代码:没有考虑重复结点的情况。#include "stdafx.h"#include<iostream>using namespace std;typedef struct node{ int data; struct node *next;}Listnode,*LinkList;int _tmain(int argc, _TCHAR* argv[]){ return 0;}bool isofseries(LinkList a,LinkList b){ LinkList pa=a->next,pb=b-& 阅读全文
posted @ 2012-08-01 21:53 代码改变未来 阅读(249) 评论(0) 推荐(0) 编辑
摘要: 本节主要针对删除和移动链表数据。7)【删除】删除带头结点的链表的最小值。#include "stdafx.h"#include<iostream>using namespace std;typedef struct node{ int data; struct node *next;}Listnode,*LinkList;int _tmain(int argc, _TCHAR* argv[]){ return 0;}void deletemin(LinkList &La){ LinkList p=La->next;//p为工作指针 LinkList 阅读全文
posted @ 2012-07-31 22:26 代码改变未来 阅读(254) 评论(0) 推荐(0) 编辑
摘要: 本随笔描述链表的重新排列和拆分。4)不带头结点的链表,要求将链表按数据域的值从小到大重新链接。不能使用额外的结点空间。本题有一定难度#include "stdafx.h"#include<iostream>using namespace std;typedef struct node{ int data; struct node *next;}Listnode,*LinkList;int _tmain(int argc, _TCHAR* argv[]){ return 0;}void Sort(LinkList &La)//使用直接插入排序算法{ Lin 阅读全文
posted @ 2012-07-28 21:40 代码改变未来 阅读(279) 评论(0) 推荐(0) 编辑