数据结构4 Tree

数据结构4 TreeTree 的定义 tree 中没有 Loop 故每一个结点都是 subtree 所以访问多按照递归

大家好,欢迎来到IT知识分享网。

Tree的定义

tree中没有Loop,故每一个结点都是subtree,所以访问多按照递归。

n个Tree结点,有n-1条边。

degree of node:结点孩子的个数

degree of Tree:所有结点中最大的degree of node

parent:一个拥有子树的结点

chird:子节点

sibling:拥有相同的parent

leaf :没有child

path:从一个结点到另一个结点的路径

length of path :一个路径经过的边

depth:root到该结点的路径长度(由于根在最上方,因此depth描述了离根的深度)

height:该结点到leave的最大长度(由于Leave在最上方,因此height描述了该节点离最下方的高度)

注意图论中的degree之和为边的两倍,数据结构中的degree应加1

Tree的实现

任意Tree

struct TreeNode{ int element; struct TreeNode* firstchild; struct TreeNode* nextsibilng; }; 

一个结点包括了数据,第一个孩子的指针,同辈的指针

当不存在child或者sibling时,指针指向NULL

binary Tree

struct TreeNode{ int element; struct TreeNode* lchild; struct TreeNode* rchild; }; 

一个结点包括了数据,左孩子的指针,右孩子的指针。

可以看出,二叉树可以和任意树相互转化,但是转化后只是连接的顺序不变,parent和sibling的关系会发生改变

Tree Traversals

preorder:递归访问,顺序为 root leftsubtree rightsubtree

inorder:递归访问,顺序为leftsubtree root rightsubtree

postorder:递归访问,顺序为leftsubtree rightsubtree root

levelorder :每一层按照从左到右的顺序输出,从第一层到最后一层。

迭代访问:

第一个root入队列,

如果队列非空,

首元素出队列,首元素的每一个child入队列

迭代

Threaded Binary Trees

线索二叉树

若左指针为NULL,左指针指向中序中该节点的前一个

若右指针为NULL,右指针指向中序中该结点的后一个

包含一个空头,空头的左结点指向root,右节点都指向自己

当中序第一个元素的左节点为NULL时,指向空头。

构造线索时,空头本身也计入中序中,因此最后一个右节点如果为空,将会指向空头

结构

typedef struct ThreadedTreeNode *PtrTo ThreadedNode; typedef struct PtrToThreadedNode ThreadedTree; typedef struct ThreadedTreeNode { 
    int LeftThread; /* if it is TRUE, then Left */ ThreadedTree Left; /* is a thread, not a child ptr. */ ElementType Element; int RightThread; /* if it is TRUE, then Right */ ThreadedTree Right; /* is a thread, not a child ptr. */ } 

题目

1.It is always possible to represent a tree by a one-dimensional integer array.

任何二叉树可以通过一维数组表示(只需要把空结点空出来),任何树可由二叉树表示。因此对。T

2.There exists a binary tree with 2016 nodes in total, and with 16 nodes having only one child.(此题由何钦铭先生所出)

去掉只有一个孩子的结点,说明2000个结点的树是full binary tree。2000不是2的指数。F

3.Given a tree of degree 3. Suppose that there are 3 nodes of degree 2 and 2 nodes of degree 3. Then the number of leaf nodes must be ____.

画图即可,注意degree是孩子的个数,与图论中degree不同。8

4.Among the following threaded binary trees (the threads are represented by dotted curves), which one is the postorder threaded tree?

问是不是后序线索二叉树。需要看右侧的线索是不是指向后序的后一个结点,左侧的线索是不是后序的前一个结点。

5.A full tree of degree 3 is a tree in which every node other than the leaves has 3 children. How many leaves does a full tree of degree 3 have if it has 217 nodes?

6.Given a quadtree(四叉树) with 4 nodes of degree 2, 4 nodes of degree 3, 3 nodes of degree 4. The number of leaf nodes in this tree is __.

注意里面描述的顶点degree是不用加1的。L+4*3+4 * 4 + 3 * 5-1=2(4+4+3+L-1),L=22

7.How many leaf node does a complete binary tree with 2435 nodes have?
(5分)
A.1218
B.1217
C.812
D.cannot be determined

首先确定有几层:1+2+…+2^n = 2435,用求根公式看应该是n=10,倒数第二层有2 ^10个顶点,最后一层有2435-(2 ^11-1) = 388,说明最后一层有388个顶点,最后一层的顶点对应1/2个上一层顶点,因此多算了194次,leave=388+1024-194=1218

8.For an in-order threaded binary tree, if the pre-order and in-order traversal sequences are B E A C F D and A E C B D F respectively, which pair of nodes’ right links are both threads?
(4分)
A.E and F
B.A and D
C.A and E
D.B and E
从中序和前序看树:前序的第一个是根,即B是根,对应到中序,AEC和DF是两个子树,再看前序中,AEC最前面是E,所以E是根,F是根,然后确定,注意当孩子是NULL时才换成线索,因此只有没有右孩子的才满足。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/154505.html

(0)
上一篇 2025-02-25 16:45
下一篇 2025-02-25 17:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信