二叉樹結點的抽象資料型別:
1 template二叉樹結點函式功能實現:2class
binarytreenode
3;
1 template二叉樹的抽象資料型別:2 binarytreenode::binarytreenode()
36 template
7 binarytreenode::binarytreenode(const t&ele)
812 template
13 binarytreenode::binarytreenode(const t& ele, binarytreenode* l, binarytreenode*r)
1419 template
20bool binarytreenode::isleaf() const
21
1 template廣度優先遍歷(佇列):2class
binarytree39
bool isempty() const; //
判斷二叉樹是否為空樹
10 binarytreenode* getroot() const; //
返回二叉樹的根結點
11void breadthfirstorder(binarytreenode* root);//
廣度優先遍歷以root為根結點的子樹
12void preorder(binarytreenode* root); //
先序遍歷以root為根結點的子樹
13void inorder(binarytreenode* root); //
中序遍歷以root為根結點的子樹
14void postorder(binarytreenode* root); //
後序遍歷以root為根結點的子樹
15void deletebinarytree(binarytreenode* root); //
刪除以root為根結點的子樹
16void visit(binarytreenode* pointer); //
訪問當前結點
17 binarytreenode* build_from_pre_and_in(char* preorder, char* inorder, int
n);18
//根據前序和中序遍歷表示式構造二叉樹
19 binarytreenode* build_from_post_and_in(char* postorder, char* inorder, int
m);20
//根據後序和中序遍歷表示式構造二叉樹
21int getrootid1(char *preorder, char *inorder, int n); //
返回根結點在中序遍歷表示式中序號
22int getrootid2(char *postorder, char *inorder, int m); //
返回根結點在中序遍歷表示式中序號
23 };
【思路】根結點入隊,佇列不空迴圈,訪問隊頭並出隊,左子樹不空則入隊,右子樹不空則入隊。
1 template先序遍歷:2void binarytree::breadthfirstorder(binarytreenode*root)
318 }
【思路】
1.訪問當前結點
2.當前結點的右兒子結點非空,則入棧
3.左兒子結點非空,使之作為當前結點,否則彈出棧頂元素,使之作為當前結點
4.反覆執行1、2、3,至棧空為止
1 template中序遍歷:2void binarytree::preorder(binarytreenode*root)315
else
1620
}21 }
【思路】
1.每遇到乙個結點就把它壓棧,然後去遍歷其左子樹
2.遍歷完左子樹後,從棧頂彈出這個結點並訪問之
3.然後遍歷該結點的右子樹
1 template後序遍歷:2void binarytree::inorder(binarytreenode*root)313
else
1420
}21 }
【基本思想】
1.每遇到乙個結點,先把它推入棧中,去遍歷它的左子樹
2.遍歷完它的左子樹後,應繼續遍歷該結點的右子樹
3.遍歷完右子樹之後,才從棧頂彈出該結點並訪問它
【解決方案】
0.將根結點作為當前結點
1.進棧過程:
a.如果當前結點不空且具有左子樹,將當前結點壓入棧中,否則進入2
b.將當前結點的左子樹的根結點設定為當前結點
c.重複 a
2.出棧過程:
a.如果當前結點不空且沒有右子樹,或者其右子樹的根結點已經訪問,訪問之,否則進入3
b.若棧空,結束,否則取出當前棧頂結點作為當前結點
c.重複 a
3.將當前結點壓入棧中
4.將當前結點的右子樹的根結點設為當前結點,重複 1
1 template刪除以root為根結點的子樹:2void binarytree::postorder(binarytreenode*root)313
//出棧過程
14while (pointer != null && (pointer->rightchild == null || pointer->rightchild ==pre))
15//
當前結點右孩子為空或右孩子剛被訪問過,則訪問該結點
1624
//將當前結點壓入棧中
25nodestack.push(pointer);
26//
將當前結點的右子樹的根結點設為當前結點
27 pointer = pointer->rightchild;28}
29 }
1 template根據前序和中序遍歷表示式構造二叉樹:2void binarytree::deletebinarytree(binarytreenode*root)
3
【思路】
根據前序序列,找到根結點在中序序列中的位置,遞迴根結點的左子樹序列和右子樹序列。
1 template根據後序和中序遍歷表示式構造二叉樹:2 binarytreenode* binarytree::build_from_pre_and_in(char* preorder, char* inorder, intn)3
13 binarytreenode* root = new binarytreenode;
14 root->element =root_element;
15 root->leftchild = build_from_pre_and_in(preorder + 1
, inorder, i);
16 root->rightchild = build_from_pre_and_in(preorder + i + 1, inorder + i + 1, n - i - 1
);17
return
root;
18 }
【思路】根據後序序列,找到根結點在中序序列中的位置,遞迴根結點的左子樹序列和右子樹序列。
1 template測試函式:2 binarytreenode* binarytree::build_from_post_and_in(char* postorder, char* inorder, intm)3
13 binarytreenode* root = new binarytreenode;
14 root->element =root_element;
15 root->leftchild =build_from_post_and_in(postorder, inorder, i);
16 root->rightchild = build_from_post_and_in(postorder+i, inorder + i+1, m-i-1
);17
return
root;
18 }
1測試結果:intmain()230
//測試的二叉樹
31//a32
//b c
33//
d e
34//
f g h
二叉樹類BinaryTree
二叉樹是結點的有限集合,該集合或者為空集,或者是由乙個根和兩棵互不相交的稱為該根的左子樹和右子樹的二叉樹組成.二叉樹可以為空集,可以有空二叉樹,也可以有空的左子樹 或 和 又子樹.二叉樹的性質 1.第i層至多有2 i 1 個結點.2.高度為h的二叉樹上至多有2 h 1個結點.3.包含n個元素的二叉樹...
BinaryTree 學習二叉樹的Python庫
原文 python library for learning binary trees 作者 joohwan翻譯 賴信濤責編 仲培藝 學過二叉樹的朋友都有過這樣的經歷 按照二叉樹的資料手動模擬畫出來二叉樹。但是現在,有了binarytree這個庫,你可以不必費這個麻煩了!binarytree是乙個小...
Go語言實現二叉樹(BinaryTree)
二叉樹的操作原理 取第乙個節點為根節點,比根節點小的數放在根節點的左子樹 左節點 比根節點大的數放在根節點的右子數 右節點 取得的時候按照中序遍歷的方式 左 中 右 實現 如下 節點結構體 type node struct 二叉樹結構體 type binarytree struct 查詢時記錄下標 ...