binarytree.h
#pragma once
#include
#include
#include
#include
#include
#include"queue.h"
typedef
char btdatatype;
//樹typedef
struct treenode
treenode;
//二叉樹
typedef
struct btnode
btnode;
//建立二叉樹
btnode* createbtree(btnode **pproot,btdatatype* arr, size_t *pindex, btdatatype invalid, size_t len);
//建立乙個節點
btnode* buybtnode(btdatatype data);
//前序遍歷遞迴
void btreeprevorderrv(btnode *proot);
//中序遍歷遞迴
void btreeinorderrv(btnode *proot);
//後序遍歷遞迴
void btreepostorderrv(btnode *proot);
//層序遍歷
void btreelevelorder(btnode *proot);
//拷貝二叉樹
btnode* copybinarytree(btnode *proot);
//求二叉樹節點個數
size_t binarytreesize(btnode *proot);
//銷毀二叉樹
void binarytreedestory(btnode **proot);
//求葉子節點
size_t binarytreeleafsize(btnode *proot, size_t *count);
//二叉樹的映象(遞迴)
void binarytreemirrorrv(btnode *proot);
//二叉樹的映象(非遞迴)
void binarytreemirror(btnode *proot);
//求第k層的節點個數
size_t binarytreeknode(btnode *proot, int k);
//求二叉樹的高度
size_t binarytreeheight(btnode *proot);
//判斷是否是完全二叉樹
int iscompletebinarytree(btnode *proot);
//找當前樹中是否有data這個數
btnode* finddatabinarytree(btnode *proot, btdatatype data);
//找當前樹中是否有這個節點
int findnodebinarytree(btnode *proot, btnode *node);
//非遞迴前序遍歷(方法一)
void binarytreeprevorder1(btnode *proot);
//非遞迴前序遍歷(方法二)
void binarytreeprevorder2(btnode *proot);
//非遞迴的中序遍歷
void binarytreeinorder(btnode *proot);
//後序非遞迴遍歷
void binarytreepostorder(btnode *proot);
binarytree.c
#include"binarytree.h"
#include"queue.h"
btnode* buybtnode(btdatatype data)
newnode->_data = data;
newnode->_left = null;
newnode->_right = null;
return newnode;
}//建立二叉樹
btnode* createbtree(btnode **pproot, btdatatype* arr, size_t *pindex, btdatatype invalid, size_t len)
return *pproot;
}//前序遍歷
void
btreeprevorderrv
(btnode *proot)
//中序遍歷
void
btreeinorderrv
(btnode *proot)
//後序遍歷
void
btreepostorderrv
(btnode *proot)
//層序遍歷
void
btreelevelorder
(btnode *proot)
}//拷貝二叉樹
btnode* copybinarytree
(btnode *proot)
size_t binarytreesize(btnode *proot)
//銷毀二叉樹
void binarytreedestory(btnode **proot)
}//求葉子節點
size_t
binarytreeleafsize
(btnode *proot, size_t *count)
return *count;
}//交換函式
void
swap
(btnode **left, btnode **right)
//二叉樹的映象(前序遞迴)
void
binarytreemirrorrv
(btnode *proot)
//二叉樹的映象(層序非遞迴) 在層序遍歷的時候交換左右節點
void
binarytreemirror
(btnode *proot)
}//求第k層的節點個數
size_t
binarytreeknode
(btnode *proot, int k)
//求二叉樹的高度
size_t
binarytreeheight
(btnode *proot)
//判斷是否是完全二叉樹
intiscompletebinarytree
(btnode *proot)
else
else
if(cur->_right)
return 0; //不是完全二叉樹
else
if(cur->_left)
else
flag = 1;}}
return 1;
}//找當前樹中是否有data
btnode* finddatabinarytree
(btnode *proot, btdatatype data)
//找當前樹中是否有這個節點
intfindnodebinarytree
(btnode *proot, btnode *node)
//非遞迴的前序遍歷(方法一) 和層次遍歷的步驟一樣
void
binarytreeprevorder1
(btnode *proot)
}//非遞迴前序遍歷(方法二)
void
binarytreeprevorder2
(btnode *proot)
}}//非遞迴的中序遍歷
void binarytreeinorder(btnode *proot)
//去棧頂節點並遍歷
cur = stacktop(&s);
printf("%c ", cur->_data);
//出棧
stackpop(&s);
//再遍歷右子樹
cur = cur->_right;
}}//後序非遞迴遍歷
void binarytreepostorder(btnode *proot)
//取棧頂元素
top = stacktop(&s);
//如果該節點右子樹為空或者右子樹已經遍歷過了則遍歷這個節點,遍歷完出棧
if (null == top->_right || top->_right == sign)
else
cur = top->_right;
}}
test.c
#include"binarytree.h"
#include"queue.h"
test1
()int main
()
二叉樹面試題
1.求二叉樹節點個數 可以使用遞迴解決。將問題分解為求根節點 左子樹的節點數 右節點的節點數。實現 public size t size private size t size node root 2.求頁節點個數 頁節點 左右子樹都為空的節點被稱為頁節點,使用遞迴遍歷,當碰到乙個左右子樹為空的節點...
面試題 二叉樹
面試題 二叉樹 1.重建二叉樹 前序 中序 treenode reconstructbinarytree vector pre,vector vin treenode root new treenode pre 0 int pos 0 for pos pre left,vin left,pre ri...
二叉樹基本操作及面試題
include include include includeusing namespace std templatestruct binarytreenode 給出二叉樹節點的結構體 t value 值 binarytreenode pleft 左孩子 binarytreenode pright ...