- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
15
Main Index
Contents
Build Tree
tnode<char> *buildTree(int n) { tnode<char> *root, *b, *c, *d, *e, *f, *g, *h, *i; switch(n) {case 0: d = new tnode<char> ('D'); e = new tnode<char> ('E'); b = new tnode<char> ('B',(tnode<char> *)NULL, d); c = new tnode<char> ('C',e, (tnode<char> *)NULL); root = new tnode<char> ('A',b, c); break;
Main Index
Contents
Selected Samples of Binary Trees
A A
B
C
B
C D E F G D
H
I
E
T ree A Siz e 9 D ep t h 3
T ree B Siz e 5 D ep t h 4
12
Main Index
Contents
Evaluating Tree Density
17
Main Index
Contents
Build Tree
case 2: g = new tnode<char> ('G'); h = new tnode<char> ('H'); i = new tnode<char> ('I'); d = new tnode<char> ('D',(tnode<char> *)NULL, g); e = new tnode<char> ('E',h, i); f = new tnode<char> ('F'); b = new tnode<char> ('B',d, (tnode<char> *)NULL); c = new tnode<char> ('C',e, f); root = new tnode<char> ('A',b, c); break; } return root;
19
Main Index
Contents
3. Binary Tree Scan Algorithm Template<typename T> Void inorderOutput(tnode<T> *t, const string & separator==“ “) { if(t!=NULL) { inorderOutput<T>(t->left, separator); cout<<t->nodeValue<<separator; inorderOutput<T>(t->right,separator); } }
8
Main Index
Contents
Tree Node Level and Path Length – Depth Discussion
A
B
C
D
E
F
G
H
I
K
N o n -C o m p let eT ree (D ep t h 3 ) N o d es at lev el 3 d o n o t o ccu rp y left m o s t p o s it io n s
}
18
Main Index
Contents
3. Binary Tree Scan Algorithm Recursive Tree Traversals:(深度优先搜索) 1. Inorder Scan
Travers the left subtree(“go left”); Vist the node; Travers the right subtree(“go right”);
9
Main Index
Contents
Binary Tree Definition A binary tree T is a finite set of nodes with one of the following properties:
– –
(a) T is a tree if the set of nodes is empty. (An empty tree is a tree.) (b) The set consists of a root, R, and exactly two distinct binary trees, the left subtree TL and the right subtreeTR. The nodes in T consist of node R and all the nodes in TL and TR.
16
Main Index
Contents
Build Tree
case 1: g = new tnode<char> ('G'); h = new tnode<char> ('H'); i = new tnode<char> ('I'); d = new tnode<char> ('D'); e = new tnode<char> ('E',g, (tnode<char> *)NULL); f = new tnode<char> ('F',h, i); b = new tnode<char> ('B',d, e); c = new tnode<char> ('C',(tnode<char> *)NULL, f); root = new tnode<char> ('A',b, c); break;
l e ft
D
ri g h t
l e ft
E
ri g h t
l e ft
F
ri g h t
l e ft
G
ri g h t
l e ft
H
ri g h t
T re e N o de M o de l
14
Main Index
Contents
Build Tree Nodes
template <typename T> class tnode { public: T nodeValue; tnode<T> *left, *right; tnode(){} tnode (const T& item, tnode<T> *lptr = NULL, tnode<T> *rptr = NULL): nodeValue(item), left(lptr), right(rptr) {} };
Lecture 10
– Binary Trees
Tree Structures Binary Tree Definition Selected Samples / Binary Trees Binary Tree Nodes Binary Search Trees Locating Data in a Tree Removing a Binary Tree Node Using Binary Search Trees - Removing Duplicates Removing an Item From a Binary Tree
20
Main Index Contents
3. Binary Tree Scan Algorithm Template<typename T> Void preorderOutput(tnode<T> *t, const string & separator==“ “) { if(t!=NULL) { cout<<t->nodeValue<<separator; preorderOutput<T>(t->left, separator); preorderOutput<T>(t->right,separator); } }
2.
Preorder Scan
Vist the node; Travers the left subtree(“go left”); Travers the right subtree(“go right”);
3.
Postorder Scan
Travers the left subtree(“go left”); Travers the right subtree(“go right”); Vist the node;
注:1.二叉树和分支为2的树 2.多叉树、森林概念
10
Main Index
Contents
Binary Tree Definition
树转换为二叉树
A B F C G D E B C F D A
G
E
转换规则:树根的第一个子节点转换为二叉树根的左节点; 树根的其它节点依次转换为该左节点的右链;