关于2叉树 请高手看看这程序是什么问题(急(
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct node
{
char data;
struct node *rchild,*lchild;
}node;
node *creattree(node *root)
{
char ch;
cin> > ch;
if(ch == ' ')
{
root = NULL;
return root;
}
else
{
root = (node *)malloc(sizeof(node));
root-> data = ch;
creattree(root-> lchild);
creattree(root-> rchild);
return root;
}
}
void outtree(node *root)
{
if(root!=NULL)
{
cout < <root -> lchild;
outtree(root-> lchild);
outtree(root-> rchild);
}
}
int main()
{
node *head,*root;
//head = (node*)malloc(sizeof(node));
root = (node*)malloc(sizeof(node));
//head-> lchild = root,head-> rchild = NULL;
root-> lchild =root-> rchild = 0;
cout < < "please enter data of the tree: " < <endl;
root = creattree(root);
outtree(root);
return 1;
}
问题描述:
不能打印出 建立的2叉树~!~!~~~~~
[解决办法]
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct node
{
char data;
struct node *rchild,*lchild;
}node;
node *creattree(node *&root)//参数类型应为node *&,因为构建的树需要保存!
{
char ch;
cin> > ch;
if(ch == '# ')//以 '# '为空接点表示符,最好不要用空格 ' '
{
root = NULL;
return root;
}
else
{
root = (node *)malloc(sizeof(node));
root-> data = ch;
creattree(root-> lchild);
creattree(root-> rchild);
return root;
}
}
void outtree(node *root)
{
if(root!=NULL)
{
cout < <root -> data;//输出数据域,lchild改为data
outtree(root-> lchild);
outtree(root-> rchild);
}
}
int main()
{
node *root;//原来的*head多余!
//head = (node*)malloc(sizeof(node));
root = (node*)malloc(sizeof(node));
//root-> lchild =root-> rchild = 0;//这句是多余的!
cout < < "please enter data of the tree: " < <endl;
root = creattree(root);
outtree(root);
return 1;
}
如对我说的 "参数类型应为node *&,因为构建的树需要保存! "有疑问的话,可以参考下贴:
http://community.csdn.net/Expert/topic/5528/5528213.xml?temp=.7316095
[解决办法]
二叉树没有建成功
参数类型应为node *&,把指针传进去,按照你写的,返回时候root仍然是指向NULL,不信可以调试
同意LS的
[解决办法]
函数的参数除了数组和引用都是不传递的
但是函数的返回了指针,虽然root变了(我认为变了,main中,root = creattree(root))
但是他的左右儿子没有接受返回值,所以大概root不为空,但没有左右儿子吧.
指针初值的处理在不同编译器上好象不大一样,建议在申请空间时将指针置空
[解决办法]
先mark,改天看看
[解决办法]
问下medie2005(阿诺) :
下面的这几行代码,能够正常生成一颗二叉树:
class BinNode {
public:
BinNode(char data) {
this-> data = data;
this-> leftChild = leftChild;
this-> rightChild = rightChild;
}
void setLeftChild(BinNode *leftChild) {
this-> leftChild = leftChild;
}
void setRightChild(BinNode *rightChild) {
this-> rightChild = rightChild;
}
private:
char data;
BinNode *leftChild;
BinNode *rightChild;
};
BinNode *create(void) {
char ch;
if (ch == '# ') {
return NULL;
}
else {
BinNode *t = new BinNode(ch);
t-> setLeftChild(create());
t-> setRightChild(create());
return t;
}
}
上面这个程序能成功建立一颗二叉树。这里的t被保存了
楼主的这行
root = (node *)malloc(sizeof(node));
root为什么没有被保存呢,root不也一样被保存到本层次的活动记录里了吗?
[解决办法]
传递指针值只改变行参值,实参不变,malloc函数分配的存储空间在堆区,main 函数里定义的root在栈区
[解决办法]
自己帖代码
package tree;
public class Tree {
TreeNode root;
public Tree() {
}
public boolean isEmpty() {
return root == null;
}
// 插入
public void insertBinaryTree(int info) {
TreeNode node = new TreeNode();
node.info = info;
if (root == null) {
root = node;
} else {
TreeNode currentNode = root;
TreeNode parent;
while (currentNode != null) {
parent = currentNode;
if (info > currentNode.info) {
currentNode = currentNode.rlink;
if (currentNode == null) {
parent.rlink = node;
}
} else if (info < currentNode.info) {
currentNode = currentNode.llink;
if (currentNode == null) {
parent.llink = node;
}
}
}
}
}
// 删除
public void treeDelete(int info) {
TreeNode tNode = serach(info);
TreeNode deleteNode = new TreeNode();
TreeNode dNodeChild = new TreeNode();
if (tNode == null) {
System.out.println("node is not exists");
} else if (tNode.llink == null || tNode.rlink == null) {
deleteNode = tNode;
} else {
deleteNode = treeSuccessor(tNode);
}
if (deleteNode.llink != null) {
dNodeChild = deleteNode.llink;
} else {
dNodeChild = deleteNode.rlink;
}
if (getFatherNode(deleteNode) == null) {
root = dNodeChild;
} else {
if (deleteNode == getFatherNode(deleteNode).llink) {
getFatherNode(deleteNode).llink = dNodeChild;
} else {
getFatherNode(deleteNode).rlink = dNodeChild;
}
}
if (deleteNode != tNode) {
tNode.info = deleteNode.info;
}
}
// 搜索
public TreeNode serach(int info) {
return treeSerach(root, info);
}
// 搜索
public TreeNode treeSerach(TreeNode tNode, int info) {
if (tNode == null || tNode.info == info) {
return tNode;
}
if (info < tNode.info) {
return treeSerach(tNode.llink, info);
} else {
return treeSerach(tNode.rlink, info);
}
}
// 最大节点
public TreeNode treeMaxiMum(TreeNode tNode) {
while (tNode.rlink != null) {
tNode = tNode.rlink;
}
return tNode;
}
// 最小节点
public TreeNode treeMiniMun(TreeNode tNode) {
while (tNode.llink != null) {
tNode = tNode.llink;
}
return tNode;
}
// 找后继
public TreeNode treeSuccessor(TreeNode tNode) {
if (tNode.rlink != null) {
return treeMiniMun(tNode.rlink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.rlink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找前驱
public TreeNode treePrecursor(TreeNode tNode) {
if (tNode.llink != null) {
return treeMaxiMum(tNode.llink);
}
TreeNode currentNode = getFatherNode(tNode);
while (currentNode != null && tNode == currentNode.llink) {
tNode = currentNode;
currentNode = getFatherNode(tNode);
}
return currentNode;
}
// 找节点的父节点
public TreeNode getFatherNode(TreeNode tNode) {
TreeNode current = root;
TreeNode trailCurrent = null;
while (current != null) {
if (current.info == tNode.info) {
break;
}
trailCurrent = current;
if (tNode.info < current.info) {
current = current.llink;
} else {
current = current.rlink;
}
}
return trailCurrent;
}
// 中序遍历
public void inOrder(TreeNode tNode) {
if (tNode != null) {
inOrder(tNode.llink);
System.out.print(tNode.info + " ");
inOrder(tNode.rlink);
}
}
// 打印树
public void printTree() {
System.out.println("inOrder");
inOrder(root);
System.out.println();
System.out.println("preOrder");
preOrder(root);
System.out.println();
System.out.println("postOrder");
postOrder(root);
System.out.println();
}
// 前序遍历
public void preOrder(TreeNode tNode) {
if (tNode != null) {
System.out.print(tNode.info + " ");
preOrder(tNode.llink);
preOrder(tNode.rlink);
}
}
// 后序遍历
public void postOrder(TreeNode tNode) {
if (tNode != null) {
postOrder(tNode.llink);
postOrder(tNode.rlink);
System.out.print(tNode.info + " ");
}
}
public static void main(String[] args) {
Tree tree = new Tree();
System.out.println("insert tree start");
tree.insertBinaryTree(15);
tree.insertBinaryTree(5);
tree.insertBinaryTree(16);
tree.insertBinaryTree(3);
tree.insertBinaryTree(12);
tree.insertBinaryTree(20);
tree.insertBinaryTree(10);
tree.insertBinaryTree(13);
tree.insertBinaryTree(18);
tree.insertBinaryTree(23);
tree.insertBinaryTree(6);
tree.insertBinaryTree(7);
System.out.println("insert tree end");
System.out.println("print tree start");
tree.treeDelete(15);
tree.printTree();
System.out.println("print tree end");
}
}